2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ExtentFromLayer.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
2019-04-30 13:20:02 +02:00
|
|
|
from math import floor, ceil
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2018-05-15 15:48:28 +07:00
|
|
|
from qgis.core import (QgsApplication,
|
|
|
|
QgsField,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsFeature,
|
|
|
|
QgsWkbTypes,
|
2017-07-08 14:43:07 +10:00
|
|
|
QgsProcessing,
|
2018-04-27 12:52:02 +10:00
|
|
|
QgsProcessingException,
|
2017-09-03 19:42:49 +10:00
|
|
|
QgsProcessingParameterMapLayer,
|
2019-05-02 20:17:28 +02:00
|
|
|
QgsProcessingParameterDistance,
|
|
|
|
QgsProcessingParameterDefinition,
|
2017-06-06 17:26:29 +10:00
|
|
|
QgsProcessingParameterFeatureSink,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsFields)
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class ExtentFromLayer(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-08-30 14:42:19 +10:00
|
|
|
INPUT = 'INPUT'
|
2013-10-01 20:52:22 +03:00
|
|
|
BY_FEATURE = 'BY_FEATURE'
|
2019-04-30 13:20:02 +02:00
|
|
|
ROUND_TO = 'ROUND_TO'
|
2012-10-03 19:09:32 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
OUTPUT = 'OUTPUT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2018-05-15 15:48:28 +07:00
|
|
|
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmExtractLayerExtent.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("/algorithms/mAlgorithmExtractLayerExtent.svg")
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
2019-04-30 16:02:07 +02:00
|
|
|
return self.tr('polygon,vector,raster,extent,envelope,bounds,bounding,boundary,layer,round,rounded').split(',')
|
2017-03-29 09:51:13 +10:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-09-03 19:42:49 +10:00
|
|
|
return self.tr('Layer tools')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'layertools'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2012-10-03 19:09:32 +03:00
|
|
|
|
2017-07-10 16:31:14 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
2017-09-03 19:42:49 +10:00
|
|
|
self.addParameter(QgsProcessingParameterMapLayer(self.INPUT, self.tr('Input layer')))
|
2019-05-02 20:17:28 +02:00
|
|
|
|
|
|
|
round_to_parameter = QgsProcessingParameterDistance(self.ROUND_TO,
|
|
|
|
self.tr('Round values to'),
|
|
|
|
parentParameterName=self.INPUT,
|
|
|
|
minValue=0,
|
|
|
|
defaultValue=0)
|
|
|
|
round_to_parameter.setFlags(round_to_parameter.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
|
|
|
|
self.addParameter(round_to_parameter)
|
|
|
|
|
2017-07-12 07:39:43 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Extent'), type=QgsProcessing.TypeVectorPolygon))
|
2012-10-03 19:09:32 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'polygonfromlayerextent'
|
|
|
|
|
|
|
|
def displayName(self):
|
2017-09-04 08:45:07 +10:00
|
|
|
return self.tr('Extract layer extent')
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-09-03 19:42:49 +10:00
|
|
|
layer = self.parameterAsLayer(parameters, self.INPUT, context)
|
2012-10-03 19:09:32 +03:00
|
|
|
|
2019-04-30 13:20:02 +02:00
|
|
|
round_to = self.parameterAsDouble(parameters, self.ROUND_TO, context)
|
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
fields = QgsFields()
|
|
|
|
fields.append(QgsField('MINX', QVariant.Double))
|
|
|
|
fields.append(QgsField('MINY', QVariant.Double))
|
|
|
|
fields.append(QgsField('MAXX', QVariant.Double))
|
|
|
|
fields.append(QgsField('MAXY', QVariant.Double))
|
|
|
|
fields.append(QgsField('CNTX', QVariant.Double))
|
|
|
|
fields.append(QgsField('CNTY', QVariant.Double))
|
|
|
|
fields.append(QgsField('AREA', QVariant.Double))
|
|
|
|
fields.append(QgsField('PERIM', QVariant.Double))
|
|
|
|
fields.append(QgsField('HEIGHT', QVariant.Double))
|
|
|
|
fields.append(QgsField('WIDTH', QVariant.Double))
|
2012-10-03 19:09:32 +03:00
|
|
|
|
2017-06-06 17:26:29 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
2017-09-03 19:42:49 +10:00
|
|
|
fields, QgsWkbTypes.Polygon, layer.crs())
|
2018-04-27 12:52:02 +10:00
|
|
|
if sink is None:
|
|
|
|
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
|
2012-10-03 19:09:32 +03:00
|
|
|
|
2017-09-03 19:42:49 +10:00
|
|
|
try:
|
|
|
|
# may not be possible
|
|
|
|
layer.updateExtents()
|
|
|
|
except:
|
|
|
|
pass
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2017-09-03 19:42:49 +10:00
|
|
|
rect = layer.extent()
|
2019-04-30 13:20:02 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-06-06 17:26:29 +10:00
|
|
|
geometry = QgsGeometry.fromRect(rect)
|
2019-04-30 13:20:02 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
minx = rect.xMinimum()
|
|
|
|
miny = rect.yMinimum()
|
|
|
|
maxx = rect.xMaximum()
|
|
|
|
maxy = rect.yMaximum()
|
|
|
|
height = rect.height()
|
|
|
|
width = rect.width()
|
2013-10-01 20:52:22 +03:00
|
|
|
cntx = minx + width / 2.0
|
|
|
|
cnty = miny + height / 2.0
|
2012-09-15 18:25:25 +03:00
|
|
|
area = width * height
|
2013-10-01 20:52:22 +03:00
|
|
|
perim = 2 * width + 2 * height
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
feat = QgsFeature()
|
2013-02-07 01:09:39 +01:00
|
|
|
feat.setGeometry(geometry)
|
2013-10-01 20:52:22 +03:00
|
|
|
attrs = [
|
|
|
|
minx,
|
|
|
|
miny,
|
|
|
|
maxx,
|
|
|
|
maxy,
|
|
|
|
cntx,
|
|
|
|
cnty,
|
|
|
|
area,
|
|
|
|
perim,
|
|
|
|
height,
|
|
|
|
width,
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
]
|
2013-02-04 00:14:39 +01:00
|
|
|
feat.setAttributes(attrs)
|
2017-06-23 14:34:38 +10:00
|
|
|
sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-09-03 19:42:49 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|