Skip to content
Permalink
Browse files

Partially port a trial python alg to new API

  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 0c3ad14 commit bdf051a03a4af0f16de70ff544cc57d92751a350
@@ -27,11 +27,16 @@

import os

from qgis.core import QgsGeometry, QgsWkbTypes, QgsProcessingUtils
from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterOutputVectorLayer,
QgsProcessingOutputVectorLayer)

from qgis.PyQt.QtGui import QIcon

from processing.algs.qgis import QgisAlgorithm
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
@@ -45,27 +50,33 @@ class Boundary(QgisAlgorithm):
INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'

def __init__(self):
super().__init__()
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT_LAYER, self.tr('Input layer')))

self.addParameter(QgsProcessingParameterOutputVectorLayer(self.OUTPUT_LAYER, self.tr('Boundary')))

self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr("Boundaries")))

# self.addParameter(ParameterVector(self.INPUT_LAYER,
# self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE,
# dataobjects.TYPE_VECTOR_POLYGON]))
# self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Boundary')))

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'convex_hull.png'))

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

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE,
dataobjects.TYPE_VECTOR_POLYGON]))
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Boundary')))

def name(self):
return 'boundary'

def displayName(self):
return self.tr('Boundary')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
layer = self.parameterAsLayer(parameters, self.INPUT_LAYER, context)

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

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), output_wkb, layer.crs(), context)
dest = self.parameterAsString(parameters, self.OUTPUT_LAYER, context)
(writer, dest_layer) = QgsProcessingUtils.createFeatureSink(dest, '', layer.fields(), output_wkb, layer.crs(), context)

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

del writer
return {self.OUTPUT_LAYER: dest_layer}

0 comments on commit bdf051a

Please sign in to comment.
You can’t perform that action at this time.