mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-04 00:06:46 -05:00
review Delaunay tool
This commit is contained in:
parent
e91efc1ec1
commit
5ceedfd14f
@ -1,14 +1,19 @@
|
||||
from sextante.core.GeoAlgorithm import GeoAlgorithm
|
||||
import os.path
|
||||
from sets import Set
|
||||
|
||||
from PyQt4 import QtGui
|
||||
from PyQt4.QtCore import *
|
||||
from PyQt4.QtGui import *
|
||||
|
||||
from qgis.core import *
|
||||
from sextante.parameters.ParameterVector import ParameterVector
|
||||
|
||||
from sextante.core.GeoAlgorithm import GeoAlgorithm
|
||||
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
||||
from sextante.core.QGisLayers import QGisLayers
|
||||
|
||||
from sextante.parameters.ParameterVector import ParameterVector
|
||||
from sextante.outputs.OutputVector import OutputVector
|
||||
import voronoi
|
||||
from sets import Set
|
||||
|
||||
from sextante.ftools import voronoi
|
||||
|
||||
class Delaunay(GeoAlgorithm):
|
||||
|
||||
@ -18,31 +23,50 @@ class Delaunay(GeoAlgorithm):
|
||||
def getIcon(self):
|
||||
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/delaunay.png")
|
||||
|
||||
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"))
|
||||
|
||||
def processAlgorithm(self, progress):
|
||||
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(Delaunay.INPUT))
|
||||
vprovider = vlayer.dataProvider()
|
||||
allAttrs = vprovider.attributeIndexes()
|
||||
vprovider.select( allAttrs )
|
||||
fields = {
|
||||
0 : QgsField( "POINTA", QVariant.Double ),
|
||||
1 : QgsField( "POINTB", QVariant.Double ),
|
||||
2 : QgsField( "POINTC", QVariant.Double ) }
|
||||
writer = self.getOutputFromName(Delaunay.OUTPUT).getVectorWriter(fields, QGis.WKBPolygon, vprovider.crs() )
|
||||
inFeat = QgsFeature()
|
||||
c = voronoi.Context()
|
||||
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())
|
||||
|
||||
pts = []
|
||||
ptDict = {}
|
||||
ptNdx = -1
|
||||
while vprovider.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()
|
||||
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()
|
||||
|
||||
if len(pts) < 3:
|
||||
return False
|
||||
raise GeoAlgorithmExecutionException("Input file should contain at least 3 points. Choose another file and try again.")
|
||||
|
||||
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])
|
||||
@ -50,30 +74,29 @@ class Delaunay(GeoAlgorithm):
|
||||
voronoi.voronoi(sl, c)
|
||||
triangles = c.triangles
|
||||
feat = QgsFeature()
|
||||
nFeat = len( triangles )
|
||||
nElement = 0
|
||||
|
||||
current = 0
|
||||
total = 100.0 / float(len(triangles))
|
||||
|
||||
for triangle in triangles:
|
||||
indicies = list(triangle)
|
||||
indicies.append(indicies[0])
|
||||
polygon = []
|
||||
step = 0
|
||||
for index in indicies:
|
||||
vprovider.featureAtId(ptDict[ids[index]], inFeat, True, allAttrs)
|
||||
geom = QgsGeometry(inFeat.geometry())
|
||||
point = QgsPoint(geom.asPoint())
|
||||
polygon.append(point)
|
||||
if step <= 3: feat.addAttribute(step, QVariant(ids[index]))
|
||||
step += 1
|
||||
geometry = QgsGeometry().fromPolygon([polygon])
|
||||
feat.setGeometry(geometry)
|
||||
writer.addFeature(feat)
|
||||
nElement += 1
|
||||
progress.setPercentage(nElement/nFeat * 100)
|
||||
indicies = list(triangle)
|
||||
indicies.append(indicies[0])
|
||||
polygon = []
|
||||
step = 0
|
||||
|
||||
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
|
||||
|
||||
geometry = QgsGeometry().fromPolygon([polygon])
|
||||
feat.setGeometry(geometry)
|
||||
writer.addFeature(feat)
|
||||
current += 1
|
||||
progress.setPercentage(int(current * total))
|
||||
|
||||
del writer
|
||||
|
||||
def defineCharacteristics(self):
|
||||
self.name = "Delaunay triangulation"
|
||||
self.group = "Geometry tools"
|
||||
self.addParameter(ParameterVector(Delaunay.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POINT))
|
||||
self.addOutput(OutputVector(Delaunay.OUTPUT, "Delaunay triangulation"))
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user