Skip to content

Commit

Permalink
Port a single python algorithm to QgsProcessingFeatureBasedAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent 7e3c435 commit b9f2259
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
52 changes: 16 additions & 36 deletions python/plugins/processing/algs/qgis/BoundingBox.py
Expand Up @@ -29,24 +29,17 @@

from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsFeatureSink,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
QgsProcessingException)


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]


class BoundingBox(QgisAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
class BoundingBox(QgisFeatureBasedAlgorithm):

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'matrix.png'))
Expand All @@ -57,39 +50,26 @@ def group(self):
def __init__(self):
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):
return 'boundingboxes'

def displayName(self):
return self.tr('Bounding boxes')

def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT_LAYER, context)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context,
source.fields(), QgsWkbTypes.Polygon, source.sourceCrs())

features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
def outputName(self):
return self.tr('Bounds')

for current, input_feature in enumerate(features):
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'))
def outputWkbType(self, inputWkb):
return QgsWkbTypes.Polygon

output_feature.setGeometry(output_geometry)
def processFeature(self, feature, feedback):
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'))

sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
feature.setGeometry(output_geometry)

return {self.OUTPUT_LAYER: dest_id}
return True
24 changes: 12 additions & 12 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -415,66 +415,66 @@ tests:
- algorithm: qgis:boundingboxes
name: Bounding boxes for lines
params:
INPUT_LAYER:
INPUT:
name: lines.gml
type: vector
results:
OUTPUT_LAYER:
OUTPUT:
name: expected/lines_bounds.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for multilines
params:
INPUT_LAYER:
INPUT:
name: multilines.gml
type: vector
results:
OUTPUT_LAYER:
OUTPUT:
name: expected/multiline_bounds.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for multipolygons
params:
INPUT_LAYER:
INPUT:
name: multipolys.gml
type: vector
results:
OUTPUT_LAYER:
OUTPUT:
name: expected/multipoly_bounds.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for points
params:
INPUT_LAYER:
INPUT:
name: points.gml
type: vector
results:
OUTPUT_LAYER:
OUTPUT:
name: expected/point_bounds.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for polygons
params:
INPUT_LAYER:
INPUT:
name: polys.gml
type: vector
results:
OUTPUT_LAYER:
OUTPUT:
name: expected/poly_bounds.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for multipoints
params:
INPUT_LAYER:
INPUT:
name: multipoints.gml
type: vector
results:
OUTPUT_LAYER:
OUTPUT:
name: expected/multipoint_bounds.gml
type: vector

Expand Down

0 comments on commit b9f2259

Please sign in to comment.