I have a points shapefile and a polygon shapefile. I would like to select all the polygons which have at least one point in them.
The problem I'm running into is how long this takes. I have 1 million points and about 320,000 polygons, so using spatial query takes far too long. I've heard that I'd need to write a Python script with spatial indexing to get a feasibly quick result, but I have no idea how to approach this.
I don't mind if it's a little inaccurate if it runs significantly quicker.
What I've tried to cobble together from other stack overflow questions is:
pointProvider = self.pointLayer.dataProvider()
all_point = pointProvider.getFeatures()
delta = 0.1
for point in all_point:
searchRectangle = QgsRectangle(point.x() - delta, point.y() - delta, point.x() + delta, point.y() + delta)
candidateIDs = line_index.intesects(searchRectangle)
for candidateID in candidateIDs:
candFeature == rotateProvider.getFeatures(QgsFeatureRequest(candidateID)).next()
if candFeature.geometry().contains(point):
break
This throws up a NameError:
name 'self' is not defined
self
, you don't really need this in a script. Also take a look at Using a QGIS spatial index to speed up your code which may be helpful :)self
, just remove it. In terms of the example link I posted, you could replacetouches
withintersects
.