126 lines
4.6 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
Delaunay.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'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
import os
from PyQt4.QtGui import QIcon
from PyQt4.QtCore import QVariant
from qgis.core import QGis, QgsField, QgsFeatureRequest, QgsFeature, QgsGeometry, QgsPoint
2013-08-12 20:44:27 +02:00
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import dataobjects, vector
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
import voronoi
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
2012-09-15 18:25:25 +03:00
class Delaunay(GeoAlgorithm):
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
2012-09-15 18:25:25 +03:00
def getIcon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'delaunay.png'))
2012-10-01 20:59:10 +03:00
def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Delaunay triangulation')
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
2012-10-01 20:59:10 +03:00
2015-01-15 20:41:15 +02:00
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POINT]))
2012-10-01 20:59:10 +03:00
2015-01-15 20:41:15 +02:00
self.addOutput(OutputVector(self.OUTPUT,
self.tr('Delaunay triangulation')))
2012-10-01 20:59:10 +03:00
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
2012-10-01 20:59:10 +03:00
fields = [QgsField('POINTA', QVariant.Double, '', 24, 15),
QgsField('POINTB', QVariant.Double, '', 24, 15),
QgsField('POINTC', QVariant.Double, '', 24, 15)]
2012-10-01 20:59:10 +03:00
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
QGis.WKBPolygon, layer.crs())
2012-10-01 20:59:10 +03:00
2012-09-15 18:25:25 +03:00
pts = []
ptDict = {}
ptNdx = -1
2012-10-01 20:59:10 +03:00
c = voronoi.Context()
features = vector.features(layer)
total = 100.0 / len(features)
for current, inFeat in enumerate(features):
2012-10-01 20:59:10 +03:00
geom = QgsGeometry(inFeat.geometry())
point = geom.asPoint()
x = point.x()
y = point.y()
pts.append((x, y))
2013-01-01 23:52:00 +01:00
ptNdx += 1
2012-10-01 20:59:10 +03:00
ptDict[ptNdx] = inFeat.id()
progress.setPercentage(int(current * total))
2012-10-01 20:59:10 +03:00
2012-09-15 18:25:25 +03:00
if len(pts) < 3:
raise GeoAlgorithmExecutionException(
2015-01-15 20:41:15 +02:00
self.tr('Input file should contain at least 3 points. Choose '
'another file and try again.'))
2012-10-01 20:59:10 +03:00
uniqueSet = set(item for item in pts)
2012-09-15 18:25:25 +03:00
ids = [pts.index(item) for item in uniqueSet]
sl = voronoi.SiteList([voronoi.Site(*i) for i in uniqueSet])
c.triangulate = True
voronoi.voronoi(sl, c)
triangles = c.triangles
feat = QgsFeature()
2012-10-01 20:59:10 +03:00
total = 100.0 / len(triangles)
for current, triangle in enumerate(triangles):
2012-10-01 20:59:10 +03:00
indicies = list(triangle)
indicies.append(indicies[0])
polygon = []
attrs = []
2012-10-01 20:59:10 +03:00
step = 0
for index in indicies:
request = QgsFeatureRequest().setFilterFid(ptDict[ids[index]])
inFeat = layer.getFeatures(request).next()
2012-10-01 20:59:10 +03:00
geom = QgsGeometry(inFeat.geometry())
point = QgsPoint(geom.asPoint())
polygon.append(point)
if step <= 3:
attrs.append(ids[index])
2012-10-01 20:59:10 +03:00
step += 1
feat.setAttributes(attrs)
2012-10-01 20:59:10 +03:00
geometry = QgsGeometry().fromPolygon([polygon])
feat.setGeometry(geometry)
writer.addFeature(feat)
progress.setPercentage(int(current * total))
del writer