Skip to content

Commit 93f83a0

Browse files
committedSep 24, 2018
[processing] Remove unnecessary index creation in points in polygons
...and rely instead on the data provider's spatial index. Refs #19919
1 parent 6286cf1 commit 93f83a0

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed
 

‎python/plugins/processing/algs/qgis/PointsInPolygon.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ def processAlgorithm(self, parameters, context, feedback):
125125
if sink is None:
126126
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
127127

128-
spatialIndex = QgsSpatialIndex(point_source.getFeatures(
129-
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())), feedback)
130-
131128
point_attribute_indices = []
132129
if weight_field_index >= 0:
133130
point_attribute_indices.append(weight_field_index)
@@ -150,28 +147,26 @@ def processAlgorithm(self, parameters, context, feedback):
150147
count = 0
151148
classes = set()
152149

153-
points = spatialIndex.intersects(geom.boundingBox())
154-
if len(points) > 0:
155-
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())
156-
request.setSubsetOfAttributes(point_attribute_indices)
157-
for point_feature in point_source.getFeatures(request):
158-
if feedback.isCanceled():
159-
break
160-
161-
if engine.contains(point_feature.geometry().constGet()):
162-
if weight_field_index >= 0:
163-
weight = point_feature.attributes()[weight_field_index]
164-
try:
165-
count += float(weight)
166-
except:
167-
# Ignore fields with non-numeric values
168-
pass
169-
elif class_field_index >= 0:
170-
point_class = point_feature.attributes()[class_field_index]
171-
if point_class not in classes:
172-
classes.add(point_class)
173-
else:
174-
count += 1
150+
request = QgsFeatureRequest().setFilterRect(geom.boundingBox()).setDestinationCrs(poly_source.sourceCrs(), context.transformContext())
151+
request.setSubsetOfAttributes(point_attribute_indices)
152+
for point_feature in point_source.getFeatures(request):
153+
if feedback.isCanceled():
154+
break
155+
156+
if engine.contains(point_feature.geometry().constGet()):
157+
if weight_field_index >= 0:
158+
weight = point_feature.attributes()[weight_field_index]
159+
try:
160+
count += float(weight)
161+
except:
162+
# Ignore fields with non-numeric values
163+
pass
164+
elif class_field_index >= 0:
165+
point_class = point_feature.attributes()[class_field_index]
166+
if point_class not in classes:
167+
classes.add(point_class)
168+
else:
169+
count += 1
175170

176171
output_feature.setGeometry(geom)
177172

0 commit comments

Comments
 (0)