Skip to content

Commit

Permalink
Round values in ExtentFromLayer processing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondnijssen authored and m-kuhn committed May 11, 2019
1 parent d174840 commit 052db03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/help/qgis.yaml
Expand Up @@ -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.
Expand Down
20 changes: 19 additions & 1 deletion python/plugins/processing/algs/qgis/ExtentFromLayer.py
Expand Up @@ -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
Expand All @@ -39,6 +40,7 @@
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterMapLayer,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSink,
QgsFields)

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

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

OUTPUT = 'OUTPUT'

Expand All @@ -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')
Expand All @@ -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):
Expand All @@ -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))
Expand All @@ -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()
Expand Down

0 comments on commit 052db03

Please sign in to comment.