QGIS/python/plugins/processing/algs/qgis/OrientedMinimumBoundingBox.py

143 lines
5.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
***************************************************************************
OrientedMinimumBoundingBox.py
---------------------
Date : June 2015
Copyright : (C) 2015, Loïc BARTOLETTI
Email : coder at tuxfamily dot org
***************************************************************************
* *
* 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__ = 'Loïc BARTOLETTI'
__date__ = 'June 2015'
__copyright__ = '(C) 2015, Loïc BARTOLETTI'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsField,
QgsFields,
QgsFeatureSink,
QgsGeometry,
QgsFeature,
QgsWkbTypes,
QgsFeatureRequest,
QgsApplication,
QgsProcessingUtils)
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterBoolean
from processing.core.outputs import OutputVector
2017-05-02 14:47:58 +10:00
from processing.tools import dataobjects
class OrientedMinimumBoundingBox(QgisAlgorithm):
INPUT_LAYER = 'INPUT_LAYER'
BY_FEATURE = 'BY_FEATURE'
OUTPUT = 'OUTPUT'
def group(self):
return self.tr('Vector general tools')
def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))
self.addParameter(ParameterBoolean(self.BY_FEATURE,
self.tr('Calculate OMBB for each feature separately'), True))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Oriented_MBBox'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
def name(self):
return 'orientedminimumboundingbox'
def displayName(self):
return self.tr('Oriented minimum bounding box')
def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
byFeature = self.getParameterValue(self.BY_FEATURE)
if byFeature and layer.geometryType() == QgsWkbTypes.PointGeometry and layer.featureCount() <= 2:
raise GeoAlgorithmExecutionException(self.tr("Can't calculate an OMBB for each point, it's a point. The number of points must be greater than 2"))
2015-09-19 11:17:42 +02:00
if byFeature:
fields = layer.fields()
else:
fields = QgsFields()
fields.append(QgsField('area', QVariant.Double))
fields.append(QgsField('perimeter', QVariant.Double))
fields.append(QgsField('angle', QVariant.Double))
fields.append(QgsField('width', QVariant.Double))
fields.append(QgsField('height', QVariant.Double))
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Polygon, layer.crs(), context)
if byFeature:
self.featureOmbb(layer, context, writer, feedback)
else:
self.layerOmmb(layer, context, writer, feedback)
del writer
def layerOmmb(self, layer, context, writer, feedback):
req = QgsFeatureRequest().setSubsetOfAttributes([])
features = QgsProcessingUtils.getFeatures(layer, context, req)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
newgeometry = QgsGeometry()
first = True
for current, inFeat in enumerate(features):
if first:
newgeometry = inFeat.geometry()
first = False
else:
newgeometry = newgeometry.combine(inFeat.geometry())
feedback.setProgress(int(current * total))
geometry, area, angle, width, height = newgeometry.orientedMinimumBoundingBox()
if geometry:
outFeat = QgsFeature()
outFeat.setGeometry(geometry)
outFeat.setAttributes([area,
width * 2 + height * 2,
angle,
width,
height])
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
def featureOmbb(self, layer, context, writer, feedback):
features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
outFeat = QgsFeature()
for current, inFeat in enumerate(features):
geometry, area, angle, width, height = inFeat.geometry().orientedMinimumBoundingBox()
if geometry:
outFeat.setGeometry(geometry)
attrs = inFeat.attributes()
attrs.extend([area,
width * 2 + height * 2,
angle,
width,
height])
outFeat.setAttributes(attrs)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
else:
feedback.pushInfo(self.tr("Can't calculate an OMBB for feature {0}.").format(inFeat.id()))
feedback.setProgress(int(current * total))