174 lines
6.7 KiB
Python
Raw Normal View History

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'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
from PyQt4.QtCore import *
from PyQt4.QtGui import *
2012-09-15 18:25:25 +03:00
from qgis.core import *
from sextante.core.GeoAlgorithm import GeoAlgorithm
2012-09-15 18:25:25 +03:00
from sextante.core.QGisLayers import QGisLayers
from sextante.core.SextanteLog import SextanteLog
from sextante.parameters.ParameterVector import ParameterVector
2012-09-15 18:25:25 +03:00
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.outputs.OutputVector import OutputVector
from sextante.algs.ftools import FToolsUtils as utils
2012-09-15 18:25:25 +03:00
class ConvexHull(GeoAlgorithm):
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
#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/convex_hull.png")
#===========================================================================
2012-09-15 18:25:25 +03:00
def defineCharacteristics(self):
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, "Field", ConvexHull.INPUT))
self.addParameter(ParameterSelection(ConvexHull.METHOD, "Method", ConvexHull.METHODS))
self.addOutput(OutputVector(ConvexHull.OUTPUT, "Convex hull"))
def processAlgorithm(self, progress):
2012-09-15 18:25:25 +03:00
useField = (self.getParameterValue(ConvexHull.METHOD) == 1)
fieldName = self.getParameterValue(ConvexHull.FIELD)
layer = QGisLayers.getObjectFromUri(self.getParameterValue(ConvexHull.INPUT))
2012-09-15 18:25:25 +03:00
GEOS_EXCEPT = True
FEATURE_EXCEPT = True
index = layer.fieldNameIndex(fieldName)
fType = layer.pendingFields()[index].type()
f = QgsField("value")
f.setType(QVariant.String)
f.setLength(255)
if useField:
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)
fields = [QgsField("id", QVariant.Int, "", 20),
f,
QgsField("area", QVariant.Double, "", 20, 6),
QgsField("perim", QVariant.Double, "", 20, 6)
]
writer = self.getOutputFromName(ConvexHull.OUTPUT).getVectorWriter(fields, QGis.WKBPolygon, layer.dataProvider().crs())
2012-09-15 18:25:25 +03:00
outFeat = QgsFeature()
inGeom = QgsGeometry()
outGeom = QgsGeometry()
current = 0
2013-01-01 23:52:00 +01:00
fid = 0
val = ""
2013-01-01 23:52:00 +01:00
if useField:
unique = layer.uniqueValues(index)
total = 100.0 / float(layer.featureCount() * len (unique))
for i in unique:
hull = []
first = True
features = QGisLayers.features(layer)
for f in features:
idVar = f[fieldName]
if idVar.toString().trimmed() == i.toString().trimmed():
if first:
val = idVar
first = False
inGeom = QgsGeometry(f.geometry())
points = utils.extractPoints(inGeom)
hull.extend(points)
current += 1
progress.setPercentage(int(current * total))
if len(hull) >= 3:
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
try:
outGeom = tmpGeom.convexHull()
(area, perim) = utils.simpleMeasure(outGeom)
outFeat.setGeometry(outGeom)
outFeat.setAttributes([QVariant(fid),
QVariant(val),
QVariant(area),
QVariant(perim)
])
writer.addFeature(outFeat)
except:
GEOS_EXCEPT = False
continue
fid += 1
2013-01-01 23:52:00 +01:00
else:
hull = []
total = 100.0 / float(layer.featureCount())
features = QGisLayers.features(layer)
for f in features:
inGeom = QgsGeometry(f.geometry())
points = utils.extractPoints(inGeom)
hull.extend(points)
current += 1
progress.setPercentage(int(current * total))
2013-01-01 23:52:00 +01:00
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
try:
outGeom = tmpGeom.convexHull()
(area, perim) = utils.simpleMeasure(outGeom)
outFeat.setGeometry(outGeom)
outFeat.setAttributes([QVariant(0),
QVariant("all"),
QVariant(area),
QVariant(perim)
])
writer.addFeature(outFeat)
2013-01-01 23:52:00 +01:00
except:
GEOS_EXCEPT = False
2012-09-15 18:25:25 +03:00
del writer
if not GEOS_EXCEPT:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing convex hull")
if not FEATURE_EXCEPT:
SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing convex hull")