Skip to content
Permalink
Browse files

Port delaunay triangulation alg to new API

  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 75cd91b commit a15d283cd6bd2d2b136639a9047e6c8314a578a0
@@ -39,13 +39,14 @@
QgsPointXY,
QgsWkbTypes,
QgsProcessingUtils,
QgsFields)
QgsFields,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterDefinition,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)

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

from . import voronoi

@@ -65,12 +66,10 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POINT]))

self.addOutput(OutputVector(self.OUTPUT,
self.tr('Delaunay triangulation'),
datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer'), [QgsProcessingParameterDefinition.TypeVectorPoint]))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Delaunay triangulation'), type=QgsProcessingParameterDefinition.TypeVectorPolygon))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr("Delaunay triangulation"), type=QgsProcessingParameterDefinition.TypeVectorPolygon))

def name(self):
return 'delaunaytriangulation'
@@ -79,22 +78,26 @@ def displayName(self):
return self.tr('Delaunay triangulation')

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

fields = QgsFields()
fields.append(QgsField('POINTA', QVariant.Double, '', 24, 15))
fields.append(QgsField('POINTB', QVariant.Double, '', 24, 15))
fields.append(QgsField('POINTC', QVariant.Double, '', 24, 15))

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

pts = []
ptDict = {}
ptNdx = -1
c = voronoi.Context()
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 = QgsGeometry(inFeat.geometry())
if geom.isNull():
continue
@@ -125,6 +128,9 @@ def processAlgorithm(self, parameters, context, feedback):

total = 100.0 / len(triangles) if triangles else 1
for current, triangle in enumerate(triangles):
if feedback.isCanceled():
break

indices = list(triangle)
indices.append(indices[0])
polygon = []
@@ -133,7 +139,7 @@ def processAlgorithm(self, parameters, context, feedback):
for index in indices:
fid, n = ptDict[ids[index]]
request = QgsFeatureRequest().setFilterFid(fid)
inFeat = next(layer.getFeatures(request))
inFeat = next(source.getFeatures(request))
geom = QgsGeometry(inFeat.geometry())
if geom.isMultipart():
point = QgsPointXY(geom.asMultiPoint()[n])
@@ -146,7 +152,7 @@ def processAlgorithm(self, parameters, context, feedback):
feat.setAttributes(attrs)
geometry = QgsGeometry().fromPolygon([polygon])
feat.setGeometry(geometry)
writer.addFeature(feat, QgsFeatureSink.FastInsert)
sink.addFeature(feat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}
@@ -48,6 +48,7 @@
from .BoundingBox import BoundingBox
from .CheckValidity import CheckValidity
from .CreateAttributeIndex import CreateAttributeIndex
from .Delaunay import Delaunay
from .DeleteColumn import DeleteColumn
from .DeleteHoles import DeleteHoles
from .DensifyGeometries import DensifyGeometries
@@ -87,7 +88,6 @@
# from .PointDistance import PointDistance
# from .UniqueValues import UniqueValues
# from .ExportGeometryInfo import ExportGeometryInfo
# from .Delaunay import Delaunay
# from .LinesToPolygons import LinesToPolygons
# from .PolygonsToLines import PolygonsToLines
# from .SinglePartsToMultiparts import SinglePartsToMultiparts
@@ -190,7 +190,6 @@ def getAlgs(self):
# NearestNeighbourAnalysis(), MeanCoords(),
# LinesIntersection(), UniqueValues(), PointDistance(),
# ExportGeometryInfo(),
# Delaunay(),
# , SinglePartsToMultiparts(),
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
# ConvexHull(), FixedDistanceBuffer(),
@@ -248,6 +247,7 @@ def getAlgs(self):
BoundingBox(),
CheckValidity(),
CreateAttributeIndex(),
Delaunay(),
DeleteColumn(),
DeleteHoles(),
DensifyGeometries(),
@@ -1162,18 +1162,18 @@ tests:
# OUTPUT:
# name: expected/sum_line_length.gml
# type: vector
#
# - algorithm: qgis:delaunaytriangulation
# name: Delaunay triangulation (multipoint data)
# params:
# INPUT:
# name: multipoints.gml
# type: vector
# results:
# OUTPUT:
# name: expected/multipoint_delaunay.gml
# type: vector
#

- algorithm: qgis:delaunaytriangulation
name: Delaunay triangulation (multipoint data)
params:
INPUT:
name: multipoints.gml
type: vector
results:
OUTPUT:
name: expected/multipoint_delaunay.gml
type: vector

# - algorithm: qgis:idwinterpolation
# name: IDW interpolation using attribute
# params:

0 comments on commit a15d283

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