Skip to content

Commit dd38c52

Browse files
committedJul 15, 2017
[needs-docs] Copy unique class field option from Points in Polygons Unique
to Points in Polygons Again, it doesn't make sense for a whole separate algorithm just to add a single option
1 parent b67e525 commit dd38c52

File tree

5 files changed

+45
-158
lines changed

5 files changed

+45
-158
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ qgis:countpointsinpolygon: >
7777

7878
An optional weight field can be used to assign weights to each point. If set, the count generated will be the sum of the weight field for each point contained by the polygon.
7979

80-
qgis:countuniquepointsinpolygon: >
81-
This algorithm takes a points layer and a polygon layer and counts the number of points from the first one in each polygon of the second one.
80+
Alternatively, a unique class field can be specified. If set, points are classified based on the selected attribute, and if several points with the same attribute value are within the polygon, only one of them is counted. The final count of the point in a polygon is, therefore, the count of different classes that are found in it.
8281

83-
Points are classified based on an attribute, and if several points with the same attribute value are within the extent of the polygon, only one of them is counted. The final count of point in a polygon is, therefore, the count of different classes that are found in it.
84-
85-
A new polygons layer is generated, with the exact same content as the input polygons layer, but containing an additional field with the points count corresponding to each polygon.
82+
Both the weight field and unique class field cannot be specified. If they are, the weight field will take precedence and the unique class field will be ignored.
8683

8784
qgis:createattributeindex: >
8885
Creates an index to speed up queries made against a field in a table. Support for index creation is dependent on the layer's data provider and the field type.

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class PointsInPolygon(QgisAlgorithm):
5454
OUTPUT = 'OUTPUT'
5555
FIELD = 'FIELD'
5656
WEIGHT = 'WEIGHT'
57+
CLASSFIELD = 'CLASSFIELD'
5758

5859
def icon(self):
5960
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'sum_points.png'))
@@ -71,6 +72,8 @@ def initAlgorithm(self, config=None):
7172
self.tr('Points'), [QgsProcessing.TypeVectorPoint]))
7273
self.addParameter(QgsProcessingParameterField(self.WEIGHT,
7374
self.tr('Weight field'), parentLayerParameterName=self.POINTS, optional=True))
75+
self.addParameter(QgsProcessingParameterField(self.CLASSFIELD,
76+
self.tr('Class field'), parentLayerParameterName=self.POINTS, optional=True))
7477
self.addParameter(QgsProcessingParameterString(self.FIELD,
7578
self.tr('Count field name'), defaultValue='NUMPOINTS'))
7679
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))
@@ -90,6 +93,11 @@ def processAlgorithm(self, parameters, context, feedback):
9093
if weight_field:
9194
weight_field_index = point_source.fields().lookupField(weight_field)
9295

96+
class_field = self.parameterAsString(parameters, self.CLASSFIELD, context)
97+
class_field_index = -1
98+
if class_field:
99+
class_field_index = point_source.fields().lookupField(class_field)
100+
93101
field_name = self.parameterAsString(parameters, self.FIELD, context)
94102

95103
fields = poly_source.fields()
@@ -102,6 +110,12 @@ def processAlgorithm(self, parameters, context, feedback):
102110

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

113+
point_attribute_indices = []
114+
if weight_field_index >= 0:
115+
point_attribute_indices.append(weight_field_index)
116+
if class_field_index >= 0:
117+
point_attribute_indices.append(class_field_index)
118+
105119
features = poly_source.getFeatures()
106120
total = 100.0 / poly_source.featureCount() if poly_source.featureCount() else 0
107121
for current, polygon_feature in enumerate(features):
@@ -113,13 +127,12 @@ def processAlgorithm(self, parameters, context, feedback):
113127
engine.prepareGeometry()
114128

115129
count = 0
130+
classes = set()
131+
116132
points = spatialIndex.intersects(geom.boundingBox())
117133
if len(points) > 0:
118134
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs())
119-
if weight_field_index >= 0:
120-
request.setSubsetOfAttributes([weight_field_index])
121-
else:
122-
request.setSubsetOfAttributes([])
135+
request.setSubsetOfAttributes(point_attribute_indices)
123136
for point_feature in point_source.getFeatures(request):
124137
if engine.contains(point_feature.geometry().geometry()):
125138
if weight_field_index >= 0:
@@ -129,17 +142,25 @@ def processAlgorithm(self, parameters, context, feedback):
129142
except:
130143
# Ignore fields with non-numeric values
131144
pass
145+
elif class_field_index >= 0:
146+
point_class = point_feature.attributes()[class_field_index]
147+
if point_class not in classes:
148+
classes.add(point_class)
132149
else:
133150
count += 1
134151

135152
output_feature.setGeometry(geom)
136153

137154
attrs = polygon_feature.attributes()
138155

156+
if class_field_index >= 0:
157+
score = len(classes)
158+
else:
159+
score = count
139160
if field_index == len(attrs):
140-
attrs.append(count)
161+
attrs.append(score)
141162
else:
142-
attrs[field_index] = count
163+
attrs[field_index] = score
143164
output_feature.setAttributes(attrs)
144165
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
145166

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

Lines changed: 0 additions & 129 deletions
This file was deleted.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
from .ZonalStatistics import ZonalStatistics
8787

8888
# from .ExtractByLocation import ExtractByLocation
89-
# from .PointsInPolygonUnique import PointsInPolygonUnique
9089
# from .SumLines import SumLines
9190
# from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
9291
# from .LinesIntersection import LinesIntersection
@@ -185,7 +184,6 @@ def __init__(self):
185184

186185
def getAlgs(self):
187186
# algs = [SumLines(),
188-
# PointsInPolygonUnique(),
189187
# NearestNeighbourAnalysis(), MeanCoords(),
190188
# LinesIntersection(), UniqueValues(), PointDistance(),
191189
# ExportGeometryInfo(),

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,22 +2186,22 @@ tests:
21862186
# name: points.gml
21872187
# type: vector
21882188
# results: {}
2189-
#
2190-
# - algorithm: qgis:countuniquepointsinpolygon
2191-
# name: standard count unique points in polygon
2192-
# params:
2193-
# CLASSFIELD: id2
2194-
# FIELD: NUMPOINTS
2195-
# POINTS:
2196-
# name: points.gml
2197-
# type: vector
2198-
# POLYGONS:
2199-
# name: polys.gml
2200-
# type: vector
2201-
# results:
2202-
# OUTPUT:
2203-
# name: expected/count_unique_points.gml
2204-
# type: vector
2189+
2190+
- algorithm: qgis:countpointsinpolygon
2191+
name: standard count unique points in polygon
2192+
params:
2193+
CLASSFIELD: id2
2194+
FIELD: NUMPOINTS
2195+
POINTS:
2196+
name: points.gml
2197+
type: vector
2198+
POLYGONS:
2199+
name: polys.gml
2200+
type: vector
2201+
results:
2202+
OUTPUT:
2203+
name: expected/count_unique_points.gml
2204+
type: vector
22052205

22062206
- algorithm: qgis:countpointsinpolygon
22072207
name: standard count points in polygon weighted

0 commit comments

Comments
 (0)
Please sign in to comment.