Skip to content

Commit

Permalink
Port voronoi polygons algorithm to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent ebd346c commit 75cd91b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -190,7 +190,7 @@ def getAlgs(self):
# NearestNeighbourAnalysis(), MeanCoords(),
# LinesIntersection(), UniqueValues(), PointDistance(),
# ExportGeometryInfo(),
# Delaunay(), VoronoiPolygons(),
# Delaunay(),
# , SinglePartsToMultiparts(),
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
# ConvexHull(), FixedDistanceBuffer(),
Expand Down Expand Up @@ -273,6 +273,7 @@ def getAlgs(self):
SpatialiteExecuteSQL(),
SymmetricalDifference(),
VectorSplit(),
VoronoiPolygons(),
ZonalStatistics()
]

Expand Down
55 changes: 33 additions & 22 deletions python/plugins/processing/algs/qgis/VoronoiPolygons.py
Expand Up @@ -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

Expand All @@ -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'
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand Down
50 changes: 25 additions & 25 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -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:
Expand Down

0 comments on commit 75cd91b

Please sign in to comment.