Skip to content

Commit

Permalink
Add cancelation support for points in polygons algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 15, 2017
1 parent dd38c52 commit 79df6b4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions python/plugins/processing/algs/qgis/PointsInPolygon.py
Expand Up @@ -48,7 +48,6 @@


class PointsInPolygon(QgisAlgorithm):

POLYGONS = 'POLYGONS'
POINTS = 'POINTS'
OUTPUT = 'OUTPUT'
Expand All @@ -71,12 +70,15 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.POINTS,
self.tr('Points'), [QgsProcessing.TypeVectorPoint]))
self.addParameter(QgsProcessingParameterField(self.WEIGHT,
self.tr('Weight field'), parentLayerParameterName=self.POINTS, optional=True))
self.tr('Weight field'), parentLayerParameterName=self.POINTS,
optional=True))
self.addParameter(QgsProcessingParameterField(self.CLASSFIELD,
self.tr('Class field'), parentLayerParameterName=self.POINTS, optional=True))
self.tr('Class field'), parentLayerParameterName=self.POINTS,
optional=True))
self.addParameter(QgsProcessingParameterString(self.FIELD,
self.tr('Count field name'), defaultValue='NUMPOINTS'))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))
self.addParameter(
QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))

def name(self):
return 'countpointsinpolygon'
Expand Down Expand Up @@ -108,7 +110,8 @@ def processAlgorithm(self, parameters, context, feedback):
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, poly_source.wkbType(), poly_source.sourceCrs())

spatialIndex = QgsSpatialIndex(point_source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())))
spatialIndex = QgsSpatialIndex(point_source.getFeatures(
QgsFeatureRequest().setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())))

point_attribute_indices = []
if weight_field_index >= 0:
Expand All @@ -119,6 +122,9 @@ def processAlgorithm(self, parameters, context, feedback):
features = poly_source.getFeatures()
total = 100.0 / poly_source.featureCount() if poly_source.featureCount() else 0
for current, polygon_feature in enumerate(features):
if feedback.isCanceled():
break

count = 0
output_feature = QgsFeature()
if polygon_feature.hasGeometry():
Expand All @@ -134,6 +140,9 @@ def processAlgorithm(self, parameters, context, feedback):
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs())
request.setSubsetOfAttributes(point_attribute_indices)
for point_feature in point_source.getFeatures(request):
if feedback.isCanceled():
break

if engine.contains(point_feature.geometry().geometry()):
if weight_field_index >= 0:
weight = point_feature.attributes()[weight_field_index]
Expand Down

0 comments on commit 79df6b4

Please sign in to comment.