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?