Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Port delaunay triangulation alg to new API
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 75cd91b commit a15d283
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
38 changes: 22 additions & 16 deletions python/plugins/processing/algs/qgis/Delaunay.py
Expand Up @@ -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

Expand All @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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 = []
Expand All @@ -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])
Expand All @@ -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}
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -190,7 +190,6 @@ def getAlgs(self):
# NearestNeighbourAnalysis(), MeanCoords(),
# LinesIntersection(), UniqueValues(), PointDistance(),
# ExportGeometryInfo(),
# Delaunay(),
# , SinglePartsToMultiparts(),
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
# ConvexHull(), FixedDistanceBuffer(),
Expand Down Expand Up @@ -248,6 +247,7 @@ def getAlgs(self):
BoundingBox(),
CheckValidity(),
CreateAttributeIndex(),
Delaunay(),
DeleteColumn(),
DeleteHoles(),
DensifyGeometries(),
Expand Down
24 changes: 12 additions & 12 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -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:
Expand Down

0 comments on commit a15d283

Please sign in to comment.