【问题标题】:save zoom image level to file将缩放图像级别保存到文件
【发布时间】:2017-07-24 10:15:19
【问题描述】:

假设我想加载一个 shp 文件,在上面做我的事情并将地图保存为图像。

为了保存我正在使用的图像:

public void saveImage(final MapContent map, final String file, final int imageWidth) {

      GTRenderer renderer = new StreamingRenderer();
      renderer.setMapContent(map);

      Rectangle imageBounds = null;
      ReferencedEnvelope mapBounds = null;

      try {
          mapBounds = map.getMaxBounds();
          double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
          imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
      } catch (Exception e) {
          // Failed to access map layers
          throw new RuntimeException(e);
      }

      BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
      Graphics2D gr = image.createGraphics();
      gr.setPaint(Color.WHITE);
      gr.fill(imageBounds);

      try {
          renderer.paint(gr, imageBounds, mapBounds);
          File fileToSave = new File(file);
          ImageIO.write(image, "png", fileToSave);
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
  }

但是,假设我正在做这样的事情:

...
MapContent map = new MapContent();
map.setTitle("TEST");
map.addLayer(layer);
map.addLayer(shpLayer);

// zoom into the line
MapViewport viewport = new MapViewport(featureCollection.getBounds());
map.setViewport(viewport);

saveImage(map, "/tmp/img.png", 800);

1) 问题是缩放级别没有保存在图像文件上。有没有办法保存它?

2) 当我在做MapViewport(featureCollection.getBounds()); 时,有没有办法扩展一点边界以获得更好的视觉表现?

...

【问题讨论】:

    标签: java gis geotools


    【解决方案1】:

    您没有以当前缩放级别保存地图的原因是在您的 saveImage 方法中,您有以下行:

    mapBounds = map.getMaxBounds();
    

    始终使用地图的全图,您可以将其更改为

    mapBounds = map.getViewport().getBounds();
    

    您可以通过以下方式扩展边界框:

        ReferencedEnvelope bounds = featureCollection.getBounds();
        double delta = bounds.getWidth()/20.0; //5% on each side
        bounds.expandBy(delta );
        MapViewport viewport = new MapViewport(bounds);
        map.setViewport(viewport );
    

    从 GUI 保存地图的一种更快(更简单)的方法是使用这样的方法,它可以准确地保存屏幕上的内容:

    public void drawMapToImage(File outputFile, String outputType,
            JMapPane mapPane) {
        ImageOutputStream outputImageFile = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(outputFile);
            outputImageFile = ImageIO.createImageOutputStream(fileOutputStream);
            RenderedImage bufferedImage = mapPane.getBaseImage();
            ImageIO.write(bufferedImage, outputType, outputImageFile);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (outputImageFile != null) {
                    outputImageFile.flush();
                    outputImageFile.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
            } catch (IOException e) {// don't care now
            }
        }
    }
    

    【讨论】:

    • 很好,谢谢!无论我试图用什么值除(double delta = bounds.getWidth()/20.0),都没有任何效果。所以,我只使用double delta = bounds.getWidth(),它工作正常(它缩小了)。另外,在我的情况下我应该如何使用JMapPane?因为我使用的是MapContent。谢谢!(upv)
    • 在调试器中检查getwidth的值是多少?除以 20 应该给你 5%,这可能太小而无法显示
    • 如果你有一个 JMapFrame,你可以从中获取 mapPane,如果没有,那么别担心。
    • delta = 0.5 使用 bounds.getWidth() 时。 boundsmaxx = 25.22, maxy = 38.7 minx = 24.72, miny = 37.17 的值。我想我会保持这样,没有分割。
    • :嗨!你知道有没有一个函数可以取坐标点并返回时间/时区?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多