From a15d283cd6bd2d2b136639a9047e6c8314a578a0 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 1 Jul 2017 18:00:51 +1000 Subject: [PATCH] Port delaunay triangulation alg to new API --- .../plugins/processing/algs/qgis/Delaunay.py | 38 +++++++++++-------- .../algs/qgis/QGISAlgorithmProvider.py | 4 +- .../tests/testdata/qgis_algorithm_tests.yaml | 24 ++++++------ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/python/plugins/processing/algs/qgis/Delaunay.py b/python/plugins/processing/algs/qgis/Delaunay.py index 75997916765..05b3bb0676f 100644 --- a/python/plugins/processing/algs/qgis/Delaunay.py +++ b/python/plugins/processing/algs/qgis/Delaunay.py @@ -39,13 +39,14 @@ from qgis.core import (QgsField, 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 @@ class Delaunay(QgisAlgorithm): 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 @@ class Delaunay(QgisAlgorithm): 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 @@ class Delaunay(QgisAlgorithm): 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 @@ class Delaunay(QgisAlgorithm): 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 @@ class Delaunay(QgisAlgorithm): 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} diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index 8f66914e1f5..95ac4978f44 100755 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -48,6 +48,7 @@ from .Boundary import Boundary 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 .ZonalStatistics import ZonalStatistics # 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 @@ class QGISAlgorithmProvider(QgsProcessingProvider): # NearestNeighbourAnalysis(), MeanCoords(), # LinesIntersection(), UniqueValues(), PointDistance(), # ExportGeometryInfo(), - # Delaunay(), # , SinglePartsToMultiparts(), # PolygonsToLines(), LinesToPolygons(), ExtractNodes(), # ConvexHull(), FixedDistanceBuffer(), @@ -248,6 +247,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider): BoundingBox(), CheckValidity(), CreateAttributeIndex(), + Delaunay(), DeleteColumn(), DeleteHoles(), DensifyGeometries(), diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index 48db808930c..00d526ececb 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -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: