I'm currently writing a plugin which at some point saves an image of dynamic extent and resolution from WMS as a TIFF. I'm using the following code to archieve this:

ds=gdal.Translate(localrasterpath,"WMS:"+wms_url,height=height,width=width, projWin=[xMin,yMax,xMax,yMin])

While this works, it takes a lot of time. It takes about 2.2 seconds to execute this line for a 2500x2500px image. This would be fine, if it wasn't used in a for-loop and thus executed 31 times (once for each layer in the wms) per run, totalling about 70 seconds.

Since this is like 90% of the time my plugin takes to run I was wondering if there was a more efficient way to do this?

For context, these are the steps in which I use the produced image:

#binarize the image via numpy array                
im = np.array(Image.open(localrasterpath).convert('L'))
im_bin = (im < 250) * 255
del im, im_bin, ds
#re-georeference the image
src_ds = gdal.Open(binrasterpath)
srs = osr.SpatialReference()
dest_wkt = srs.ExportToWkt()

If there was a way to open the WMS directly as a numpy array I'd be very happy, however I couldn't find anything myself.

  • 1
    How long does it take to read the image from WMS with plain http request? Is the problem that the WMS does not support TIFF as an output format and therefore you need to use GDAL.
    – user30184
    Dec 8, 2022 at 10:52
  • why not request all the layers together in one hit
    – Ian Turton
    Dec 8, 2022 at 13:12
  • @IanTurton I need to create vectorlayers from the WMS. The features of said vectorlayers should know from which WMS Layer they were created. From what I've found the by far easiest (if not only) way to archieve this is to polygonize each layer individually. I know WMS isn't supposed to be used this way, but it's the only data source available.
    – JannisM
    Dec 12, 2022 at 10:45
  • @user30184 Both creating+loading a raster layer from the wms and getting an image when accessed directly via the web browser take about 0.2 seconds each. Saving the image as a png, which is supported by the WMS, takes around 3 seconds per layer, so even worse than TIFF
    – JannisM
    Dec 12, 2022 at 12:58
  • 1
    Have you measured how much time the steps in your code are using? Have you considered to use just gdal_translate with the WMS driver gdal.org/drivers/raster/wms.html? The usage would be something like gdal_translate -of GTiff -outsize 2500 2500 -projwin ulx uly lrx lry my_wms_config.xml my_output.tif.
    – user30184
    Dec 12, 2022 at 15:19


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.