103 lines
3.2 KiB
Python
Raw Normal View History

2012-09-15 18:25:25 +03:00
import os.path
2012-10-01 20:59:10 +03:00
from sets import Set
2012-09-15 18:25:25 +03:00
from PyQt4 import QtGui
from PyQt4.QtCore import *
2012-10-01 20:59:10 +03:00
2012-09-15 18:25:25 +03:00
from qgis.core import *
2012-10-01 20:59:10 +03:00
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
2012-09-15 18:25:25 +03:00
from sextante.core.QGisLayers import QGisLayers
2012-10-01 20:59:10 +03:00
from sextante.parameters.ParameterVector import ParameterVector
2012-09-15 18:25:25 +03:00
from sextante.outputs.OutputVector import OutputVector
2012-10-01 20:59:10 +03:00
from sextante.ftools import voronoi
2012-09-15 18:25:25 +03:00
class Delaunay(GeoAlgorithm):
INPUT = "INPUT"
OUTPUT = "OUTPUT"
def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/delaunay.png")
2012-10-01 20:59:10 +03:00
def defineCharacteristics(self):
self.name = "Delaunay triangulation"
self.group = "Geometry tools"
self.addParameter(ParameterVector(self.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POINT))
self.addOutput(OutputVector(self.OUTPUT, "Delaunay triangulation"))
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
2012-10-01 20:59:10 +03:00
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
output = self.getOutputValue(self.OUTPUT)
provider = layer.dataProvider()
provider.select()
fields = {0 : QgsField("POINTA", QVariant.Double, "", 24, 15),
1 : QgsField("POINTB", QVariant.Double, "", 24, 15),
2 : QgsField("POINTC", QVariant.Double, "", 24, 15)
}
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
QGis.WKBPolygon, provider.crs())
2012-09-15 18:25:25 +03:00
pts = []
ptDict = {}
ptNdx = -1
2012-10-01 20:59:10 +03:00
inFeat = QgsFeature()
c = voronoi.Context()
while provider.nextFeature(inFeat):
geom = QgsGeometry(inFeat.geometry())
point = geom.asPoint()
x = point.x()
y = point.y()
pts.append((x, y))
ptNdx +=1
ptDict[ptNdx] = inFeat.id()
2012-09-15 18:25:25 +03:00
if len(pts) < 3:
2012-10-01 20:59:10 +03:00
raise GeoAlgorithmExecutionException("Input file should contain at least 3 points. Choose another file and try again.")
2012-09-15 18:25:25 +03:00
uniqueSet = Set(item for item in pts)
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
current = 0
total = 100.0 / float(len(triangles))
2012-09-15 18:25:25 +03:00
for triangle in triangles:
2012-10-01 20:59:10 +03:00
indicies = list(triangle)
indicies.append(indicies[0])
polygon = []
step = 0
2012-09-15 18:25:25 +03:00
2012-10-01 20:59:10 +03:00
for index in indicies:
provider.featureAtId(ptDict[ids[index]], inFeat, True)
geom = QgsGeometry(inFeat.geometry())
point = QgsPoint(geom.asPoint())
polygon.append(point)
if step <= 3:
feat.addAttribute(step, QVariant(ids[index]))
step += 1
2012-09-15 18:25:25 +03:00
2012-10-01 20:59:10 +03:00
geometry = QgsGeometry().fromPolygon([polygon])
feat.setGeometry(geometry)
writer.addFeature(feat)
current += 1
progress.setPercentage(int(current * total))
del writer