-1

Do You know any way to save a WMS tile as an image (especially .png) using Java?

I have a tile, for example:

http://mapy.geoportal.gov.pl/wss/service/img/guest/ORTO/MapServer/WMSServer?VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&LAYERS=Raster&SRS=EPSG:4326&WIDTH=500&HEIGHT=500&TRANSPARENT=TRUE&FORMAT=image/png&BBOX=23.805441,50.98483844444444,23.807441,50.98594955555556&styles=

My code looks like:

public static void saveImage(String imageUrl, String destinationFile) throws IOException {

    URL url = new URL(imageUrl);
    InputStream is = url.openStream();
    OutputStream os = new FileOutputStream(destinationFile);

    byte[] b = new byte[2048];
    int length;

    while ((length = is.read(b)) != -1) {
        os.write(b, 0, length);
    }

    is.close();
    os.close();
}

It works for normal images like http://www.delaval.com/ImageVaultFiles/id_15702/cf_5/st_edited/AYAbVD33cXEhPNEqWOOd.jpg

Should I use any special library?

Brewer
  • 13
  • 2

1 Answers1

0

It seems that maps.geoportal.gov.pl checks User-Agent header and when you connect like this, there is no User-Agent header sent to server. If you set this header to some accepted value (e.g. Mozilla/5.0 seems to be valid), you will get the image you want.

So instead of

InputStream is = url.openStream();

try

URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
InputStream is = connection.getInputStream();

and it should work.