Skip to content

Commit b67e525

Browse files
committed
[needs-docs] Add optional weight field to Points In Polygon algorithm
Instead of a separate Points In Polygon algorithm just for this extra option, add it to the original Points In Polygon algorithm as a non-default option.
1 parent 68687c1 commit b67e525

File tree

5 files changed

+43
-161
lines changed

5 files changed

+43
-161
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,7 @@ qgis:countpointsinpolygon: >
7575

7676
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.
7777

78-
qgis:countpointsinpolygonweighted: >
79-
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-
81-
An attribute is used in the points layer to assign weights to each point.
82-
83-
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.
78+
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.
8479

8580
qgis:countuniquepointsinpolygon: >
8681
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.

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
QgsProcessingParameterFeatureSink,
4040
QgsProcessingParameterFeatureSource,
4141
QgsProcessingParameterString,
42+
QgsProcessingParameterField,
4243
QgsSpatialIndex)
4344

4445
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
@@ -52,6 +53,7 @@ class PointsInPolygon(QgisAlgorithm):
5253
POINTS = 'POINTS'
5354
OUTPUT = 'OUTPUT'
5455
FIELD = 'FIELD'
56+
WEIGHT = 'WEIGHT'
5557

5658
def icon(self):
5759
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'sum_points.png'))
@@ -67,6 +69,8 @@ def initAlgorithm(self, config=None):
6769
self.tr('Polygons'), [QgsProcessing.TypeVectorPolygon]))
6870
self.addParameter(QgsProcessingParameterFeatureSource(self.POINTS,
6971
self.tr('Points'), [QgsProcessing.TypeVectorPoint]))
72+
self.addParameter(QgsProcessingParameterField(self.WEIGHT,
73+
self.tr('Weight field'), parentLayerParameterName=self.POINTS, optional=True))
7074
self.addParameter(QgsProcessingParameterString(self.FIELD,
7175
self.tr('Count field name'), defaultValue='NUMPOINTS'))
7276
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))
@@ -80,6 +84,12 @@ def displayName(self):
8084
def processAlgorithm(self, parameters, context, feedback):
8185
poly_source = self.parameterAsSource(parameters, self.POLYGONS, context)
8286
point_source = self.parameterAsSource(parameters, self.POINTS, context)
87+
88+
weight_field = self.parameterAsString(parameters, self.WEIGHT, context)
89+
weight_field_index = -1
90+
if weight_field:
91+
weight_field_index = point_source.fields().lookupField(weight_field)
92+
8393
field_name = self.parameterAsString(parameters, self.FIELD, context)
8494

8595
fields = poly_source.fields()
@@ -105,10 +115,22 @@ def processAlgorithm(self, parameters, context, feedback):
105115
count = 0
106116
points = spatialIndex.intersects(geom.boundingBox())
107117
if len(points) > 0:
108-
request = QgsFeatureRequest().setFilterFids(points).setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())
118+
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([])
109123
for point_feature in point_source.getFeatures(request):
110124
if engine.contains(point_feature.geometry().geometry()):
111-
count += 1
125+
if weight_field_index >= 0:
126+
weight = point_feature.attributes()[weight_field_index]
127+
try:
128+
count += float(weight)
129+
except:
130+
# Ignore fields with non-numeric values
131+
pass
132+
else:
133+
count += 1
112134

113135
output_feature.setGeometry(geom)
114136

python/plugins/processing/algs/qgis/PointsInPolygonWeighted.py

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787

8888
# from .ExtractByLocation import ExtractByLocation
8989
# from .PointsInPolygonUnique import PointsInPolygonUnique
90-
# from .PointsInPolygonWeighted import PointsInPolygonWeighted
9190
# from .SumLines import SumLines
9291
# from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
9392
# from .LinesIntersection import LinesIntersection
@@ -186,7 +185,7 @@ def __init__(self):
186185

187186
def getAlgs(self):
188187
# algs = [SumLines(),
189-
# PointsInPolygonWeighted(), PointsInPolygonUnique(),
188+
# PointsInPolygonUnique(),
190189
# NearestNeighbourAnalysis(), MeanCoords(),
191190
# LinesIntersection(), UniqueValues(), PointDistance(),
192191
# ExportGeometryInfo(),

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,23 +2202,23 @@ tests:
22022202
# OUTPUT:
22032203
# name: expected/count_unique_points.gml
22042204
# type: vector
2205-
#
2206-
# - algorithm: qgis:countpointsinpolygonweighted
2207-
# name: standard count points in polygon wighted
2208-
# params:
2209-
# FIELD: NUMPOINTS
2210-
# POINTS:
2211-
# name: custom/points_weighted.gml
2212-
# type: vector
2213-
# POLYGONS:
2214-
# name: polys.gml
2215-
# type: vector
2216-
# WEIGHT: id
2217-
# results:
2218-
# OUTPUT:
2219-
# name: expected/count_points_weighted.gml
2220-
# type: vector
2221-
#
2205+
2206+
- algorithm: qgis:countpointsinpolygon
2207+
name: standard count points in polygon weighted
2208+
params:
2209+
FIELD: NUMPOINTS
2210+
POINTS:
2211+
name: custom/points_weighted.gml
2212+
type: vector
2213+
POLYGONS:
2214+
name: polys.gml
2215+
type: vector
2216+
WEIGHT: id
2217+
results:
2218+
OUTPUT:
2219+
name: expected/count_points_weighted.gml
2220+
type: vector
2221+
22222222
# - algorithm: qgis:pointsalonglines
22232223
# name: standard points alog lines
22242224
# params:

0 commit comments

Comments
 (0)