使用GTRenderer渲染OSM切片

时间:2019-01-09 10:52:45

标签: geotools

我正在尝试使用GTRenderer渲染OSM切片:

$.inArray

然后我将我的var names = ['search-resources', 'calendar']; if ($.inArray('calendar', names) !== -1) { // do stuff } 发送到

MapContent map = new MapContent();    
String baseURL = "http://<url>"; // OSM url
TileService service = new OSMService("OSM", baseURL);
TileLayer tileLayer = new TileLayer(service);
map.addLayer(tileLayer);

但是它返回“空白”图像。

同时,我的map过程在WMS层上可以正常工作。这可能是什么问题?

我还通过以下方式检查了我的OSM服务

public static void saveImage(final MapContent map,
                                 final String file,
                                 ReferencedEnvelope envelope,
                                 final int imageWidth,
                                 String formatName) {

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

        java.awt.Rectangle imageBounds = null;
        ReferencedEnvelope mapBounds = null;
        try {
            mapBounds = envelope;
            double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
            imageBounds = new java.awt.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, formatName, fileToSave);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

它返回正确的图块。

已编辑

我还在日志中找到以下消息:

saveImage

1 个答案:

答案 0 :(得分:0)

感谢Ian Turton对缩放级别的评论。 我将停点放置到TileLayer的draw(Graphics2D图形,MapContent地图,MapViewport theViewport)方法中,发现我的MapViewport不正确。 添加

// map is my MapContent object, envelope is a map's ReferencedEnvelope
imageBounds = new java.awt.Rectangle(
                0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
map.getViewport().setScreenArea(imageBounds);
map.getViewport().setBounds(envelope);

解决了问题。