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])
ds=None
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
Image.fromarray(np.uint8(im_bin)).save(binrasterpath)
del im, im_bin, ds
#re-georeference the image
src_ds = gdal.Open(binrasterpath)
src_ds.SetGeoTransform(gdal.Open(localrasterpath).GetGeoTransform())
srs = osr.SpatialReference()
srs.ImportFromEPSG(epsg)
dest_wkt = srs.ExportToWkt()
src_ds.SetProjection(dest_wkt)
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.
gdal_translate -of GTiff -outsize 2500 2500 -projwin ulx uly lrx lry my_wms_config.xml my_output.tif
.