【问题标题】:GeoTools WebMapServer GetMapRequest issueGeoTools WebMapServer GetMapRequest 问题
【发布时间】:2015-02-26 12:21:11
【问题描述】:

我正在使用 GeoTools 12.2 开发 java 类库项目。

首先,我正在使用this guide 开发 GeoTools WMS 模块。 我失败的一点是获取地图请求,以便我可以获得功能文档和图层等。

我的 wms 网址http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer

它包含 3 层(州、河流、城市)

我正在使用结构来获取如下地图操作。

GetMapRequest getMapRequest = wms.createGetMapRequest();//wms is my WebMapServer object

getMapRequest.addLayer(tempLayer);//tempLayer contains states layer

GetMapResponse response = (GetMapResponse) wms.issueRequest(getMapRequest);

BufferedImage image = ImageIO.read(response.getInputStream());

我还在指南中尝试了其他方法来执行 GetMapRequest,但我无法成功,总是将 NullPointerException 传递给 BufferedImage 对象。

你有什么建议?提前致谢。

【问题讨论】:

    标签: java gis geotools


    【解决方案1】:

    您需要为您的请求设置更多参数,WMS getMapResponse 不为其中几个提供任何默认值(因为它们是您的请求/地图所独有的)。因此,您至少需要以下内容:

    private BufferedImage getLayer(Layer l) {
        GetMapRequest getMapRequest = wms.createGetMapRequest();
        getMapRequest.addLayer(l);
        getMapRequest.setBBox(l.getEnvelope(DefaultGeographicCRS.WGS84));
        getMapRequest.setDimensions(200, 400);
        getMapRequest.setFormat("image/png");
        getMapRequest.setSRS("CRS:84");
        System.out.println(getMapRequest.getFinalURL());
        try {
            GetMapResponse response = wms.issueRequest(getMapRequest);
            BufferedImage image = ImageIO.read(response.getInputStream());
            return image;
        } catch (ServiceException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    
    }
    

    一般来说,为了避免出现空图像,您可以对响应进行一些错误检查:

         if (response.getContentType().equalsIgnoreCase("image/png")) {
                BufferedImage image = ImageIO.read(response.getInputStream());
                return image;
            } else {
                StringWriter writer = new StringWriter();
                IOUtils.copy(response.getInputStream(), writer);
                String error = writer.toString();
                System.out.println(error);
                return null;
            }
    

    这会给你一个 XML 编码的错误来告诉你出了什么问题:

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <ServiceExceptionReport version="1.3.0"
      xmlns="http://www.opengis.net/ogc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
      <ServiceException code="InvalidFormat">
    Parameter 'bbox' can not be empty.
      </ServiceException>
    </ServiceExceptionReport>
    

    【讨论】:

    • 感谢@iant 的回复。我明白了,它基于设置尺寸。有没有办法获得默认尺寸(宽度,高度)。当我通过 GeoTools getdimensions 函数等尝试时,没有维度值。实际上我想以编程方式设置所有 GetMap 参数。
    • 尺寸决定了图像的大小,因此取决于您对图像的处理方式。唯一需要注意的是纵横比应该与边界框的纵横比相匹配,否则您的地图将被扭曲。
    • 我想将图像添加到网页中的 openlayers 地图。如何动态计算宽度和高度值?如果我无法正确设置尺寸,图像将不会与地图重叠。
    • 您正在考虑地图所在的边界框,尺寸是图像的大小。但是OL会直接和WMS对话,根本不需要GeoTools
    猜你喜欢
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多