I don't think this is possible as the QgsFeatureRequest() class must use a specified bounding box if you want to add a spatial filter. An example method isQgsFeatureRequest().setFilterRect(boundingBox)
. The ExactIntersect
flag instead uses the actual geometry of the feature for intersection, but it will be used against the bounding box.
The following is an example of using QgsFeatureRequest()
where the geometry of each line is intersected with the bounding box of each polygon:
polygon_lyr = QgsProject.instance().mapLayersByName('polygon example')[0]
line_lyr = QgsProject.instance().mapLayersByName('line example')[0]
ids = []
for i, poly in enumerate(polygon_lyr.getFeatures()):
poly_geom = poly.geometry()
request = QgsFeatureRequest().setFilterRect(poly_geom.boundingBox()).setFlags(QgsFeatureRequest.ExactIntersect)
for line in line_lyr.getFeatures(request):
ids.append(line.id())
line_lyr.selectByIds(ids)
If you want to do an intersection using the line geometries against the polygon geometries, you could do this more directly using:
polygon_lyr = QgsProject.instance().mapLayersByName('polygon example')[0]
line_lyr = QgsProject.instance().mapLayersByName('line example')[0]
ids = []
for i, poly in enumerate(polygon_lyr.getFeatures()):
poly_geom = poly.geometry()
for line in line_lyr.getFeatures():
if line.geometry().intersects(poly_geom):
ids.append(line.id())
line_lyr.selectByIds(ids)