Skip to content
Permalink
Browse files

Port voronoi polygons algorithm to new API

  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent ebd346c commit 75cd91b1a03676dd57ef9d2be6f764a9c6929734
@@ -73,6 +73,7 @@
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
from .SymmetricalDifference import SymmetricalDifference
from .VectorSplit import VectorSplit
from .VoronoiPolygons import VoronoiPolygons
from .ZonalStatistics import ZonalStatistics

# from .ExtractByLocation import ExtractByLocation
@@ -87,7 +88,6 @@
# from .UniqueValues import UniqueValues
# from .ExportGeometryInfo import ExportGeometryInfo
# from .Delaunay import Delaunay
# from .VoronoiPolygons import VoronoiPolygons
# from .LinesToPolygons import LinesToPolygons
# from .PolygonsToLines import PolygonsToLines
# from .SinglePartsToMultiparts import SinglePartsToMultiparts
@@ -190,7 +190,7 @@ def getAlgs(self):
# NearestNeighbourAnalysis(), MeanCoords(),
# LinesIntersection(), UniqueValues(), PointDistance(),
# ExportGeometryInfo(),
# Delaunay(), VoronoiPolygons(),
# Delaunay(),
# , SinglePartsToMultiparts(),
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
# ConvexHull(), FixedDistanceBuffer(),
@@ -273,6 +273,7 @@ def getAlgs(self):
SpatialiteExecuteSQL(),
SymmetricalDifference(),
VectorSplit(),
VoronoiPolygons(),
ZonalStatistics()
]

@@ -30,14 +30,21 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeatureRequest, QgsFeatureSink, QgsFeature, QgsGeometry, QgsPointXY, QgsWkbTypes, QgsProcessingUtils
from qgis.core import (QgsFeatureRequest,
QgsFeatureSink,
QgsFeature,
QgsGeometry,
QgsPointXY,
QgsWkbTypes,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer,
QgsProcessingParameterNumber)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects

from . import voronoi

@@ -58,12 +65,13 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POINT]))
self.addParameter(ParameterNumber(self.BUFFER,
self.tr('Buffer region'), 0.0, 100.0, 0.0))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Voronoi polygons'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer'), [QgsProcessingParameterDefinition.TypeVectorPoint]))
self.addParameter(QgsProcessingParameterNumber(self.BUFFER, self.tr('Buffer region'), type=QgsProcessingParameterNumber.Double,
minValue=0.0, maxValue=9999999999, defaultValue=0.0))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Voronoi polygons'), type=QgsProcessingParameterDefinition.TypeVectorPolygon))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr("Voronoi polygons"), type=QgsProcessingParameterDefinition.TypeVectorPolygon))

def name(self):
return 'voronoipolygons'
@@ -72,15 +80,13 @@ def displayName(self):
return self.tr('Voronoi polygons')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)

buf = self.getParameterValue(self.BUFFER)

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), QgsWkbTypes.Polygon,
layer.crs(), context)
source = self.parameterAsSource(parameters, self.INPUT, context)
buf = self.parameterAsDouble(parameters, self.BUFFER, context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), QgsWkbTypes.Polygon, source.sourceCrs())

outFeat = QgsFeature()
extent = layer.extent()
extent = source.sourceExtent()
extraX = extent.height() * (buf / 100.0)
extraY = extent.width() * (buf / 100.0)
height = extent.height()
@@ -90,9 +96,11 @@ def processAlgorithm(self, parameters, context, feedback):
ptDict = {}
ptNdx = -1

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, inFeat in enumerate(features):
if feedback.isCanceled():
break
geom = inFeat.geometry()
point = geom.asPoint()
x = point.x() - extent.xMinimum()
@@ -122,20 +130,23 @@ def processAlgorithm(self, parameters, context, feedback):
total = 100.0 / len(c.polygons)

for (site, edges) in list(c.polygons.items()):
if feedback.isCanceled():
break

request = QgsFeatureRequest().setFilterFid(ptDict[ids[site]])
inFeat = next(layer.getFeatures(request))
inFeat = next(source.getFeatures(request))
lines = self.clip_voronoi(edges, c, width, height, extent, extraX, extraY)

geom = QgsGeometry.fromMultiPoint(lines)
geom = QgsGeometry(geom.convexHull())
outFeat.setGeometry(geom)
outFeat.setAttributes(inFeat.attributes())
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)

current += 1
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}

def clip_voronoi(self, edges, c, width, height, extent, exX, exY):
"""Clip voronoi function based on code written for Inkscape.
@@ -2300,31 +2300,31 @@ tests:
# OUTPUT:
# name: expected/polygonize.gml
# type: vector
#
# - algorithm: qgis:voronoipolygons
# name: Standard voronoi
# params:
# BUFFER: 0.0
# INPUT:
# name: points.gml
# type: vector
# results:
# OUTPUT:
# name: expected/voronoi.gml
# type: vector
#
# - algorithm: qgis:voronoipolygons
# name: Vornoi with buffer region
# params:
# BUFFER: 10.0
# INPUT:
# name: points.gml
# type: vector
# results:
# OUTPUT:
# name: expected/voronoi_buffer.gml
# type: vector
#

- algorithm: qgis:voronoipolygons
name: Standard voronoi
params:
BUFFER: 0.0
INPUT:
name: points.gml
type: vector
results:
OUTPUT:
name: expected/voronoi.gml
type: vector

- algorithm: qgis:voronoipolygons
name: Vornoi with buffer region
params:
BUFFER: 10.0
INPUT:
name: points.gml
type: vector
results:
OUTPUT:
name: expected/voronoi_buffer.gml
type: vector

# - algorithm: qgis:findprojection
# name: Find projection
# params:

0 comments on commit 75cd91b

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