Port a single python algorithm to QgsProcessingFeatureBasedAlgorithm

This commit is contained in:
Nyall Dawson 2017-07-17 10:49:57 +10:00
parent 7e3c435dd6
commit b9f225905a
2 changed files with 28 additions and 48 deletions

View File

@ -29,24 +29,17 @@ import os
from qgis.core import (QgsGeometry, from qgis.core import (QgsGeometry,
QgsWkbTypes, QgsWkbTypes,
QgsFeatureSink, QgsProcessingException)
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtGui import QIcon
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class BoundingBox(QgisAlgorithm): class BoundingBox(QgisFeatureBasedAlgorithm):
INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
def icon(self): def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'matrix.png')) return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'matrix.png'))
@ -57,39 +50,26 @@ class BoundingBox(QgisAlgorithm):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_LAYER, self.tr('Bounds'), QgsProcessing.TypeVectorPolygon))
def name(self): def name(self):
return 'boundingboxes' return 'boundingboxes'
def displayName(self): def displayName(self):
return self.tr('Bounding boxes') return self.tr('Bounding boxes')
def processAlgorithm(self, parameters, context, feedback): def outputName(self):
source = self.parameterAsSource(parameters, self.INPUT_LAYER, context) return self.tr('Bounds')
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context, def outputWkbType(self, inputWkb):
source.fields(), QgsWkbTypes.Polygon, source.sourceCrs()) return QgsWkbTypes.Polygon
features = source.getFeatures() def processFeature(self, feature, feedback):
total = 100.0 / source.featureCount() if source.featureCount() else 0 input_geometry = feature.geometry()
if input_geometry:
output_geometry = QgsGeometry.fromRect(input_geometry.boundingBox())
if not output_geometry:
raise QgsProcessingException(
self.tr('Error calculating bounding box'))
for current, input_feature in enumerate(features): feature.setGeometry(output_geometry)
if feedback.isCanceled():
break
output_feature = input_feature
input_geometry = input_feature.geometry()
if input_geometry:
output_geometry = QgsGeometry.fromRect(input_geometry.boundingBox())
if not output_geometry:
raise QgsProcessingException(
self.tr('Error calculating bounding box'))
output_feature.setGeometry(output_geometry) return True
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
return {self.OUTPUT_LAYER: dest_id}

View File

@ -415,66 +415,66 @@ tests:
- algorithm: qgis:boundingboxes - algorithm: qgis:boundingboxes
name: Bounding boxes for lines name: Bounding boxes for lines
params: params:
INPUT_LAYER: INPUT:
name: lines.gml name: lines.gml
type: vector type: vector
results: results:
OUTPUT_LAYER: OUTPUT:
name: expected/lines_bounds.gml name: expected/lines_bounds.gml
type: vector type: vector
- algorithm: qgis:boundingboxes - algorithm: qgis:boundingboxes
name: Bounding boxes for multilines name: Bounding boxes for multilines
params: params:
INPUT_LAYER: INPUT:
name: multilines.gml name: multilines.gml
type: vector type: vector
results: results:
OUTPUT_LAYER: OUTPUT:
name: expected/multiline_bounds.gml name: expected/multiline_bounds.gml
type: vector type: vector
- algorithm: qgis:boundingboxes - algorithm: qgis:boundingboxes
name: Bounding boxes for multipolygons name: Bounding boxes for multipolygons
params: params:
INPUT_LAYER: INPUT:
name: multipolys.gml name: multipolys.gml
type: vector type: vector
results: results:
OUTPUT_LAYER: OUTPUT:
name: expected/multipoly_bounds.gml name: expected/multipoly_bounds.gml
type: vector type: vector
- algorithm: qgis:boundingboxes - algorithm: qgis:boundingboxes
name: Bounding boxes for points name: Bounding boxes for points
params: params:
INPUT_LAYER: INPUT:
name: points.gml name: points.gml
type: vector type: vector
results: results:
OUTPUT_LAYER: OUTPUT:
name: expected/point_bounds.gml name: expected/point_bounds.gml
type: vector type: vector
- algorithm: qgis:boundingboxes - algorithm: qgis:boundingboxes
name: Bounding boxes for polygons name: Bounding boxes for polygons
params: params:
INPUT_LAYER: INPUT:
name: polys.gml name: polys.gml
type: vector type: vector
results: results:
OUTPUT_LAYER: OUTPUT:
name: expected/poly_bounds.gml name: expected/poly_bounds.gml
type: vector type: vector
- algorithm: qgis:boundingboxes - algorithm: qgis:boundingboxes
name: Bounding boxes for multipoints name: Bounding boxes for multipoints
params: params:
INPUT_LAYER: INPUT:
name: multipoints.gml name: multipoints.gml
type: vector type: vector
results: results:
OUTPUT_LAYER: OUTPUT:
name: expected/multipoint_bounds.gml name: expected/multipoint_bounds.gml
type: vector type: vector