2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ConvexHull.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2012-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
from qgis.PyQt.QtCore import QVariant
|
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
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
from qgis.core import (QgsField,
|
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-05-02 21:15:03 +10:00
|
|
|
QgsGeometry,
|
|
|
|
QgsWkbTypes,
|
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsFields)
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
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
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class ConvexHull(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
METHOD = 'METHOD'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'convex_hull.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.methods = [self.tr('Create single minimum convex hull'),
|
|
|
|
self.tr('Create convex hulls based on field')]
|
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Field (optional, only used if creating convex hulls by classes)'),
|
|
|
|
self.INPUT, optional=True))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.METHOD,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Method'), self.methods))
|
2016-08-23 19:33:42 +03:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Convex hull'), datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'convexhull'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Convex hull')
|
|
|
|
|
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), context)
|
2014-11-30 11:31:36 +02:00
|
|
|
useField = self.getParameterValue(self.METHOD) == 1
|
|
|
|
fieldName = self.getParameterValue(self.FIELD)
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2015-05-20 11:01:26 +03:00
|
|
|
f = QgsField('value', QVariant.String, '', 255)
|
2013-02-27 15:07:57 +04:00
|
|
|
if useField:
|
2016-09-22 12:09:13 +02:00
|
|
|
index = layer.fields().lookupField(fieldName)
|
2016-08-04 07:37:22 +10:00
|
|
|
fType = layer.fields()[index].type()
|
2016-06-27 14:17:51 +03:00
|
|
|
if fType in [QVariant.Int, QVariant.UInt, QVariant.LongLong, QVariant.ULongLong]:
|
|
|
|
f.setType(fType)
|
2013-04-13 09:40:01 +02:00
|
|
|
f.setLength(20)
|
|
|
|
elif fType == QVariant.Double:
|
|
|
|
f.setType(QVariant.Double)
|
|
|
|
f.setLength(20)
|
|
|
|
f.setPrecision(6)
|
|
|
|
else:
|
|
|
|
f.setType(QVariant.String)
|
|
|
|
f.setLength(255)
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
fields = QgsFields()
|
|
|
|
fields.append(QgsField('id', QVariant.Int, '', 20))
|
|
|
|
fields.append(f)
|
|
|
|
fields.append(QgsField('area', QVariant.Double, '', 20, 6))
|
|
|
|
fields.append(QgsField('perim', QVariant.Double, '', 20, 6))
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Polygon, layer.crs(), context)
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
|
|
|
inGeom = QgsGeometry()
|
|
|
|
outGeom = QgsGeometry()
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
fid = 0
|
2014-11-30 11:31:36 +02:00
|
|
|
val = None
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2013-01-01 23:52:00 +01:00
|
|
|
if useField:
|
2013-02-27 15:07:57 +04:00
|
|
|
unique = layer.uniqueValues(index)
|
2016-02-17 09:36:59 +02:00
|
|
|
current = 0
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / (layer.featureCount() * len(unique)) if layer.featureCount() else 1
|
2013-02-27 15:07:57 +04:00
|
|
|
for i in unique:
|
|
|
|
first = True
|
2014-11-30 11:31:36 +02:00
|
|
|
hull = []
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2013-02-27 15:07:57 +04:00
|
|
|
for f in features:
|
2013-02-28 21:43:59 +04:00
|
|
|
idVar = f[fieldName]
|
2016-09-21 18:24:26 +02:00
|
|
|
if str(idVar).strip() == str(i).strip():
|
2013-02-27 15:07:57 +04:00
|
|
|
if first:
|
|
|
|
val = idVar
|
|
|
|
first = False
|
2014-11-30 11:31:36 +02:00
|
|
|
|
2016-08-01 16:25:46 +10:00
|
|
|
inGeom = f.geometry()
|
2013-10-01 20:52:22 +03:00
|
|
|
points = vector.extractPoints(inGeom)
|
2013-02-27 15:07:57 +04:00
|
|
|
hull.extend(points)
|
|
|
|
current += 1
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-02-27 15:07:57 +04:00
|
|
|
|
|
|
|
if len(hull) >= 3:
|
|
|
|
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
|
|
|
try:
|
|
|
|
outGeom = tmpGeom.convexHull()
|
2013-10-01 20:52:22 +03:00
|
|
|
(area, perim) = vector.simpleMeasure(outGeom)
|
2013-02-27 15:07:57 +04:00
|
|
|
outFeat.setGeometry(outGeom)
|
2013-10-01 20:52:22 +03:00
|
|
|
outFeat.setAttributes([fid, val, area, perim])
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2013-02-27 15:07:57 +04:00
|
|
|
except:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Exception while computing convex hull'))
|
2013-02-27 15:07:57 +04:00
|
|
|
fid += 1
|
2013-01-01 23:52:00 +01:00
|
|
|
else:
|
2013-10-01 20:52:22 +03:00
|
|
|
hull = []
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 1
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2016-08-01 16:25:46 +10:00
|
|
|
inGeom = f.geometry()
|
2013-10-01 20:52:22 +03:00
|
|
|
points = vector.extractPoints(inGeom)
|
|
|
|
hull.extend(points)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
|
|
|
try:
|
|
|
|
outGeom = tmpGeom.convexHull()
|
|
|
|
(area, perim) = vector.simpleMeasure(outGeom)
|
|
|
|
outFeat.setGeometry(outGeom)
|
|
|
|
outFeat.setAttributes([0, 'all', area, perim])
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2013-10-01 20:52:22 +03:00
|
|
|
except:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Exception while computing convex hull'))
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|