Skip to content

Commit

Permalink
Partially port a trial python alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent 0c3ad14 commit bdf051a
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 308 deletions.
37 changes: 24 additions & 13 deletions python/plugins/processing/algs/qgis/Boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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}
Loading

0 comments on commit bdf051a

Please sign in to comment.