Skip to content
Permalink
Browse files

Resurrect another Python QGIS algorithm

  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent a658135 commit 379d060de44003a3be768a8158f9163d1b9f19b8
@@ -27,11 +27,18 @@

import os

from qgis.core import QgsGeometry, QgsWkbTypes, QgsProcessingUtils
from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer,
QgsProcessingParameterDefinition)


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
@@ -53,9 +60,10 @@ def group(self):

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

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_LAYER, self.tr('Bounds'), QgsProcessingParameterDefinition.TypeVectorPolygon))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr("Bounds")))

def name(self):
return 'boundingboxes'
@@ -64,15 +72,17 @@ def displayName(self):
return self.tr('Bounding boxes')

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

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), QgsWkbTypes.Polygon, layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context,
source.fields(), QgsWkbTypes.Polygon, source.sourceCrs())

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
features = source.getFeatures()
total = 100.0 / source.featureCount()

for current, input_feature in enumerate(features):
if feedback.isCanceled():
break
output_feature = input_feature
input_geometry = input_feature.geometry()
if input_geometry:
@@ -83,7 +93,7 @@ def processAlgorithm(self, parameters, context, feedback):

output_feature.setGeometry(output_geometry)

writer.addFeature(output_feature)
sink.addFeature(output_feature)
feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT_LAYER: dest_id}
@@ -145,7 +145,7 @@
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
# from .MergeLines import MergeLines
# from .BoundingBox import BoundingBox
from .BoundingBox import BoundingBox
from .Boundary import Boundary
# from .PointOnSurface import PointOnSurface
# from .OffsetLine import OffsetLine
@@ -241,7 +241,7 @@ def getAlgs(self):
# ReverseLineDirection(), SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
# BoundingBox(), Boundary(), PointOnSurface(),
# PointOnSurface(),
# OffsetLine(), PolygonCentroids(), Translate(),
# SingleSidedBuffer(), PointsAlongGeometry(),
# Aspect(), Slope(), Ruggedness(), Hillshade(),
@@ -259,7 +259,8 @@ def getAlgs(self):
# FixGeometry(), ExecuteSQL(), FindProjection(),
# TopoColor(), EliminateSelection()
# ]
algs = [Boundary()]
algs = [Boundary(),
BoundingBox()]

# if hasPlotly:
# from .VectorLayerHistogram import VectorLayerHistogram
@@ -402,71 +402,71 @@ tests:
# name: expected/multi_to_single.gml
# type: vector
#
# - algorithm: qgis:boundingboxes
# name: Bounding boxes for lines
# params:
# INPUT_LAYER:
# name: lines.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/lines_bounds.gml
# type: vector
#
# - algorithm: qgis:boundingboxes
# name: Bounding boxes for multilines
# params:
# INPUT_LAYER:
# name: multilines.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/multiline_bounds.gml
# type: vector
#
# - algorithm: qgis:boundingboxes
# name: Bounding boxes for multipolygons
# params:
# INPUT_LAYER:
# name: multipolys.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/multipoly_bounds.gml
# type: vector
#
# - algorithm: qgis:boundingboxes
# name: Bounding boxes for points
# params:
# INPUT_LAYER:
# name: points.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/point_bounds.gml
# type: vector
#
# - algorithm: qgis:boundingboxes
# name: Bounding boxes for polygons
# params:
# INPUT_LAYER:
# name: polys.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/poly_bounds.gml
# type: vector
#
# - algorithm: qgis:boundingboxes
# name: Bounding boxes for multipoints
# params:
# INPUT_LAYER:
# name: multipoints.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/multipoint_bounds.gml
# type: vector
- algorithm: qgis:boundingboxes
name: Bounding boxes for lines
params:
INPUT_LAYER:
name: lines.gml
type: vector
results:
OUTPUT_LAYER:
name: expected/lines_bounds.gml
type: vector

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

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

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

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

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

- algorithm: qgis:boundary
name: Polygon boundary

0 comments on commit 379d060

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