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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__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$'
|
|
|
|
|
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 PyQt4.QtCore import QVariant
|
|
|
|
|
|
|
|
from qgis.core import QGis, QgsField, QgsFeature, QgsGeometry
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
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
|
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
class ConvexHull(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
METHOD = 'METHOD'
|
|
|
|
METHODS = ['Create single minimum convex hull',
|
|
|
|
'Create convex hulls based on field']
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Convex hull'
|
|
|
|
self.group = 'Vector geometry tools'
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
|
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
|
|
|
self.tr('Field (optional, only used if creating convex hulls by classes)'),
|
2014-11-30 11:31:36 +02:00
|
|
|
self.INPUT, optional=True))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.METHOD,
|
|
|
|
self.tr('Method'), self.METHODS))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Convex hull')))
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2013-01-12 23:36:00 +01:00
|
|
|
def processAlgorithm(self, progress):
|
2013-10-01 20:52:22 +03:00
|
|
|
layer = dataobjects.getObjectFromUri(
|
2014-11-30 11:31:36 +02:00
|
|
|
self.getParameterValue(self.INPUT))
|
|
|
|
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:
|
2013-04-13 09:40:01 +02:00
|
|
|
index = layer.fieldNameIndex(fieldName)
|
|
|
|
fType = layer.pendingFields()[index].type()
|
|
|
|
if fType == QVariant.Int:
|
|
|
|
f.setType(QVariant.Int)
|
|
|
|
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
|
|
|
|
2015-05-20 11:01:26 +03:00
|
|
|
fields = [QgsField('id', QVariant.Int, '', 20),
|
|
|
|
f,
|
|
|
|
QgsField('area', QVariant.Double, '', 20, 6),
|
|
|
|
QgsField('perim', QVariant.Double, '', 20, 6)
|
|
|
|
]
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2014-11-30 11:31:36 +02:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
|
|
|
|
fields, QGis.WKBPolygon, layer.dataProvider().crs())
|
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
|
|
|
current = 0
|
2013-01-01 23:52:00 +01:00
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
fid = 0
|
2014-11-30 11:31:36 +02:00
|
|
|
val = None
|
|
|
|
features = vector.features(layer)
|
2013-01-01 23:52:00 +01:00
|
|
|
if useField:
|
2013-02-27 15:07:57 +04:00
|
|
|
unique = layer.uniqueValues(index)
|
2014-11-30 11:31:36 +02:00
|
|
|
total = 100.0 / (len(features) * len(unique))
|
2013-02-27 15:07:57 +04:00
|
|
|
for i in unique:
|
|
|
|
first = True
|
2014-11-30 11:31:36 +02:00
|
|
|
hull = []
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(layer)
|
2013-02-27 15:07:57 +04:00
|
|
|
for f in features:
|
2013-02-28 21:43:59 +04:00
|
|
|
idVar = f[fieldName]
|
2014-11-30 11:31:36 +02:00
|
|
|
if unicode(idVar).strip() == unicode(i).strip():
|
2013-02-27 15:07:57 +04:00
|
|
|
if first:
|
|
|
|
val = idVar
|
|
|
|
first = False
|
2014-11-30 11:31:36 +02:00
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
inGeom = QgsGeometry(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
|
|
|
|
progress.setPercentage(int(current * total))
|
|
|
|
|
|
|
|
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])
|
2013-02-27 15:07:57 +04:00
|
|
|
writer.addFeature(outFeat)
|
|
|
|
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 = []
|
|
|
|
total = 100.0 / float(layer.featureCount())
|
|
|
|
features = vector.features(layer)
|
|
|
|
for f in features:
|
|
|
|
inGeom = QgsGeometry(f.geometry())
|
|
|
|
points = vector.extractPoints(inGeom)
|
|
|
|
hull.extend(points)
|
|
|
|
current += 1
|
|
|
|
progress.setPercentage(int(current * total))
|
|
|
|
|
|
|
|
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
|
|
|
try:
|
|
|
|
outGeom = tmpGeom.convexHull()
|
|
|
|
(area, perim) = vector.simpleMeasure(outGeom)
|
|
|
|
outFeat.setGeometry(outGeom)
|
|
|
|
outFeat.setAttributes([0, 'all', area, perim])
|
|
|
|
writer.addFeature(outFeat)
|
|
|
|
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
|