Java 从WMS请求地图,并将其作为PNG图像保存到磁盘

Java 从WMS请求地图,并将其作为PNG图像保存到磁盘,java,geotools,Java,Geotools,我必须使用java和GeoTools编写代码来执行WMS请求,获取图像并将其保存到计算机上的特定位置。我遵循了GeoTools WMS教程,代码编译时没有错误,但我不知道如何检查它是否有效,或者如何保存请求的图像 以下是带有所有必要参数的GetMap请求: 代码如下: public class WmsConnectorMaven { public static void main(String[] args) { URL url = null; try

我必须使用java和GeoTools编写代码来执行WMS请求,获取图像并将其保存到计算机上的特定位置。我遵循了GeoTools WMS教程,代码编译时没有错误,但我不知道如何检查它是否有效,或者如何保存请求的图像

以下是带有所有必要参数的GetMap请求:

代码如下:

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)
        } 



    }
}

您需要检查返回的
响应的
contentType
,并根据该值做出决定。比如:

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;
} 
更新

我刚刚跑了,我得到:

<?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
删除(错误拼写)“defualt”将给出(我想这是正确的):


我已经尝试了您的代码,它在返回行中给出了一个错误,即它们不能在void方法中返回值。除此之外,我如何从WMS中选择一个特定的层,这似乎列出了服务器上的所有nemed层(层:WMSUtils.getNamedLayers(功能)){request.addLayer(层);}
在主方法中,您不需要返回,您的代码指定了图层名称和样式。在省略返回行后,这就是我得到的
缺少的参数['layers'、'styles'、'crs']
您的错误报告帮助我使其正常工作。我基本上添加了
Layer[]layers=WMSUtils.getNamedLayers(功能);请求添加层(层[1])
以解决缺少的“layer”和“style”参数,以及
request.setSRS(“EPSG:4326”)以解决缺少的“crs”参数。有了它,它只需使用
try{GetMapResponse=(GetMapResponse)wms.issueRequest(请求);buffereImage=ImageIO.read(response.getInputStream());ImageIO.write(image,“png”,新文件(“C:\\Users\\Edhem\\Desktop\\salzburg.png”);
…谢谢Ian!