Skip to main content
Became Hot Network Question
edited tags
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Tweeted twitter.com/StackGIS/status/1610652275432628228
added 6 characters in body; edited title
Source Link
Taras
  • 33.9k
  • 4
  • 73
  • 148

Iterate "Could not load source layer for MASK: invalid value" error when iterating over features in a polygon layer and clipclipping raster by mask layer in PyQGIS

I have just started using PyQGIS and need to loop over parcels in a vector layer to clip a raster layer.

For each parcel, I want to generate a .tif.tif file with the feature.id()feature.id() in the filename. I will also be saving the attributes in a separate .csv/.txt.csv/.txt file by the same name. Because of this, I can't run the script using the iterator in the GUI.

This is what I have come up with thus far:

raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = {
    'ALPHA_BAND' : False,
    'CROP_TO_CUTLINE' : True,
    'DATA_TYPE' : 0,
    'EXTRA' : '',
    'INPUT' : raster,
    'KEEP_RESOLUTION' : False,
    'MULTITHREADING' : False,
    'NODATA' : None,
    'OPTIONS' : '',
    'SET_RESOLUTION' : False,
    'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'X_RESOLUTION' : None,
    'Y_RESOLUTION' : None
}

request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)

This is the error I get:

Could not load source layer for MASK: invalid value
Could not load source layer for MASK: invalid value

I think the issue is that the 'MASK' parameter needs to be a QgsVectorLayer not a QgsFeature, which is what "feature" is now. How do I go about this?

Iterate over features in a polygon layer and clip raster by mask layer

I have just started using PyQGIS and need to loop over parcels in a vector layer to clip a raster layer.

For each parcel, I want to generate a .tif file with the feature.id() in the filename. I will also be saving the attributes in a separate .csv/.txt file by the same name. Because of this, I can't run the script using the iterator in the GUI.

This is what I have come up with thus far:

raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = {
    'ALPHA_BAND' : False,
    'CROP_TO_CUTLINE' : True,
    'DATA_TYPE' : 0,
    'EXTRA' : '',
    'INPUT' : raster,
    'KEEP_RESOLUTION' : False,
    'MULTITHREADING' : False,
    'NODATA' : None,
    'OPTIONS' : '',
    'SET_RESOLUTION' : False,
    'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'X_RESOLUTION' : None,
    'Y_RESOLUTION' : None
}

request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)

This is the error I get:

Could not load source layer for MASK: invalid value

I think the issue is that the 'MASK' parameter needs to be a QgsVectorLayer not a QgsFeature, which is what "feature" is now. How do I go about this?

"Could not load source layer for MASK: invalid value" error when iterating over features in polygon layer and clipping raster by mask layer in PyQGIS

I have just started using PyQGIS and need to loop over parcels in a vector layer to clip a raster layer.

For each parcel, I want to generate a .tif file with the feature.id() in the filename. I will also be saving the attributes in a separate .csv/.txt file by the same name. Because of this, I can't run the script using the iterator in the GUI.

This is what I have come up with thus far:

raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = {
    'ALPHA_BAND' : False,
    'CROP_TO_CUTLINE' : True,
    'DATA_TYPE' : 0,
    'EXTRA' : '',
    'INPUT' : raster,
    'KEEP_RESOLUTION' : False,
    'MULTITHREADING' : False,
    'NODATA' : None,
    'OPTIONS' : '',
    'SET_RESOLUTION' : False,
    'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'X_RESOLUTION' : None,
    'Y_RESOLUTION' : None
}

request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)

This is the error I get:

Could not load source layer for MASK: invalid value

I think the issue is that the 'MASK' parameter needs to be a QgsVectorLayer not a QgsFeature, which is what "feature" is now. How do I go about this?

added 55 characters in body
Source Link
Taras
  • 33.9k
  • 4
  • 73
  • 148
raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = \
{ 'ALPHA_BAND' : False, 'CROP_TO_CUTLINE' : True, \
'DATA_TYPE' : 0, 'EXTRA' : '', \
'INPUT' : raster, \
'KEEP_RESOLUTION' : False, \
'MULTITHREADING' : False, 'NODATA' : None, 'OPTIONS' : '', \
'SET_RESOLUTION' : False, 'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'), 'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'), 'X_RESOLUTION' : None,'Y_RESOLUTION' : None }


request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)
        
raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = {
    'ALPHA_BAND' : False,
    'CROP_TO_CUTLINE' : True,
    'DATA_TYPE' : 0,
    'EXTRA' : '',
    'INPUT' : raster,
    'KEEP_RESOLUTION' : False,
    'MULTITHREADING' : False,
    'NODATA' : None,
    'OPTIONS' : '',
    'SET_RESOLUTION' : False,
    'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'X_RESOLUTION' : None,
    'Y_RESOLUTION' : None
}

request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)
Could not load source layer for MASK: invalid value
Could not load source layer for MASK: invalid value
raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = \
{ 'ALPHA_BAND' : False, 'CROP_TO_CUTLINE' : True, \
'DATA_TYPE' : 0, 'EXTRA' : '', \
'INPUT' : raster, \
'KEEP_RESOLUTION' : False, \
'MULTITHREADING' : False, 'NODATA' : None, 'OPTIONS' : '', \
'SET_RESOLUTION' : False, 'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'), 'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'), 'X_RESOLUTION' : None,'Y_RESOLUTION' : None }


request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)
        
Could not load source layer for MASK: invalid value
raster = 'C:/Datasets/orthophotos/OMWRGB21VL_K14_K15/OMWRGB21_VL_K14_K15.tif'
Lbgbrprc = 'C:/Datasets/Landbouwgebruikspercelen/Landbouwgebruikspercelen_2020/Shapefile/Lbgbrprc20_SintNiklaas.shp'
Lbgbrprc = QgsVectorLayer(Lbgbrprc, 'landbouw', 'ogr')

params = {
    'ALPHA_BAND' : False,
    'CROP_TO_CUTLINE' : True,
    'DATA_TYPE' : 0,
    'EXTRA' : '',
    'INPUT' : raster,
    'KEEP_RESOLUTION' : False,
    'MULTITHREADING' : False,
    'NODATA' : None,
    'OPTIONS' : '',
    'SET_RESOLUTION' : False,
    'SOURCE_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'TARGET_CRS' : QgsCoordinateReferenceSystem('EPSG:31370'),
    'X_RESOLUTION' : None,
    'Y_RESOLUTION' : None
}

request = QgsFeatureRequest()
for feature in Lbgbrprc.getFeatures(request):
    if int(feature.id()) < 5:
        print(type(feature)) # <class 'qgis._core.QgsFeature'>
        params['MASK'] = feature
        params['OUTPUT'] = 'C:/Output/ortho_Lbgbrprc/{}.tif'.format(feature.id())
        
        processing.run("gdal:cliprasterbymasklayer", params)
Could not load source layer for MASK: invalid value
title as statement
Link
Vince
  • 20.3k
  • 16
  • 48
  • 65
Loading
added 3 characters in body; edited tags
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404
Loading
Source Link
Loading