从WMS向Java本地存储请求图像



我一直在尝试使用Geotools从Java中的WMS中检索图像,并将图像保存在本地。我只是关注了这个链接https://gitlab.com/-/snippets/1883964Ian Turton和官方文件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文档似乎是";无效";因为它没有设置CCD_ 1。

我以前从未见过这个错误,所以它可能是由于GeoTools使用的XML解析器最近的升级或其他一些更改,或者MapServer行为的更改。从技术上讲,这不是GeoTools的错误,但最好能找到一种方法来修复它,这样我们就可以继续使用MapServer WMS了。

我认为问题正如错误消息所示。

在下面的代码段中<lt;LF>gt;字符序列用于指示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 的产物

最新更新