Search code examples
openlayersgeoserverwms

Export properly a PNG from a WMS layer (BBOX issue). How does Openlayers recalculate the bbox?


I have a geoserver and exporting an image from a wms layer by using the WMS GetMap request with format=image/png, I'm getting a stretched image, i.e. IMG 1:

Text

By consuming the same WMS layer with format=application/openlayers, OpenLayers library changes/fit the bbox of the layer obtaining a proper image (not stretched), i.e. IMG 2:

Text

The only difference between WMS requests one and two is the (output) format (all other parameters of the request srs, width, height, etc.. are identical). OpenLayers recalculates a new bbox in order to visualize a proper image, what is the algorithm to do this?

Thx.


Solution

  • To avoid stretching you must ensure the width and height parameters aspect ratio is the same as that of the bbox

    For example in https://openlayers.org/en/latest/examples/wms-image.html

    The served image url includes

    &WIDTH=658&HEIGHT=355&CRS=EPSG:3857&BBOX=-13888944.635145342,2867630.7173607955,-7451112.364854658,6340929.2826392045
    

    where

    ((-7451112.364854658) - (-13888944.635145342)) / (6340929.2826392045 - 2867630.7173607955) = 658 / 355
    

    (note that for some projections the BBOX axis order may be reversed)