1
|
|
2
|
|
3
|
|
4
|
def voronoi_polygons( self ):
|
5
|
import voronoi
|
6
|
|
7
|
ptDict = {}
|
8
|
ptNdx = -1
|
9
|
vprovider = self.vlayer.dataProvider()
|
10
|
allAttrs = vprovider.attributeIndexes()
|
11
|
vprovider.select( allAttrs )
|
12
|
fields = {
|
13
|
0 : QgsField( "PointId", QVariant.Int ),
|
14
|
1 : QgsField( "PointName", QVariant.String )}
|
15
|
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
|
16
|
fields, QGis.WKBPolygon, vprovider.crs() )
|
17
|
inFeat = QgsFeature()
|
18
|
points = []
|
19
|
while vprovider.nextFeature( inFeat ):
|
20
|
inGeom = QgsGeometry( inFeat.geometry() )
|
21
|
point = inGeom.asPoint()
|
22
|
points.append( point )
|
23
|
ptNdx +=1
|
24
|
ptDict[ptNdx] = inFeat.id()
|
25
|
vprovider.rewind()
|
26
|
vprovider.select( allAttrs )
|
27
|
vertices,equations,edges,bounds,polygon_vertices = voronoi.computeVoronoiDiagram(points)
|
28
|
feat = QgsFeature()
|
29
|
nFeat = len( vertices )
|
30
|
nElement = 0
|
31
|
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
|
32
|
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
|
33
|
|
34
|
polygon_vertices.sort()
|
35
|
cur = -1
|
36
|
for i,v in enumerate(polygon_vertices):
|
37
|
if (not(v[0] == cur) and i > 0) or (i == len(polygon_vertices) - 1):
|
38
|
polygon.append(curStart)
|
39
|
feat.addAttribute( 0, QVariant( ptDict[ cur ] ) )
|
40
|
geometry = QgsGeometry().fromPolygon( [ polygon ] )
|
41
|
feat.setGeometry( geometry )
|
42
|
writer.addFeature( feat )
|
43
|
nElement += 1
|
44
|
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
|
45
|
if (not(v[0] == cur)) :
|
46
|
cur = v[0]
|
47
|
curStart = QgsPoint(v[2],v[3])
|
48
|
polygon = []
|
49
|
polygon.append( QgsPoint(v[2],v[3]) )
|
50
|
|
51
|
del writer
|
52
|
return True
|
53
|
|
54
|
|
55
|
|
56
|
|
57
|
|
58
|
def delaunay_triangulation( self ):
|
59
|
import voronoi
|
60
|
|
61
|
ptDict = {}
|
62
|
ptNdx = -1
|
63
|
vprovider = self.vlayer.dataProvider()
|
64
|
allAttrs = vprovider.attributeIndexes()
|
65
|
vprovider.select( allAttrs )
|
66
|
fields = {
|
67
|
0 : QgsField( "POINTA", QVariant.Double ),
|
68
|
1 : QgsField( "POINTB", QVariant.Double ),
|
69
|
2 : QgsField( "POINTC", QVariant.Double ) }
|
70
|
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
|
71
|
fields, QGis.WKBPolygon, vprovider.crs() )
|
72
|
inFeat = QgsFeature()
|
73
|
points = []
|
74
|
while vprovider.nextFeature( inFeat ):
|
75
|
inGeom = QgsGeometry( inFeat.geometry() )
|
76
|
point = inGeom.asPoint()
|
77
|
points.append( point )
|
78
|
ptNdx +=1
|
79
|
ptDict[ptNdx] = inFeat.id()
|
80
|
vprovider.rewind()
|
81
|
vprovider.select( allAttrs )
|
82
|
triangles = voronoi.computeDelaunayTriangulation( points )
|
83
|
feat = QgsFeature()
|
84
|
nFeat = len( triangles )
|
85
|
nElement = 0
|
86
|
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
|
87
|
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
|
88
|
for triangle in triangles:
|
89
|
indicies = list( triangle )
|
90
|
indicies.append( indicies[ 0 ] )
|
91
|
polygon = []
|
92
|
step = 0
|
93
|
for index in indicies:
|
94
|
vprovider.featureAtId( ptDict[index], inFeat, True, allAttrs )
|
95
|
geom = QgsGeometry( inFeat.geometry() )
|
96
|
point = QgsPoint( geom.asPoint() )
|
97
|
polygon.append( point )
|
98
|
if step <= 3: feat.addAttribute( step, QVariant( index ) )
|
99
|
step += 1
|
100
|
geometry = QgsGeometry().fromPolygon( [ polygon ] )
|
101
|
feat.setGeometry( geometry )
|
102
|
writer.addFeature( feat )
|
103
|
nElement += 1
|
104
|
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
|
105
|
del writer
|
106
|
return True
|
107
|
|