1

I would like to resample a raster to multiple rasters of a certain amount of pixels in width and height based on a bilinear resampling method.

However, I am not sure if the available syntax allows to do this from the QGIS Python console.

This works fine:

processing.runalg('gdalogr:translate',{"INPUT":rlayer, "OUTSIZE":j, "OUTSIZE_PERC":False, "EXPAND":0, "PROJWIN":"%f,%f,%f,%f" % (xmin, xmax, ymin, ymax), "OUTPUT":resampled})

For the snipped above the default resample method (nearest neighbor) is used. According to the gdal manual, the method can be changed with the -r flag, but from processing.alghelp("gdalogr:translate"), this option seems to be unavailable.

Therefore, I thought of using gdalogr:warpreproject instead, where the resampling method can be defined. However, now I cannot define the amount of pixels in width and height as indicated in the gdal manual, because the TS flag is not available according to processing.alghelp("gdalogr:warpreproject").

Optimal would be something like:

processing.runalg('gdalogr:warpreproject',{"INPUT":rlayer, "METHOD":1, "RAST_EXT":"%f,%f,%f,%f" % (xmin, xmax, ymin, ymax),"TS":"%i,%i" % (j, j), "OUTPUT":resampled})

Does anyone know how to solve this?

  • Have you tried inserting those flags in the extra parameter of those algorithms? – Joseph Oct 18 '16 at 10:30
  • Rather than use the processing.runalg function, you could import gdal and then call gdal.Translate() directly, which will give you all of its functionality. – user6072577 Oct 18 '16 at 11:11
  • Both sound like valid solutions, but I struggle finding syntax examples for defining the extra parameter and gdal.Translate() parameters in PyQGIS. Could you provide a short example? – Sophie Crommelinck Oct 18 '16 at 13:58
1

I found the correct syntax for defining the extra parameter when using gdal methods in QGIS processing scripts:

"EXTRA": "-r {bilinear}"

The problem above is therefore solved with:

processing.runalg('gdalogr:translate',{"INPUT":rlayer, "OUTSIZE":j, "OUTSIZE_PERC":False, "EXPAND":0, "EXTRA": "-r {bilinear}","PROJWIN":"%f,%f,%f,%f" % (xmin, xmax, ymin, ymax), "OUTPUT":resampled})

I assume further extra parameters from the gdal manual can similarly be set.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.