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?

3 Answers 3


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!

  • 1
    I agree, and I would add that one could direct all intermediate results to a temporal directory and, at the end of the process, remove it manually or via the script itself. That way you can get rid of intermediate results. Mar 2, 2015 at 13:32
  • 1
    Thanks @Joseph! intermediate_raster is a dictionary and intermediate_raster["OUTPUT"] returns the path as a string to the output file saved in a temporary folder. Yes, this kind of helps since I don't have to define temporary file paths myself (which I currently do according to @Curlew's comment to this question). @gcarrillo, true. But I tried to rather use in-memory layers to avoid attempting to delete physical files which may be locked. Not sure if this is really relevant though.
    – openwater
    Mar 3, 2015 at 16:18
  • Ah well noticed, forgot that intermediate_raster was a dictionary! Will edit the post, thank you.
    – Joseph
    Mar 3, 2015 at 16:25

You can create a folder in the temporary system as follows:

import os, tempfile
tempdir7 = tempfile.TemporaryDirectory()

The path of the temporary folder with the file extension is:


code execution

processing.run("gdal:cliprasterbymasklayer", {'INPUT':DTM,'MASK':Shp_Corte,'NODATA':None,'ALPHA_BAND':False,'CROP_TO_CUTLINE':True,'KEEP_RESOLUTION':False,'OPTIONS':'','DATA_TYPE':0,'OUTPUT':rutaProv2}) 
clip_raster = QgsRasterLayer(rutaProv2, 'x'+str(i)+'.tif')

Once you finish running your code, you delete the temporary folder.

del clip_raster
del tempdir7

It is not possible to store rasters layers in memory in QGIS*.

"memory:abc" makes QGIS try to write the resulting raster file to the path memory:abc and since a : character is not allowed in Windows filenames, the error happens.

In modern QGIS you can use QgsProcessing.TEMPORARY_OUTPUT for the output parameter to make QGIS write a result to a temporary file (in C:\Temp\ or something similar).

*: Unless you configured your operating system to provide a directory mounted in RAM and tell QGIS to store files there. This is usually the case on Linux.

Your Answer

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

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