Skip to content

Commit bdf051a

Browse files
committed
Partially port a trial python alg to new API
1 parent 0c3ad14 commit bdf051a

File tree

13 files changed

+330
-308
lines changed

13 files changed

+330
-308
lines changed

python/plugins/processing/algs/qgis/Boundary.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@
2727

2828
import os
2929

30-
from qgis.core import QgsGeometry, QgsWkbTypes, QgsProcessingUtils
30+
from qgis.core import (QgsGeometry,
31+
QgsWkbTypes,
32+
QgsProcessingUtils,
33+
QgsProcessingParameterVectorLayer,
34+
QgsProcessingParameterOutputVectorLayer,
35+
QgsProcessingOutputVectorLayer)
3136

3237
from qgis.PyQt.QtGui import QIcon
3338

34-
from processing.algs.qgis import QgisAlgorithm
39+
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
3540
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3641
from processing.core.parameters import ParameterVector
3742
from processing.core.outputs import OutputVector
@@ -45,27 +50,33 @@ class Boundary(QgisAlgorithm):
4550
INPUT_LAYER = 'INPUT_LAYER'
4651
OUTPUT_LAYER = 'OUTPUT_LAYER'
4752

53+
def __init__(self):
54+
super().__init__()
55+
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT_LAYER, self.tr('Input layer')))
56+
57+
self.addParameter(QgsProcessingParameterOutputVectorLayer(self.OUTPUT_LAYER, self.tr('Boundary')))
58+
59+
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr("Boundaries")))
60+
61+
# self.addParameter(ParameterVector(self.INPUT_LAYER,
62+
# self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE,
63+
# dataobjects.TYPE_VECTOR_POLYGON]))
64+
# self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Boundary')))
65+
4866
def icon(self):
4967
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'convex_hull.png'))
5068

5169
def group(self):
5270
return self.tr('Vector geometry tools')
5371

54-
def __init__(self):
55-
super().__init__()
56-
self.addParameter(ParameterVector(self.INPUT_LAYER,
57-
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE,
58-
dataobjects.TYPE_VECTOR_POLYGON]))
59-
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Boundary')))
60-
6172
def name(self):
6273
return 'boundary'
6374

6475
def displayName(self):
6576
return self.tr('Boundary')
6677

6778
def processAlgorithm(self, parameters, context, feedback):
68-
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
79+
layer = self.parameterAsLayer(parameters, self.INPUT_LAYER, context)
6980

7081
input_wkb = layer.wkbType()
7182
if QgsWkbTypes.geometryType(input_wkb) == QgsWkbTypes.LineGeometry:
@@ -77,8 +88,8 @@ def processAlgorithm(self, parameters, context, feedback):
7788
if QgsWkbTypes.hasM(input_wkb):
7889
output_wkb = QgsWkbTypes.addM(output_wkb)
7990

80-
writer = self.getOutputFromName(
81-
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), output_wkb, layer.crs(), context)
91+
dest = self.parameterAsString(parameters, self.OUTPUT_LAYER, context)
92+
(writer, dest_layer) = QgsProcessingUtils.createFeatureSink(dest, '', layer.fields(), output_wkb, layer.crs(), context)
8293

8394
features = QgsProcessingUtils.getFeatures(layer, context)
8495
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
@@ -97,4 +108,4 @@ def processAlgorithm(self, parameters, context, feedback):
97108
writer.addFeature(output_feature)
98109
feedback.setProgress(int(current * total))
99110

100-
del writer
111+
return {self.OUTPUT_LAYER: dest_layer}

0 commit comments

Comments
 (0)