This is kind of a workaround, dont know if it is faster than creating contours for the entire raster every 0.1 m then intersect and selecting by polygon attribute:
For each feature/polygon in the vector layer:
- Clip raster by mask layer, where the mask layer is each selected polygon
- Execute contour with the clipped raster as input and by reading interval from the polygon
- Store output in a list
Then merge all outputs in the list.
vlyr = QgsProject.instance().mapLayersByName('New scratch layer')[0]
height_field = 'height'
rlyr = QgsProject.instance().mapLayersByName('DEM')[0]
contourlist = []
for f in vlyr.getFeatures():
vlyr.select(f.id())
clipped_raster = processing.run("gdal:cliprasterbymasklayer",
{'INPUT':rlyr,'MASK':QgsProcessingFeatureSourceDefinition(vlyr.source(),
selectedFeaturesOnly=True, featureLimit=-1,
geometryCheck=QgsFeatureRequest.GeometryAbortOnInvalid),'SOURCE_CRS':None,
'TARGET_CRS':None,'NODATA':None,'ALPHA_BAND':False,'CROP_TO_CUTLINE':True,
'KEEP_RESOLUTION':False,'SET_RESOLUTION':False,'X_RESOLUTION':None,'Y_RESOLUTION':None,'MULTITHREADING':False,'OPTIONS':'','DATA_TYPE':0,'EXTRA':'','OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']
temp_contour = processing.run("gdal:contour",
{'INPUT':clipped_raster,'BAND':1,'INTERVAL':[f[height_field] for f in vlyr.getSelectedFeatures()][0], 'FIELD_NAME':'ELEV',
'CREATE_3D':False,'IGNORE_NODATA':False,'NODATA':None,'OFFSET':0,
'EXTRA':'','OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']
contourlist.append(temp_contour)
vlyr.deselect(f.id())
processing.runAndLoadResults("native:mergevectorlayers", {'LAYERS':['{0}|layername={1}'.format(c, 'contour') for c in contourlist],
'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})
