gpt4 book ai didi

java - 从 WMS 请求 map 并将其作为 PNG 图像保存到磁盘

转载 作者:行者123 更新时间:2023-11-30 12:03:53 25 4
gpt4 key购买 nike

我必须使用 java 和 GeoTools 编写代码来执行 WMS 请求、获取图像并将其保存到我计算机上的特定位置。我已按照 GeoTools WMS 教程进行操作,代码编译无误,但我不知道如何检查它是否有效或如何保存请求的图像?

这是带有所有必要参数的 GetMap 请求:http://ows.mundialis.de/services/service?request=GetMap&service=WMS&version=1.3.0&layers=OSM-Overlay-WMS&styles=default&crs=EPSG%3A4326&bbox=47.75,12.98,47.86,13.12&&width=2000&height=2000&format=image/png&transparent=true

代码如下:

public class WmsConnectorMaven {

public static void main(String[] args) {

URL url = null;
try {
url = new URL("http://ows.mundialis.de/services/service?service=wms&version=1.3.0&request=GetCapabilities");
} catch (MalformedURLException e) {
//will not happen
}

WebMapServer wms = null;
try {
wms = new WebMapServer(url);
GetMapRequest request = wms.createGetMapRequest();
request.addLayer("OSM-Overlay-WMS", "defualt");
request.setFormat("image/png");
request.setDimensions("800", "800"); //sets the dimensions of the image to be returned from the server
request.setTransparent(true);
request.setSRS("EPSG:4326");
request.setBBox("47.75,12.98,47.86,13.12");

GetMapResponse response = (GetMapResponse) wms.issueRequest(request);
BufferedImage image = ImageIO.read(response.getInputStream());

/* File outputfile = new File("saved.png");
ImageIO.write(image, "png", outputfile); */

// FileOutputStream img = new FileOutputStream("C:\\Users\\Edhem\\Desktop\\WMSimage.png");
} 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)
}



}
}

最佳答案

您需要检查返回的responsecontentType 并根据该值做出决定。像这样的东西:

try {
GetMapResponse response = wms.issueRequest(getMapRequest);
if (response.getContentType().equalsIgnoreCase(format)) {
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;
}
} catch (ServiceException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

更新

我刚刚跑了your code with my checks我得到:

<?xml version="1.0"?>
<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="StyleNotDefined">unsupported styles: defualt</ServiceException>
</ServiceExceptionReport>

删除(拼写错误的)“defualt”给出(我猜是正确的):

enter image description here

关于java - 从 WMS 请求 map 并将其作为 PNG 图像保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57417562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com