簡體   English   中英

Java中從WMS請求圖片到本地存儲

[英]Request image from WMS to local storage in Java

我一直在嘗試使用 Geotools 從 Java 中的 WMS 檢索圖像並將圖像保存在本地。 我只是點擊了 Ian Turton 的這個鏈接https://gitlab.com/-/snippets/1883964和官方文檔https://docs.geotools.org/latest/userguide/extension/wms/wms.html

我的代碼是

package wmsexplore;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.geotools.ows.ServiceException;
import org.geotools.ows.wms.WebMapServer;
import org.geotools.ows.wms.request.GetMapRequest;
import org.geotools.ows.wms.response.GetMapResponse;

public class WMSConnector {
  public static void main(String[] args) {


    URL url = null;
    try {
      url = new URL("http://maps.heigit.org/osm-wms/service?service=WMS&request=GetCapabilities&version=1.1.0");
    } catch (MalformedURLException e) {
      // will not happen
    }

    WebMapServer wms = null;
    try {
      wms = new WebMapServer(url);
      GetMapRequest request = wms.createGetMapRequest();
      request.addLayer("osm_auto:all", "");
      String format = "image/png";
      request.setFormat(format);
      request.setDimensions("1000", "1000"); // sets the dimensions of the image
                                           // to be returned from the server
      request.setTransparent(true);
      request.setSRS("EPSG:4326");
      request.setBBox("-71.13,42.32,-71.03,42.42");

      try {
        GetMapResponse response = (GetMapResponse) wms.issueRequest(request);
        if (response.getContentType().equalsIgnoreCase(format)) {
          BufferedImage image = ImageIO.read(response.getInputStream());
          File outputfile = new File("saved.png");
          ImageIO.write(image, "png", outputfile);
        } else {
          StringWriter writer = new StringWriter();
          IOUtils.copy(response.getInputStream(), writer);
          String error = writer.toString();
          System.out.println(error);

        }
      } catch (ServiceException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

      }
    } catch (IOException e) {
      // There was an error communicating with the server
      // For example, the server is down
    } catch (ServiceException e) {
      // The server returned a ServiceException (unusual in this case)
    }

  }
}

我得到的錯誤是:

Feb 09, 2022 2:09:36 AM org.geotools.xml.XMLSAXHandler fatalError
SEVERE: FATAL White spaces are required between publicId and systemId.
Feb 09, 2022 2:09:36 AM org.geotools.xml.XMLSAXHandler fatalError
SEVERE: col 50, line 1
Feb 09, 2022 2:09:36 AM org.geotools.data.ows.AbstractOpenWebService internalIssueRequest
SEVERE: Failed to execute request http://maps.heigit.org/osm-wms/service?REQUEST=GetCapabilities&VERSION=1.1.0&SERVICE=WMS
Feb 09, 2022 2:09:36 AM org.geotools.xml.XMLSAXHandler fatalError
SEVERE: FATAL White spaces are required between publicId and systemId.
Feb 09, 2022 2:09:36 AM org.geotools.xml.XMLSAXHandler fatalError
SEVERE: col 50, line 1
Feb 09, 2022 2:09:36 AM org.geotools.data.ows.AbstractOpenWebService internalIssueRequest
SEVERE: Failed to execute request http://maps.heigit.org/osm-wms/service?request=capabilities&service=WMS&wmtver=1.0.0

這里可能出了什么問題? 我在這里先向您的幫助表示感謝 !!

從快速檢查(和錯誤消息的谷歌)來看,該服務器返回的 XML 文檔似乎是“無效的”,因為它沒有設置systemId

我以前沒有見過這個錯誤,所以這可能是由於最近升級了 GeoTools 使用的 XML 解析器或其他一些更改,或者 MapServer 行為的更改。 從技術上講,這不是 GeoTools 錯誤,但最好找到一種方法來修復它,以便我們可以繼續使用 MapServer WMS。

我認為問題出在錯誤消息中。

在下面的代碼片段中,<< LF >> 字符序列表示 LF 字符。 任何存在的空白都是空白。 在 DTD URL 字符串和 VendorSpecificCapabilities 元素之間沒有空格。 除非 LF 字符被認為是空白; 我認為這是實現中的一個邊緣案例。 這可能是問題的原因。

<?xml version="1.0"?><<LF>>
<!DOCTYPE WMT_MS_Capabilities SYSTEM 
"http://schemas.opengis.net/wms/1.1.0/capabilities_1_1_0.dtd"<<LF>>
[<<LF>>
 <!ELEMENT VendorSpecificCapabilities EMPTY><<LF>>
 ]>  <!-- end of DOCTYPE declaration --><<LF>>
<WMT_MS_Capabilities version="1.1.0"><<LF>>
<Service><<LF>>
...

SYSTEM 字和 DTD URL 字符串之間的換行是 stackoverflow 的產物

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM