Skip to content
Permalink
Browse files

Round values in ExtentFromLayer processing algorithm

  • Loading branch information
raymondnijssen authored and m-kuhn committed Apr 30, 2019
1 parent d174840 commit 052db03919a584820f34ac0356aa426aafc767c9
@@ -334,7 +334,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 map layer and generates a new vector layer with the minimum bounding box (rectangle with N-S orientation) that covers the input layer.
This algorithm takes a map layer and generates a new vector layer with the minimum bounding box (rectangle polygon with N-S orientation) that covers the input layer. The extent can be enlarged to a rounded value.

qgis:polygonize: >
This algorithm takes a lines layer and creates a polygon layer, with polygons generated from the lines in the input layer.
@@ -26,6 +26,7 @@
__revision__ = '$Format:%H$'

import os
from math import floor, ceil

from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant
@@ -39,6 +40,7 @@
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterMapLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink,
QgsFields)

@@ -51,6 +53,7 @@ class ExtentFromLayer(QgisAlgorithm):

INPUT = 'INPUT'
BY_FEATURE = 'BY_FEATURE'
ROUND_TO = 'ROUND_TO'

OUTPUT = 'OUTPUT'

@@ -61,7 +64,7 @@ def svgIconPath(self):
return QgsApplication.iconPath("/algorithms/mAlgorithmExtractLayerExtent.svg")

def tags(self):
return self.tr('polygon,from,vector,raster,extent,envelope,bounds,bounding,boundary,layer').split(',')
return self.tr('polygon,from,vector,raster,extent,envelope,bounds,bounding,boundary,layer,round,rounded').split(',')

def group(self):
return self.tr('Layer tools')
@@ -74,6 +77,10 @@ def __init__(self):

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterMapLayer(self.INPUT, self.tr('Input layer')))
self.addParameter(QgsProcessingParameterNumber(self.ROUND_TO,
self.tr('Round values to'), minValue=0,
defaultValue=0,
type=QgsProcessingParameterNumber.Double))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Extent'), type=QgsProcessing.TypeVectorPolygon))

def name(self):
@@ -85,6 +92,9 @@ def displayName(self):
def processAlgorithm(self, parameters, context, feedback):
layer = self.parameterAsLayer(parameters, self.INPUT, context)

round_to = self.parameterAsDouble(parameters, self.ROUND_TO, context)


fields = QgsFields()
fields.append(QgsField('MINX', QVariant.Double))
fields.append(QgsField('MINY', QVariant.Double))
@@ -109,7 +119,15 @@ def processAlgorithm(self, parameters, context, feedback):
pass

rect = layer.extent()

if round_to > 0:
rect.setXMinimum(floor(rect.xMinimum() / round_to) * round_to)
rect.setYMinimum(floor(rect.yMinimum() / round_to) * round_to)
rect.setXMaximum(ceil(rect.xMaximum() / round_to) * round_to)
rect.setYMaximum(ceil(rect.yMaximum() / round_to) * round_to)

geometry = QgsGeometry.fromRect(rect)

minx = rect.xMinimum()
miny = rect.yMinimum()
maxx = rect.xMaximum()

0 comments on commit 052db03

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