Skip to content

Commit a15d283

Browse files
committedJul 6, 2017
Port delaunay triangulation alg to new API
1 parent 75cd91b commit a15d283

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed
 

‎python/plugins/processing/algs/qgis/Delaunay.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
QgsPointXY,
4040
QgsWkbTypes,
4141
QgsProcessingUtils,
42-
QgsFields)
42+
QgsFields,
43+
QgsProcessingParameterFeatureSource,
44+
QgsProcessingParameterDefinition,
45+
QgsProcessingParameterFeatureSink,
46+
QgsProcessingOutputVectorLayer)
4347

4448
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
4549
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
46-
from processing.core.parameters import ParameterVector
47-
from processing.core.outputs import OutputVector
48-
from processing.tools import dataobjects
4950

5051
from . import voronoi
5152

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

6667
def __init__(self):
6768
super().__init__()
68-
self.addParameter(ParameterVector(self.INPUT,
69-
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POINT]))
7069

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

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

8180
def processAlgorithm(self, parameters, context, feedback):
82-
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
81+
source = self.parameterAsSource(parameters, self.INPUT, context)
8382

8483
fields = QgsFields()
8584
fields.append(QgsField('POINTA', QVariant.Double, '', 24, 15))
8685
fields.append(QgsField('POINTB', QVariant.Double, '', 24, 15))
8786
fields.append(QgsField('POINTC', QVariant.Double, '', 24, 15))
8887

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

9191
pts = []
9292
ptDict = {}
9393
ptNdx = -1
9494
c = voronoi.Context()
95-
features = QgsProcessingUtils.getFeatures(layer, context)
96-
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
95+
features = source.getFeatures()
96+
total = 100.0 / source.featureCount() if source.featureCount() else 0
9797
for current, inFeat in enumerate(features):
98+
if feedback.isCanceled():
99+
break
100+
98101
geom = QgsGeometry(inFeat.geometry())
99102
if geom.isNull():
100103
continue
@@ -125,6 +128,9 @@ def processAlgorithm(self, parameters, context, feedback):
125128

126129
total = 100.0 / len(triangles) if triangles else 1
127130
for current, triangle in enumerate(triangles):
131+
if feedback.isCanceled():
132+
break
133+
128134
indices = list(triangle)
129135
indices.append(indices[0])
130136
polygon = []
@@ -133,7 +139,7 @@ def processAlgorithm(self, parameters, context, feedback):
133139
for index in indices:
134140
fid, n = ptDict[ids[index]]
135141
request = QgsFeatureRequest().setFilterFid(fid)
136-
inFeat = next(layer.getFeatures(request))
142+
inFeat = next(source.getFeatures(request))
137143
geom = QgsGeometry(inFeat.geometry())
138144
if geom.isMultipart():
139145
point = QgsPointXY(geom.asMultiPoint()[n])
@@ -146,7 +152,7 @@ def processAlgorithm(self, parameters, context, feedback):
146152
feat.setAttributes(attrs)
147153
geometry = QgsGeometry().fromPolygon([polygon])
148154
feat.setGeometry(geometry)
149-
writer.addFeature(feat, QgsFeatureSink.FastInsert)
155+
sink.addFeature(feat, QgsFeatureSink.FastInsert)
150156
feedback.setProgress(int(current * total))
151157

152-
del writer
158+
return {self.OUTPUT: dest_id}

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from .BoundingBox import BoundingBox
4949
from .CheckValidity import CheckValidity
5050
from .CreateAttributeIndex import CreateAttributeIndex
51+
from .Delaunay import Delaunay
5152
from .DeleteColumn import DeleteColumn
5253
from .DeleteHoles import DeleteHoles
5354
from .DensifyGeometries import DensifyGeometries
@@ -87,7 +88,6 @@
8788
# from .PointDistance import PointDistance
8889
# from .UniqueValues import UniqueValues
8990
# from .ExportGeometryInfo import ExportGeometryInfo
90-
# from .Delaunay import Delaunay
9191
# from .LinesToPolygons import LinesToPolygons
9292
# from .PolygonsToLines import PolygonsToLines
9393
# from .SinglePartsToMultiparts import SinglePartsToMultiparts
@@ -190,7 +190,6 @@ def getAlgs(self):
190190
# NearestNeighbourAnalysis(), MeanCoords(),
191191
# LinesIntersection(), UniqueValues(), PointDistance(),
192192
# ExportGeometryInfo(),
193-
# Delaunay(),
194193
# , SinglePartsToMultiparts(),
195194
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
196195
# ConvexHull(), FixedDistanceBuffer(),
@@ -248,6 +247,7 @@ def getAlgs(self):
248247
BoundingBox(),
249248
CheckValidity(),
250249
CreateAttributeIndex(),
250+
Delaunay(),
251251
DeleteColumn(),
252252
DeleteHoles(),
253253
DensifyGeometries(),

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,18 +1162,18 @@ tests:
11621162
# OUTPUT:
11631163
# name: expected/sum_line_length.gml
11641164
# type: vector
1165-
#
1166-
# - algorithm: qgis:delaunaytriangulation
1167-
# name: Delaunay triangulation (multipoint data)
1168-
# params:
1169-
# INPUT:
1170-
# name: multipoints.gml
1171-
# type: vector
1172-
# results:
1173-
# OUTPUT:
1174-
# name: expected/multipoint_delaunay.gml
1175-
# type: vector
1176-
#
1165+
1166+
- algorithm: qgis:delaunaytriangulation
1167+
name: Delaunay triangulation (multipoint data)
1168+
params:
1169+
INPUT:
1170+
name: multipoints.gml
1171+
type: vector
1172+
results:
1173+
OUTPUT:
1174+
name: expected/multipoint_delaunay.gml
1175+
type: vector
1176+
11771177
# - algorithm: qgis:idwinterpolation
11781178
# name: IDW interpolation using attribute
11791179
# params:

0 commit comments

Comments
 (0)
Please sign in to comment.