Skip to content

Commit 79df6b4

Browse files
committedJul 15, 2017
Add cancelation support for points in polygons algorithm
1 parent dd38c52 commit 79df6b4

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed
 

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949

5050
class PointsInPolygon(QgisAlgorithm):
51-
5251
POLYGONS = 'POLYGONS'
5352
POINTS = 'POINTS'
5453
OUTPUT = 'OUTPUT'
@@ -71,12 +70,15 @@ def initAlgorithm(self, config=None):
7170
self.addParameter(QgsProcessingParameterFeatureSource(self.POINTS,
7271
self.tr('Points'), [QgsProcessing.TypeVectorPoint]))
7372
self.addParameter(QgsProcessingParameterField(self.WEIGHT,
74-
self.tr('Weight field'), parentLayerParameterName=self.POINTS, optional=True))
73+
self.tr('Weight field'), parentLayerParameterName=self.POINTS,
74+
optional=True))
7575
self.addParameter(QgsProcessingParameterField(self.CLASSFIELD,
76-
self.tr('Class field'), parentLayerParameterName=self.POINTS, optional=True))
76+
self.tr('Class field'), parentLayerParameterName=self.POINTS,
77+
optional=True))
7778
self.addParameter(QgsProcessingParameterString(self.FIELD,
7879
self.tr('Count field name'), defaultValue='NUMPOINTS'))
79-
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))
80+
self.addParameter(
81+
QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))
8082

8183
def name(self):
8284
return 'countpointsinpolygon'
@@ -108,7 +110,8 @@ def processAlgorithm(self, parameters, context, feedback):
108110
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
109111
fields, poly_source.wkbType(), poly_source.sourceCrs())
110112

111-
spatialIndex = QgsSpatialIndex(point_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())))
113+
spatialIndex = QgsSpatialIndex(point_source.getFeatures(
114+
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())))
112115

113116
point_attribute_indices = []
114117
if weight_field_index >= 0:
@@ -119,6 +122,9 @@ def processAlgorithm(self, parameters, context, feedback):
119122
features = poly_source.getFeatures()
120123
total = 100.0 / poly_source.featureCount() if poly_source.featureCount() else 0
121124
for current, polygon_feature in enumerate(features):
125+
if feedback.isCanceled():
126+
break
127+
122128
count = 0
123129
output_feature = QgsFeature()
124130
if polygon_feature.hasGeometry():
@@ -134,6 +140,9 @@ def processAlgorithm(self, parameters, context, feedback):
134140
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs())
135141
request.setSubsetOfAttributes(point_attribute_indices)
136142
for point_feature in point_source.getFeatures(request):
143+
if feedback.isCanceled():
144+
break
145+
137146
if engine.contains(point_feature.geometry().geometry()):
138147
if weight_field_index >= 0:
139148
weight = point_feature.attributes()[weight_field_index]

0 commit comments

Comments
 (0)
Please sign in to comment.