I have a buffer and need to filter a multipoint layer (QGSVectorLayer) so that I would get only features intersecting the buffer. What I've found:

a) setFilterRect()

This doesn't accept polygon arguments, just rectangles. I've found no polygon alternative. I don't want to decrease accuracy by converting my buffer to its MBR.

b) setSpatialFilter()

I must be using it wrong way, or perhaps it's a part of an old API which is no longer used. In the example it's applied directly on the layer, but while trying this, I get an error:

AttributeError: 'QgsVectorLayer' object has no attribute 'SetSpatialFilter'

c) select()

I've found that such a function exists, but my google-fu is too weak to find detailed documentation on it or whether it's suitable for my needs. This link seems to say that it's deprecated/abandoned now, though I might have misunderstood it.

There already are few similar questions, even almost duplicates. One is basically "I did this in ArcPy, is it possible in PyQGIS?", and the only answer (in comments) is "it is possible" with no suggestion how to do it. The others provide some workarounds, either through looping and calling intersects() or through QgsOverlayAnalyzer. Neither of these two seem to be a clean solution, so even though I will try the intersects() way, I don't consider it a solution unless someone proves that there's no better way.


setSpatialFilter() is from GDAL/OGR not from Pyqgis.

With PyQGIS, you have to deal with geometries. Check out the QgsGeometry class.

It has few functions to check conditions: intersects, crosses, disjoint etc.

So, to check if features in one layer intersects for example one feature in another layer, you have to get geometries of all features and check them with loop:

for feature1 in layer1:

for rectangle it will be:

for feature1 in layer1:

Then if true, save them to your result layer or set selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.