Skip to content

Commit

Permalink
[processing] Remove unnecessary index creation in points in polygons
Browse files Browse the repository at this point in the history
...and rely instead on the data provider's spatial index.

Refs #19919
  • Loading branch information
nyalldawson committed Sep 24, 2018
1 parent 6286cf1 commit 93f83a0
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions python/plugins/processing/algs/qgis/PointsInPolygon.py
Expand Up @@ -125,9 +125,6 @@ def processAlgorithm(self, parameters, context, feedback):
if sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))

spatialIndex = QgsSpatialIndex(point_source.getFeatures(
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())), feedback)

point_attribute_indices = []
if weight_field_index >= 0:
point_attribute_indices.append(weight_field_index)
Expand All @@ -150,28 +147,26 @@ def processAlgorithm(self, parameters, context, feedback):
count = 0
classes = set()

points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())
request.setSubsetOfAttributes(point_attribute_indices)
for point_feature in point_source.getFeatures(request):
if feedback.isCanceled():
break

if engine.contains(point_feature.geometry().constGet()):
if weight_field_index >= 0:
weight = point_feature.attributes()[weight_field_index]
try:
count += float(weight)
except:
# Ignore fields with non-numeric values
pass
elif class_field_index >= 0:
point_class = point_feature.attributes()[class_field_index]
if point_class not in classes:
classes.add(point_class)
else:
count += 1
request = QgsFeatureRequest().setFilterRect(geom.boundingBox()).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())
request.setSubsetOfAttributes(point_attribute_indices)
for point_feature in point_source.getFeatures(request):
if feedback.isCanceled():
break

if engine.contains(point_feature.geometry().constGet()):
if weight_field_index >= 0:
weight = point_feature.attributes()[weight_field_index]
try:
count += float(weight)
except:
# Ignore fields with non-numeric values
pass
elif class_field_index >= 0:
point_class = point_feature.attributes()[class_field_index]
if point_class not in classes:
classes.add(point_class)
else:
count += 1

output_feature.setGeometry(geom)

Expand Down

0 comments on commit 93f83a0

Please sign in to comment.