1

I am using GDAL to clip my DEM by the selected polygon in the active layer with QGIS 3.4. I am facing a problem with the memory layer. I read the questions asked before related to this topic PyQGIS Processing: "memory: not found", Loading temporary layer in processing PyQgis 3.4 However, the solutions that are proposed are not working for me. With the following code, I do not get any result as the memory layer is not created

res1 = processing.run("gdal:cliprasterbymasklayer", {
    'INPUT':demPath,
    'MASK':QgsProcessingFeatureSourceDefinition(areaPath, True),
    'NODATA':99999,
    'ALPHA_BAND':False,
    'CROP_TO_CUTLINE':True,
    'KEEP_RESOLUTION':True,
    'OPTIONS':'',
    'DATA_TYPE':6, # Float32
    'OUTPUT':'memory:test'})
QgsProject.instance().addMapLayer(res1['OUTPUT']) 

So my workaround to solve the problem is the following.

##Clip DEM
res1 = processing.run("gdal:cliprasterbymasklayer", {
    'INPUT':demPath,
    'MASK':QgsProcessingFeatureSourceDefinition(areaPath, True),
    'NODATA':99999,
    'ALPHA_BAND':False,
    'CROP_TO_CUTLINE':True,
    'KEEP_RESOLUTION':True,
    'OPTIONS':'',
    'DATA_TYPE':6, # Float32
    'OUTPUT':QgsProcessingUtils.tempFolder()+'/OUTPUT' + str(random.getrandbits(128))+'.tif'})
rlyr_demClp = QgsRasterLayer (res1['OUTPUT'], "rlyr_demClp", "gdal")

However, I would like to ask whether there is a way to use the memory layer in QGIS 3.4. I am using the same processing tool in QGIS 3.10 without any problem as follows.

# Clip DEM in QGIS 3.10 
res3 = processing.run("gdal:cliprasterbymasklayer", {
    'INPUT': rlyr_dem,
    'MASK': vlyr_ezgStats,
    'NODATA': 99999,
    'CROP_TO_CUTLINE': True,
    'DATA_TYPE': 6,  # Float32
    'OUTPUT': 'TEMPORARY_OUTPUT'})

Your Answer

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

Browse other questions tagged or ask your own question.