Resurrect another Python QGIS algorithm

This commit is contained in:
Nyall Dawson 2017-06-05 16:51:29 +10:00
parent a658135693
commit 379d060de4
3 changed files with 91 additions and 80 deletions

View File

@ -27,11 +27,18 @@ __revision__ = '$Format:%H$'
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 @@ class BoundingBox(QgisAlgorithm):
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 @@ class BoundingBox(QgisAlgorithm):
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 @@ class BoundingBox(QgisAlgorithm):
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}

View File

@ -145,7 +145,7 @@ from .QgisAlgorithm import QgisAlgorithm
# 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 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
# ReverseLineDirection(), SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
# BoundingBox(), Boundary(), PointOnSurface(),
# PointOnSurface(),
# OffsetLine(), PolygonCentroids(), Translate(),
# SingleSidedBuffer(), PointsAlongGeometry(),
# Aspect(), Slope(), Ruggedness(), Hillshade(),
@ -259,7 +259,8 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
# FixGeometry(), ExecuteSQL(), FindProjection(),
# TopoColor(), EliminateSelection()
# ]
algs = [Boundary()]
algs = [Boundary(),
BoundingBox()]
# if hasPlotly:
# from .VectorLayerHistogram import VectorLayerHistogram

View File

@ -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