0
假设我想加载一个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)的问题是缩放级别ISN”保存在图像文件上。是否有一种方法可以保存它?
2)当我在做MapViewport(featureCollection.getBounds());
有没有办法扩大一点点的边界,以获得更好的视觉表现?
...
尼斯,感谢无论价值我试着('double delta = bounds.getWidth()/ 20.0'),没有任何效果。因此,我只使用'double delta = bounds.getWidth()',它可以正常工作(缩小)。另外,我如何在我的情况下使用'JMapPane'?因为我正在使用'MapContent'.Thanks!(upv) – George
检查调试器中getwidth的值是什么?除以20应该给你5%,这可能太小而不能显示 –
如果你有一个JMapFrame,你可以从中得到mapPane,如果没有的话就不要担心。 –