Skip to content
Permalink
Browse files

[processing] Remove unnecessary index creation in points in polygons

...and rely instead on the data provider's spatial index.

Refs #19919
  • Loading branch information
nyalldawson committed Sep 24, 2018
1 parent 6286cf1 commit 93f83a02718342ddf95be513e9c3a6dac8f55495
Showing with 20 additions and 25 deletions.
  1. +20 −25 python/plugins/processing/algs/qgis/PointsInPolygon.py
@@ -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)
@@ -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)

0 comments on commit 93f83a0

Please sign in to comment.
You can’t perform that action at this time.