2012-10-27 10:59:23 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
PointsInPolygon.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2012-10-27 10:59:23 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-27 10:59:23 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-27 10:59:23 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsField,
|
|
|
|
QgsFeatureRequest,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsFeature,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterString
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class PointsInPolygonWeighted(QgisAlgorithm):
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
POLYGONS = 'POLYGONS'
|
|
|
|
POINTS = 'POINTS'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
WEIGHT = 'WEIGHT'
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector analysis tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.POLYGONS,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Polygons'), [dataobjects.TYPE_VECTOR_POLYGON]))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.POINTS,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Points'), [dataobjects.TYPE_VECTOR_POINT]))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.WEIGHT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Weight field'), self.POINTS))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterString(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Count field name'), 'NUMPOINTS'))
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Weighted count'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'countpointsinpolygonweighted'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Count points in polygon(weighted)')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
polyLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POLYGONS), context)
|
|
|
|
pointLayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.POINTS), context)
|
2012-10-27 10:59:23 +02:00
|
|
|
fieldName = self.getParameterValue(self.FIELD)
|
2016-09-22 12:09:13 +02:00
|
|
|
fieldIdx = pointLayer.fields().lookupField(self.getParameterValue(self.WEIGHT))
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2016-08-04 07:33:49 +10:00
|
|
|
fields = polyLayer.fields()
|
2014-10-23 19:25:33 +02:00
|
|
|
fields.append(QgsField(fieldName, QVariant.Int))
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
(idxCount, fieldList) = vector.findOrCreateField(polyLayer,
|
2016-08-04 07:33:49 +10:00
|
|
|
polyLayer.fields(), fieldName)
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, polyLayer.wkbType(),
|
2017-04-26 14:33:53 +10:00
|
|
|
polyLayer.crs(), context)
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2017-05-02 13:39:36 +10:00
|
|
|
spatialIndex = QgsProcessingUtils.createSpatialIndex(pointLayer, context)
|
2012-10-27 10:59:23 +02:00
|
|
|
|
|
|
|
ftPoint = QgsFeature()
|
|
|
|
outFeat = QgsFeature()
|
|
|
|
geom = QgsGeometry()
|
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(polyLayer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / polyLayer.featureCount() if polyLayer.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, ftPoly in enumerate(features):
|
2012-10-27 10:59:23 +02:00
|
|
|
geom = ftPoly.geometry()
|
2015-11-13 11:52:17 +11:00
|
|
|
engine = QgsGeometry.createGeometryEngine(geom.geometry())
|
|
|
|
engine.prepareGeometry()
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
attrs = ftPoly.attributes()
|
2012-10-27 10:59:23 +02:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
points = spatialIndex.intersects(geom.boundingBox())
|
|
|
|
if len(points) > 0:
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgressText(str(len(points)))
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(points).setSubsetOfAttributes([fieldIdx])
|
2015-11-13 11:52:17 +11:00
|
|
|
fit = pointLayer.getFeatures(request)
|
|
|
|
ftPoint = QgsFeature()
|
|
|
|
while fit.nextFeature(ftPoint):
|
2012-10-27 10:59:23 +02:00
|
|
|
tmpGeom = QgsGeometry(ftPoint.geometry())
|
2015-11-13 11:52:17 +11:00
|
|
|
if engine.contains(tmpGeom.geometry()):
|
2016-09-21 18:24:26 +02:00
|
|
|
weight = str(ftPoint.attributes()[fieldIdx])
|
2013-06-20 23:40:51 +02:00
|
|
|
try:
|
2013-06-03 21:25:22 +02:00
|
|
|
count += float(weight)
|
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
# Ignore fields with non-numeric values
|
|
|
|
pass
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2013-02-07 01:09:39 +01:00
|
|
|
outFeat.setGeometry(geom)
|
2013-02-28 21:43:59 +04:00
|
|
|
if idxCount == len(attrs):
|
2013-06-03 21:25:22 +02:00
|
|
|
attrs.append(count)
|
2013-02-04 00:14:39 +01:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
attrs[idxCount] = count
|
2013-02-28 21:43:59 +04:00
|
|
|
outFeat.setAttributes(attrs)
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2012-10-27 10:59:23 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-10-27 10:59:23 +02:00
|
|
|
|
|
|
|
del writer
|