I'm trying to add the intersected geometries of 2 layers in PyQGIS.
I have a grid made up by cells (polygons) and a line layer. I want to select the features of the grid layer where they intersect the line features (so basically the Select by Location
tool used within a Python script).
The code (that works) is the following:
# get the layers from the interface
grid = iface.activeLayer()
line = iface.activeLayer()
for i in grid.getFeatures():
for l in line.getFeatures():
if i.geometry().intersects(l.geometry()):
print i
It returns (correctly) 17 features.
What I'm not able to to is to add these QgsFeatures
as a selection to the grid layer.
iface.activeLayer()
to get both input layers, so they are identical? Therefore, if you have 17 features to test for an intersection, with the same 17 features, you get 17 self-intersections.