mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Port delaunay triangulation alg to new API
This commit is contained in:
parent
75cd91b1a0
commit
a15d283cd6
@ -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}
|
||||
|
@ -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(),
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user