39
39
QgsPointXY ,
40
40
QgsWkbTypes ,
41
41
QgsProcessingUtils ,
42
- QgsFields )
42
+ QgsFields ,
43
+ QgsProcessingParameterFeatureSource ,
44
+ QgsProcessingParameterDefinition ,
45
+ QgsProcessingParameterFeatureSink ,
46
+ QgsProcessingOutputVectorLayer )
43
47
44
48
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
45
49
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
49
50
50
51
from . import voronoi
51
52
@@ -65,12 +66,10 @@ def group(self):
65
66
66
67
def __init__ (self ):
67
68
super ().__init__ ()
68
- self .addParameter (ParameterVector (self .INPUT ,
69
- self .tr ('Input layer' ), [dataobjects .TYPE_VECTOR_POINT ]))
70
69
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 ))
74
73
75
74
def name (self ):
76
75
return 'delaunaytriangulation'
@@ -79,22 +78,26 @@ def displayName(self):
79
78
return self .tr ('Delaunay triangulation' )
80
79
81
80
def processAlgorithm (self , parameters , context , feedback ):
82
- layer = QgsProcessingUtils . mapLayerFromString ( self .getParameterValue ( self .INPUT ) , context )
81
+ source = self .parameterAsSource ( parameters , self .INPUT , context )
83
82
84
83
fields = QgsFields ()
85
84
fields .append (QgsField ('POINTA' , QVariant .Double , '' , 24 , 15 ))
86
85
fields .append (QgsField ('POINTB' , QVariant .Double , '' , 24 , 15 ))
87
86
fields .append (QgsField ('POINTC' , QVariant .Double , '' , 24 , 15 ))
88
87
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 ())
90
90
91
91
pts = []
92
92
ptDict = {}
93
93
ptNdx = - 1
94
94
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
97
97
for current , inFeat in enumerate (features ):
98
+ if feedback .isCanceled ():
99
+ break
100
+
98
101
geom = QgsGeometry (inFeat .geometry ())
99
102
if geom .isNull ():
100
103
continue
@@ -125,6 +128,9 @@ def processAlgorithm(self, parameters, context, feedback):
125
128
126
129
total = 100.0 / len (triangles ) if triangles else 1
127
130
for current , triangle in enumerate (triangles ):
131
+ if feedback .isCanceled ():
132
+ break
133
+
128
134
indices = list (triangle )
129
135
indices .append (indices [0 ])
130
136
polygon = []
@@ -133,7 +139,7 @@ def processAlgorithm(self, parameters, context, feedback):
133
139
for index in indices :
134
140
fid , n = ptDict [ids [index ]]
135
141
request = QgsFeatureRequest ().setFilterFid (fid )
136
- inFeat = next (layer .getFeatures (request ))
142
+ inFeat = next (source .getFeatures (request ))
137
143
geom = QgsGeometry (inFeat .geometry ())
138
144
if geom .isMultipart ():
139
145
point = QgsPointXY (geom .asMultiPoint ()[n ])
@@ -146,7 +152,7 @@ def processAlgorithm(self, parameters, context, feedback):
146
152
feat .setAttributes (attrs )
147
153
geometry = QgsGeometry ().fromPolygon ([polygon ])
148
154
feat .setGeometry (geometry )
149
- writer .addFeature (feat , QgsFeatureSink .FastInsert )
155
+ sink .addFeature (feat , QgsFeatureSink .FastInsert )
150
156
feedback .setProgress (int (current * total ))
151
157
152
- del writer
158
+ return { self . OUTPUT : dest_id }
0 commit comments