Skip to content
Permalink
Browse files

[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.
  • Loading branch information
nyalldawson committed Jul 15, 2017
1 parent 68687c1 commit b67e525f295394826a2af652505744e64515d63e
@@ -75,12 +75,7 @@ qgis:countpointsinpolygon: >

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.

qgis:countpointsinpolygonweighted: >
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.

An attribute is used in the points layer to assign weights to each point.

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

qgis:countuniquepointsinpolygon: >
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.
@@ -39,6 +39,7 @@
QgsProcessingParameterFeatureSink,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterField,
QgsSpatialIndex)

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

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'sum_points.png'))
@@ -67,6 +69,8 @@ def initAlgorithm(self, config=None):
self.tr('Polygons'), [QgsProcessing.TypeVectorPolygon]))
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.addParameter(QgsProcessingParameterString(self.FIELD,
self.tr('Count field name'), defaultValue='NUMPOINTS'))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Count'), QgsProcessing.TypeVectorPolygon))
@@ -80,6 +84,12 @@ def displayName(self):
def processAlgorithm(self, parameters, context, feedback):
poly_source = self.parameterAsSource(parameters, self.POLYGONS, context)
point_source = self.parameterAsSource(parameters, self.POINTS, context)

weight_field = self.parameterAsString(parameters, self.WEIGHT, context)
weight_field_index = -1
if weight_field:
weight_field_index = point_source.fields().lookupField(weight_field)

field_name = self.parameterAsString(parameters, self.FIELD, context)

fields = poly_source.fields()
@@ -105,10 +115,22 @@ def processAlgorithm(self, parameters, context, feedback):
count = 0
points = spatialIndex.intersects(geom.boundingBox())
if len(points) > 0:
request = QgsFeatureRequest().setFilterFids(points).setSubsetOfAttributes([]).setDestinationCrs(poly_source.sourceCrs())
request = QgsFeatureRequest().setFilterFids(points).setDestinationCrs(poly_source.sourceCrs())
if weight_field_index >= 0:
request.setSubsetOfAttributes([weight_field_index])
else:
request.setSubsetOfAttributes([])
for point_feature in point_source.getFeatures(request):
if engine.contains(point_feature.geometry().geometry()):
count += 1
if weight_field_index >= 0:
weight = point_feature.attributes()[weight_field_index]
try:
count += float(weight)
except:
# Ignore fields with non-numeric values
pass
else:
count += 1

output_feature.setGeometry(geom)

This file was deleted.

@@ -87,7 +87,6 @@

# from .ExtractByLocation import ExtractByLocation
# from .PointsInPolygonUnique import PointsInPolygonUnique
# from .PointsInPolygonWeighted import PointsInPolygonWeighted
# from .SumLines import SumLines
# from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
# from .LinesIntersection import LinesIntersection
@@ -186,7 +185,7 @@ def __init__(self):

def getAlgs(self):
# algs = [SumLines(),
# PointsInPolygonWeighted(), PointsInPolygonUnique(),
# PointsInPolygonUnique(),
# NearestNeighbourAnalysis(), MeanCoords(),
# LinesIntersection(), UniqueValues(), PointDistance(),
# ExportGeometryInfo(),
@@ -2202,23 +2202,23 @@ tests:
# OUTPUT:
# name: expected/count_unique_points.gml
# type: vector
#
# - algorithm: qgis:countpointsinpolygonweighted
# name: standard count points in polygon wighted
# params:
# FIELD: NUMPOINTS
# POINTS:
# name: custom/points_weighted.gml
# type: vector
# POLYGONS:
# name: polys.gml
# type: vector
# WEIGHT: id
# results:
# OUTPUT:
# name: expected/count_points_weighted.gml
# type: vector
#

- algorithm: qgis:countpointsinpolygon
name: standard count points in polygon weighted
params:
FIELD: NUMPOINTS
POINTS:
name: custom/points_weighted.gml
type: vector
POLYGONS:
name: polys.gml
type: vector
WEIGHT: id
results:
OUTPUT:
name: expected/count_points_weighted.gml
type: vector

# - algorithm: qgis:pointsalonglines
# name: standard points alog lines
# params:

0 comments on commit b67e525

Please sign in to comment.
You can’t perform that action at this time.