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$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
from qgis.core import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2013-10-01 20:52:22 +03: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'
|
|
|
|
self.addParameter(ParameterVector(ConvexHull.INPUT, 'Input layer',
|
|
|
|
[ParameterVector.VECTOR_TYPE_ANY]))
|
|
|
|
self.addParameter(ParameterTableField(ConvexHull.FIELD,
|
2014-06-27 08:33:07 +10:00
|
|
|
'Field (optional, only used if creating convex hulls by classes)',
|
|
|
|
ConvexHull.INPUT, optional=True))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterSelection(ConvexHull.METHOD, 'Method',
|
|
|
|
ConvexHull.METHODS))
|
|
|
|
self.addOutput(OutputVector(ConvexHull.OUTPUT, '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
|
|
|
useField = self.getParameterValue(ConvexHull.METHOD) == 1
|
2013-02-27 15:07:57 +04:00
|
|
|
fieldName = self.getParameterValue(ConvexHull.FIELD)
|
2013-10-01 20:52:22 +03:00
|
|
|
layer = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(ConvexHull.INPUT))
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
f = QgsField('value')
|
2013-02-27 15:07:57 +04:00
|
|
|
f.setType(QVariant.String)
|
|
|
|
f.setLength(255)
|
|
|
|
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
|
|
|
|
2013-10-01 20:52:22 +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
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = self.getOutputFromName(
|
|
|
|
ConvexHull.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
|
2013-10-01 20:52:22 +03:00
|
|
|
val = ''
|
2013-01-01 23:52:00 +01:00
|
|
|
if useField:
|
2013-02-27 15:07:57 +04:00
|
|
|
unique = layer.uniqueValues(index)
|
2013-10-01 20:52:22 +03:00
|
|
|
total = 100.0 / float(layer.featureCount() * len(unique))
|
2013-02-27 15:07:57 +04:00
|
|
|
|
|
|
|
for i in unique:
|
|
|
|
hull = []
|
|
|
|
first = True
|
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]
|
2013-06-03 21:25:22 +02:00
|
|
|
if unicode(idVar).strip() == unicode(i).strip:
|
2013-02-27 15:07:57 +04:00
|
|
|
if first:
|
|
|
|
val = idVar
|
|
|
|
first = False
|
|
|
|
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(
|
|
|
|
'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(
|
|
|
|
'Exception while computing convex hull')
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|