I am trying to use overlay_intersects()
inside an expression in order to loop through the features from a target layer that intersect with a feature having a certain "id"
.
request_string = "overlay_intersects(layer:='"+target_layer_name+"', filter:=id='"+matching_feature_id+"')"
features = current_layer.getFeatures(request_string)
for feat in features:
print(feat.attribute('name')+" intersects with the feature having id '"+matching_feature_id+"'")
But this doesn't loop, all features are evaluated to false in this situation.
If I use the same expression in the Field Calculator and set a field like matches
with it, this is going to work as expected: all features from the current layer that intersect with the one having id matching_feature_id
in the target layer will have their field matches
set to true
and the others to false
.
Field matches
(boolean) = overlay_intersects(layer:='target_layer_name', filter:=id='matching_feature_id')
- QGIS: 3.22.7-Białowieża
- Python: 3.9.2
target_layer_name
andmatching_feature_id
in your expression? If they are variables you have defined previously in the Python code, then you need to insert them into the expression properly e.g. with f-string formatting, otherwise they will be interpreted as string literals. The same goes for how you are using it in your print statement.