[GIS] Use raster memory layer from QGIS processing result

memory-layerpyqgisqgisqgis-processingraster

I want to apply subsequent QGIS raster processing algorithms in a python script. I want to use memory layers for intermediate calculation results since I am only interested in the end result.

As described in a previous post, for vector layers this works fine when run in the Python console:

processing.runandload("qgis:intersection", layer1, layer2, "memory:myLayerName")

I can access the "memory:myLayerName" for subsequent calculations.

I'd expect a similar syntax for rasters, but I can't get this (or other functions where a raster is created as an output) to work:

processing.runandload("gdalogr:cliprasterbymasklayer", path_to_raster,
                      path_to_shape, "", False, False, "", "memory:OutputRaster")

I get an "Oooops! The following output layers could not be open" – error. The processing.log file shows:

'INFO|Sun Mar 01 2015 02:20:52|GDAL execution console output|ERROR 1:
TIFFOpen:memory:temp_layer: Permission denied'

When I replace the "memory:OutputRaster" with r"C:/path/to/out/raster.tif" the code runs. What am I doing wrong?

Best Answer

I can't say exactly why the runandload method doesn't work with raster memory layers (not experienced enough) but since you only want to use the result as an intermediate calculation, an alternative can be:

int_raster = processing.runalg("gdalogr:cliprasterbymasklayer", path_to_raster, path_to_shape, "", False, False, "", None)

You can use int_raster (which is just a name, you can call it whatever you want) as the input for your next process.

Hope this helps!

Related Question