36
36
QgsFeatureSink ,
37
37
QgsGeometry ,
38
38
QgsWkbTypes ,
39
- QgsProcessingUtils ,
39
+ QgsFeatureRequest ,
40
40
QgsFields ,
41
- NULL )
41
+ NULL ,
42
+ QgsProcessingParameterFeatureSource ,
43
+ QgsProcessingParameterField ,
44
+ QgsProcessingParameterFeatureSink ,
45
+ QgsProcessing ,
46
+ QgsProcessingException )
42
47
43
48
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
44
- from processing .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
45
- from processing .core .parameters import ParameterVector
46
- from processing .core .parameters import ParameterTableField
47
- from processing .core .parameters import ParameterSelection
48
- from processing .core .outputs import OutputVector
49
49
from processing .tools import dataobjects , vector
50
50
51
51
pluginPath = os .path .split (os .path .split (os .path .dirname (__file__ ))[0 ])[0 ]
@@ -56,7 +56,6 @@ class ConvexHull(QgisAlgorithm):
56
56
INPUT = 'INPUT'
57
57
OUTPUT = 'OUTPUT'
58
58
FIELD = 'FIELD'
59
- METHOD = 'METHOD'
60
59
61
60
def icon (self ):
62
61
return QIcon (os .path .join (pluginPath , 'images' , 'ftools' , 'convex_hull.png' ))
@@ -68,17 +67,12 @@ def __init__(self):
68
67
super ().__init__ ()
69
68
70
69
def initAlgorithm (self , config = None ):
71
- self .methods = [self .tr ('Create single minimum convex hull' ),
72
- self .tr ('Create convex hulls based on field' )]
73
-
74
- self .addParameter (ParameterVector (self .INPUT ,
75
- self .tr ('Input layer' )))
76
- self .addParameter (ParameterTableField (self .FIELD ,
77
- self .tr ('Field (optional, only used if creating convex hulls by classes)' ),
78
- self .INPUT , optional = True ))
79
- self .addParameter (ParameterSelection (self .METHOD ,
80
- self .tr ('Method' ), self .methods ))
81
- self .addOutput (OutputVector (self .OUTPUT , self .tr ('Convex hull' ), datatype = [dataobjects .TYPE_VECTOR_POLYGON ]))
70
+ self .addParameter (QgsProcessingParameterFeatureSource (self .INPUT ,
71
+ self .tr ('Input layer' )))
72
+ self .addParameter (QgsProcessingParameterField (self .FIELD ,
73
+ self .tr ('Field (optional, set if creating convex hulls by classes)' ),
74
+ parentLayerParameterName = self .INPUT , optional = True ))
75
+ self .addParameter (QgsProcessingParameterFeatureSink (self .OUTPUT , self .tr ('Convex hull' ), QgsProcessing .TypeVectorPolygon ))
82
76
83
77
def name (self ):
84
78
return 'convexhull'
@@ -87,14 +81,15 @@ def displayName(self):
87
81
return self .tr ('Convex hull' )
88
82
89
83
def processAlgorithm (self , parameters , context , feedback ):
90
- layer = QgsProcessingUtils . mapLayerFromString ( self .getParameterValue ( self .INPUT ) , context )
91
- useField = self .getParameterValue ( self .METHOD ) == 1
92
- fieldName = self . getParameterValue ( self . FIELD )
84
+ source = self .parameterAsSource ( parameters , self .INPUT , context )
85
+ fieldName = self .parameterAsString ( parameters , self .FIELD , context )
86
+ useField = bool ( fieldName )
93
87
88
+ field_index = None
94
89
f = QgsField ('value' , QVariant .String , '' , 255 )
95
90
if useField :
96
- index = layer .fields ().lookupField (fieldName )
97
- fType = layer .fields ()[index ].type ()
91
+ field_index = source .fields ().lookupField (fieldName )
92
+ fType = source .fields ()[field_index ].type ()
98
93
if fType in [QVariant .Int , QVariant .UInt , QVariant .LongLong , QVariant .ULongLong ]:
99
94
f .setType (fType )
100
95
f .setLength (20 )
@@ -112,25 +107,30 @@ def processAlgorithm(self, parameters, context, feedback):
112
107
fields .append (QgsField ('area' , QVariant .Double , '' , 20 , 6 ))
113
108
fields .append (QgsField ('perim' , QVariant .Double , '' , 20 , 6 ))
114
109
115
- writer = self .getOutputFromName (self .OUTPUT ).getVectorWriter (fields , QgsWkbTypes .Polygon , layer .crs (), context )
110
+ (sink , dest_id ) = self .parameterAsSink (parameters , self .OUTPUT , context ,
111
+ fields , QgsWkbTypes .Polygon , source .sourceCrs ())
116
112
117
113
outFeat = QgsFeature ()
118
- inGeom = QgsGeometry ()
119
114
outGeom = QgsGeometry ()
120
115
121
116
fid = 0
122
117
val = None
123
- features = QgsProcessingUtils .getFeatures (layer , context )
124
118
if useField :
125
- unique = layer .uniqueValues (index )
119
+ unique = source .uniqueValues (field_index )
126
120
current = 0
127
- total = 100.0 / (layer .featureCount () * len (unique )) if layer .featureCount () else 1
121
+ total = 100.0 / (source .featureCount () * len (unique )) if source .featureCount () else 1
128
122
for i in unique :
123
+ if feedback .isCanceled ():
124
+ break
125
+
129
126
first = True
130
127
hull = []
131
- features = QgsProcessingUtils .getFeatures (layer , context )
128
+ features = source .getFeatures (QgsFeatureRequest (). setSubsetOfAttributes ([ field_index ]) )
132
129
for f in features :
133
- idVar = f [fieldName ]
130
+ if feedback .isCanceled ():
131
+ break
132
+
133
+ idVar = f .attributes ()[field_index ]
134
134
if str (idVar ).strip () == str (i ).strip ():
135
135
if first :
136
136
val = idVar
@@ -154,16 +154,19 @@ def processAlgorithm(self, parameters, context, feedback):
154
154
perim = NULL
155
155
outFeat .setGeometry (outGeom )
156
156
outFeat .setAttributes ([fid , val , area , perim ])
157
- writer .addFeature (outFeat , QgsFeatureSink .FastInsert )
157
+ sink .addFeature (outFeat , QgsFeatureSink .FastInsert )
158
158
except :
159
- raise GeoAlgorithmExecutionException (
159
+ raise QgsProcessingException (
160
160
self .tr ('Exception while computing convex hull' ))
161
161
fid += 1
162
162
else :
163
163
hull = []
164
- total = 100.0 / layer .featureCount () if layer .featureCount () else 1
165
- features = QgsProcessingUtils .getFeatures (layer , context )
164
+ total = 100.0 / source .featureCount () if source .featureCount () else 1
165
+ features = source .getFeatures (QgsFeatureRequest (). setSubsetOfAttributes ([]) )
166
166
for current , f in enumerate (features ):
167
+ if feedback .isCanceled ():
168
+ break
169
+
167
170
inGeom = f .geometry ()
168
171
points = vector .extractPoints (inGeom )
169
172
hull .extend (points )
@@ -180,9 +183,9 @@ def processAlgorithm(self, parameters, context, feedback):
180
183
perim = NULL
181
184
outFeat .setGeometry (outGeom )
182
185
outFeat .setAttributes ([0 , 'all' , area , perim ])
183
- writer .addFeature (outFeat , QgsFeatureSink .FastInsert )
186
+ sink .addFeature (outFeat , QgsFeatureSink .FastInsert )
184
187
except :
185
- raise GeoAlgorithmExecutionException (
188
+ raise QgsProcessingException (
186
189
self .tr ('Exception while computing convex hull' ))
187
190
188
- del writer
191
+ return { self . OUTPUT : dest_id }
0 commit comments