Skip to content
Permalink
Browse files

Unify processing "polygon from layer extent" algorithms

Now that the extra features of the "polygon from vector layer extent"
algorithm are covered by the new "Minimum bounding geometry" algorithm,
we can replace the previous two "polygon from vector extent" and
"polygon from raster extent" algorithms by a single "polygon
from layer extent" algorithm.
  • Loading branch information
nyalldawson committed Sep 3, 2017
1 parent 9eeecbe commit 5d504dcafb5ff8e76e4ca8f7ea99f0a4fcb532ad
@@ -402,12 +402,7 @@ qgis:polygoncentroids: >
NOTE: This algorithm is deprecated and the generic "centroids" algorithm (which works for line and multi geometry layers) should be used instead.

qgis:polygonfromlayerextent: >
This algorithm takes a vector layer and generates a new one with the minimum bounding box (rectangle with N-S orientation) that covers all the input features.

As an alternative, the output layer can contain not just a single bounding box, but one for each input feature, representing the bounding box of each of them.

qgis:polygonfromrasterextent: >
This algorithm takes a raster layer and generates a vector layer containing a feature with the minimum bounding box that covers the raster layer's extent.
This algorithm takes a map layer and generates a new vector layer with the minimum bounding box (rectangle with N-S orientation) that covers the input layer.

qgis:polygonize: >
This algorithm takes a lines layer and creates a polygon layer, with polygons generated from the lines in the input layer.
@@ -32,14 +32,12 @@

from qgis.core import (QgsField,
QgsFeatureSink,
QgsPointXY,
QgsGeometry,
QgsFeature,
QgsWkbTypes,
QgsProcessing,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterMapLayer,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterBoolean,
QgsFields)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
@@ -61,27 +59,23 @@ def tags(self):
return self.tr('extent,envelope,bounds,bounding,boundary,layer').split(',')

def group(self):
return self.tr('Vector general')
return self.tr('Layer tools')

def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterBoolean(self.BY_FEATURE,
self.tr('Calculate extent for each feature separately'), False))

self.addParameter(QgsProcessingParameterMapLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Extent'), type=QgsProcessing.TypeVectorPolygon))

def name(self):
return 'polygonfromlayerextent'

def displayName(self):
return self.tr('Polygon from vector extent')
return self.tr('Polygon from layer extent')

def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
byFeature = self.parameterAsBool(parameters, self.BY_FEATURE, context)
layer = self.parameterAsLayer(parameters, self.INPUT, context)

fields = QgsFields()
fields.append(QgsField('MINX', QVariant.Double))
@@ -96,17 +90,15 @@ def processAlgorithm(self, parameters, context, feedback):
fields.append(QgsField('WIDTH', QVariant.Double))

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

if byFeature:
self.featureExtent(source, context, sink, feedback)
else:
self.layerExtent(source, sink, feedback)
fields, QgsWkbTypes.Polygon, layer.crs())

return {self.OUTPUT: dest_id}
try:
# may not be possible
layer.updateExtents()
except:
pass

def layerExtent(self, source, sink, feedback):
rect = source.sourceExtent()
rect = layer.extent()
geometry = QgsGeometry.fromRect(rect)
minx = rect.xMinimum()
miny = rect.yMinimum()
@@ -136,43 +128,4 @@ def layerExtent(self, source, sink, feedback):
feat.setAttributes(attrs)
sink.addFeature(feat, QgsFeatureSink.FastInsert)

def featureExtent(self, source, context, sink, feedback):
features = source.getFeatures()
total = 100.0 / source.featureCount() if source.featureCount() else 0
feat = QgsFeature()
for current, f in enumerate(features):
if feedback.isCanceled():
break

rect = f.geometry().boundingBox()
minx = rect.xMinimum()
miny = rect.yMinimum()
maxx = rect.xMaximum()
maxy = rect.yMaximum()
height = rect.height()
width = rect.width()
cntx = minx + width / 2.0
cnty = miny + height / 2.0
area = width * height
perim = 2 * width + 2 * height
rect = [QgsPointXY(minx, miny), QgsPointXY(minx, maxy), QgsPointXY(maxx,
maxy), QgsPointXY(maxx, miny), QgsPointXY(minx, miny)]

geometry = QgsGeometry().fromPolygon([rect])
feat.setGeometry(geometry)
attrs = [
minx,
miny,
maxx,
maxy,
cntx,
cnty,
area,
perim,
height,
width,
]
feat.setAttributes(attrs)

sink.addFeature(feat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
return {self.OUTPUT: dest_id}

This file was deleted.

@@ -68,7 +68,6 @@
from .ExportGeometryInfo import ExportGeometryInfo
from .ExtendLines import ExtendLines
from .ExtentFromLayer import ExtentFromLayer
from .ExtentFromRasterLayer import ExtentFromRasterLayer
from .ExtractNodes import ExtractNodes
from .ExtractSpecificNodes import ExtractSpecificNodes
from .FieldPyculator import FieldsPyculator
@@ -219,7 +218,6 @@ def getAlgs(self):
ExportGeometryInfo(),
ExtendLines(),
ExtentFromLayer(),
ExtentFromRasterLayer(),
ExtractNodes(),
ExtractSpecificNodes(),
FieldsCalculator(),
@@ -3205,7 +3205,7 @@ tests:
name: expected/execute_sql.gml
type: vector

- algorithm: qgis:polygonfromrasterextent
- algorithm: qgis:polygonfromlayerextent
name: Polygon from raster extent
params:
INPUT:

0 comments on commit 5d504dc

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