48
48
49
49
50
50
class PointsInPolygon (QgisAlgorithm ):
51
-
52
51
POLYGONS = 'POLYGONS'
53
52
POINTS = 'POINTS'
54
53
OUTPUT = 'OUTPUT'
@@ -71,12 +70,15 @@ def initAlgorithm(self, config=None):
71
70
self .addParameter (QgsProcessingParameterFeatureSource (self .POINTS ,
72
71
self .tr ('Points' ), [QgsProcessing .TypeVectorPoint ]))
73
72
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 ))
75
75
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 ))
77
78
self .addParameter (QgsProcessingParameterString (self .FIELD ,
78
79
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 ))
80
82
81
83
def name (self ):
82
84
return 'countpointsinpolygon'
@@ -108,7 +110,8 @@ def processAlgorithm(self, parameters, context, feedback):
108
110
(sink , dest_id ) = self .parameterAsSink (parameters , self .OUTPUT , context ,
109
111
fields , poly_source .wkbType (), poly_source .sourceCrs ())
110
112
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 ())))
112
115
113
116
point_attribute_indices = []
114
117
if weight_field_index >= 0 :
@@ -119,6 +122,9 @@ def processAlgorithm(self, parameters, context, feedback):
119
122
features = poly_source .getFeatures ()
120
123
total = 100.0 / poly_source .featureCount () if poly_source .featureCount () else 0
121
124
for current , polygon_feature in enumerate (features ):
125
+ if feedback .isCanceled ():
126
+ break
127
+
122
128
count = 0
123
129
output_feature = QgsFeature ()
124
130
if polygon_feature .hasGeometry ():
@@ -134,6 +140,9 @@ def processAlgorithm(self, parameters, context, feedback):
134
140
request = QgsFeatureRequest ().setFilterFids (points ).setDestinationCrs (poly_source .sourceCrs ())
135
141
request .setSubsetOfAttributes (point_attribute_indices )
136
142
for point_feature in point_source .getFeatures (request ):
143
+ if feedback .isCanceled ():
144
+ break
145
+
137
146
if engine .contains (point_feature .geometry ().geometry ()):
138
147
if weight_field_index >= 0 :
139
148
weight = point_feature .attributes ()[weight_field_index ]
0 commit comments