2015-06-11 21:50:55 +02:00
|
|
|
# -*- 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
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsField,
|
|
|
|
QgsFields,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-03-29 10:42:42 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsFeature,
|
|
|
|
QgsWkbTypes,
|
|
|
|
QgsFeatureRequest,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2015-09-12 09:31:58 +02:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2015-06-11 21:50:55 +02:00
|
|
|
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
|
2015-06-11 21:50:55 +02:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class OrientedMinimumBoundingBox(QgisAlgorithm):
|
2015-06-11 21:50:55 +02:00
|
|
|
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
BY_FEATURE = 'BY_FEATURE'
|
|
|
|
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector general tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-06-11 21:50:55 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-06-11 21:50:55 +02:00
|
|
|
self.addParameter(ParameterBoolean(self.BY_FEATURE,
|
2015-09-12 09:31:58 +02:00
|
|
|
self.tr('Calculate OMBB for each feature separately'), True))
|
2015-06-11 21:50:55 +02:00
|
|
|
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Oriented_MBBox'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
2015-06-11 21:50:55 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'orientedminimumboundingbox'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Oriented minimum bounding box')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
|
2015-06-11 21:50:55 +02:00
|
|
|
byFeature = self.getParameterValue(self.BY_FEATURE)
|
2015-09-12 09:31:58 +02:00
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
if byFeature and layer.geometryType() == QgsWkbTypes.PointGeometry and layer.featureCount() <= 2:
|
2015-09-12 09:31:58 +02:00
|
|
|
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
|
|
|
|
2016-12-07 16:41:56 +10: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))
|
2015-06-11 21:50:55 +02:00
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Polygon, layer.crs(), context)
|
2015-06-11 21:50:55 +02:00
|
|
|
|
|
|
|
if byFeature:
|
2017-04-25 15:57:24 +10:00
|
|
|
self.featureOmbb(layer, context, writer, feedback)
|
2015-06-11 21:50:55 +02:00
|
|
|
else:
|
2017-04-25 15:57:24 +10:00
|
|
|
self.layerOmmb(layer, context, writer, feedback)
|
2015-06-11 21:50:55 +02:00
|
|
|
|
|
|
|
del writer
|
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
def layerOmmb(self, layer, context, writer, feedback):
|
2016-12-07 16:41:56 +10:00
|
|
|
req = QgsFeatureRequest().setSubsetOfAttributes([])
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context, req)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2015-06-11 21:50:55 +02:00
|
|
|
newgeometry = QgsGeometry()
|
|
|
|
first = True
|
2016-12-07 16:41:56 +10:00
|
|
|
for current, inFeat in enumerate(features):
|
2015-06-11 21:50:55 +02:00
|
|
|
if first:
|
2016-08-01 16:25:46 +10:00
|
|
|
newgeometry = inFeat.geometry()
|
2015-06-11 21:50:55 +02:00
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
newgeometry = newgeometry.combine(inFeat.geometry())
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2015-06-11 21:50:55 +02:00
|
|
|
|
2016-12-07 16:41:56 +10:00
|
|
|
geometry, area, angle, width, height = newgeometry.orientedMinimumBoundingBox()
|
2015-06-11 21:50:55 +02:00
|
|
|
|
|
|
|
if geometry:
|
|
|
|
outFeat = QgsFeature()
|
|
|
|
|
|
|
|
outFeat.setGeometry(geometry)
|
2015-09-12 09:31:58 +02:00
|
|
|
outFeat.setAttributes([area,
|
2016-12-07 16:41:56 +10:00
|
|
|
width * 2 + height * 2,
|
2015-09-12 09:31:58 +02:00
|
|
|
angle,
|
|
|
|
width,
|
|
|
|
height])
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2015-06-11 21:50:55 +02:00
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
def featureOmbb(self, layer, context, writer, feedback):
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2015-06-11 21:50:55 +02:00
|
|
|
outFeat = QgsFeature()
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, inFeat in enumerate(features):
|
2016-12-07 16:41:56 +10:00
|
|
|
geometry, area, angle, width, height = inFeat.geometry().orientedMinimumBoundingBox()
|
2015-06-11 21:50:55 +02:00
|
|
|
if geometry:
|
|
|
|
outFeat.setGeometry(geometry)
|
2016-12-07 16:41:56 +10:00
|
|
|
attrs = inFeat.attributes()
|
|
|
|
attrs.extend([area,
|
|
|
|
width * 2 + height * 2,
|
|
|
|
angle,
|
|
|
|
width,
|
|
|
|
height])
|
|
|
|
outFeat.setAttributes(attrs)
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2015-06-11 21:50:55 +02:00
|
|
|
else:
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.pushInfo(self.tr("Can't calculate an OMBB for feature {0}.").format(inFeat.id()))
|
|
|
|
feedback.setProgress(int(current * total))
|