1
- from sextante .core .GeoAlgorithm import GeoAlgorithm
2
1
import os .path
2
+ from sets import Set
3
+
3
4
from PyQt4 import QtGui
4
5
from PyQt4 .QtCore import *
5
- from PyQt4 . QtGui import *
6
+
6
7
from qgis .core import *
7
- from sextante .parameters .ParameterVector import ParameterVector
8
+
9
+ from sextante .core .GeoAlgorithm import GeoAlgorithm
10
+ from sextante .core .GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
8
11
from sextante .core .QGisLayers import QGisLayers
12
+
13
+ from sextante .parameters .ParameterVector import ParameterVector
9
14
from sextante .outputs .OutputVector import OutputVector
10
- import voronoi
11
- from sets import Set
15
+
16
+ from sextante . ftools import voronoi
12
17
13
18
class Delaunay (GeoAlgorithm ):
14
19
@@ -18,62 +23,80 @@ class Delaunay(GeoAlgorithm):
18
23
def getIcon (self ):
19
24
return QtGui .QIcon (os .path .dirname (__file__ ) + "/icons/delaunay.png" )
20
25
26
+ def defineCharacteristics (self ):
27
+ self .name = "Delaunay triangulation"
28
+ self .group = "Geometry tools"
29
+
30
+ self .addParameter (ParameterVector (self .INPUT , "Input layer" , ParameterVector .VECTOR_TYPE_POINT ))
31
+
32
+ self .addOutput (OutputVector (self .OUTPUT , "Delaunay triangulation" ))
33
+
21
34
def processAlgorithm (self , progress ):
22
- vlayer = QGisLayers .getObjectFromUri (self .getParameterValue (Delaunay .INPUT ))
23
- vprovider = vlayer .dataProvider ()
24
- allAttrs = vprovider .attributeIndexes ()
25
- vprovider .select ( allAttrs )
26
- fields = {
27
- 0 : QgsField ( "POINTA" , QVariant .Double ),
28
- 1 : QgsField ( "POINTB" , QVariant .Double ),
29
- 2 : QgsField ( "POINTC" , QVariant .Double ) }
30
- writer = self .getOutputFromName (Delaunay .OUTPUT ).getVectorWriter (fields , QGis .WKBPolygon , vprovider .crs () )
31
- inFeat = QgsFeature ()
32
- c = voronoi .Context ()
35
+ settings = QSettings ()
36
+ encoding = settings .value ( "/UI/encoding" , "System" ).toString ()
37
+
38
+ layer = QGisLayers .getObjectFromUri (self .getParameterValue (self .INPUT ))
39
+ output = self .getOutputValue (self .OUTPUT )
40
+
41
+ provider = layer .dataProvider ()
42
+ provider .select ()
43
+
44
+ fields = {0 : QgsField ("POINTA" , QVariant .Double , "" , 24 , 15 ),
45
+ 1 : QgsField ("POINTB" , QVariant .Double , "" , 24 , 15 ),
46
+ 2 : QgsField ("POINTC" , QVariant .Double , "" , 24 , 15 )
47
+ }
48
+
49
+ writer = self .getOutputFromName (self .OUTPUT ).getVectorWriter (fields ,
50
+ QGis .WKBPolygon , provider .crs ())
51
+
33
52
pts = []
34
53
ptDict = {}
35
54
ptNdx = - 1
36
- while vprovider .nextFeature (inFeat ):
37
- geom = QgsGeometry (inFeat .geometry ())
38
- point = geom .asPoint ()
39
- x = point .x ()
40
- y = point .y ()
41
- pts .append ((x , y ))
42
- ptNdx += 1
43
- ptDict [ptNdx ] = inFeat .id ()
55
+ inFeat = QgsFeature ()
56
+ c = voronoi .Context ()
57
+
58
+ while provider .nextFeature (inFeat ):
59
+ geom = QgsGeometry (inFeat .geometry ())
60
+ point = geom .asPoint ()
61
+ x = point .x ()
62
+ y = point .y ()
63
+ pts .append ((x , y ))
64
+ ptNdx += 1
65
+ ptDict [ptNdx ] = inFeat .id ()
66
+
44
67
if len (pts ) < 3 :
45
- return False
68
+ raise GeoAlgorithmExecutionException ("Input file should contain at least 3 points. Choose another file and try again." )
69
+
46
70
uniqueSet = Set (item for item in pts )
47
71
ids = [pts .index (item ) for item in uniqueSet ]
48
72
sl = voronoi .SiteList ([voronoi .Site (* i ) for i in uniqueSet ])
49
73
c .triangulate = True
50
74
voronoi .voronoi (sl , c )
51
75
triangles = c .triangles
52
76
feat = QgsFeature ()
53
- nFeat = len ( triangles )
54
- nElement = 0
77
+
78
+ current = 0
79
+ total = 100.0 / float (len (triangles ))
80
+
55
81
for triangle in triangles :
56
- indicies = list (triangle )
57
- indicies .append (indicies [0 ])
58
- polygon = []
59
- step = 0
60
- for index in indicies :
61
- vprovider .featureAtId (ptDict [ids [index ]], inFeat , True , allAttrs )
62
- geom = QgsGeometry (inFeat .geometry ())
63
- point = QgsPoint (geom .asPoint ())
64
- polygon .append (point )
65
- if step <= 3 : feat .addAttribute (step , QVariant (ids [index ]))
66
- step += 1
67
- geometry = QgsGeometry ().fromPolygon ([polygon ])
68
- feat .setGeometry (geometry )
69
- writer .addFeature (feat )
70
- nElement += 1
71
- progress .setPercentage (nElement / nFeat * 100 )
72
- del writer
82
+ indicies = list (triangle )
83
+ indicies .append (indicies [0 ])
84
+ polygon = []
85
+ step = 0
73
86
74
- def defineCharacteristics (self ):
75
- self .name = "Delaunay triangulation"
76
- self .group = "Geometry tools"
77
- self .addParameter (ParameterVector (Delaunay .INPUT , "Input layer" , ParameterVector .VECTOR_TYPE_POINT ))
78
- self .addOutput (OutputVector (Delaunay .OUTPUT , "Delaunay triangulation" ))
87
+ for index in indicies :
88
+ provider .featureAtId (ptDict [ids [index ]], inFeat , True )
89
+ geom = QgsGeometry (inFeat .geometry ())
90
+ point = QgsPoint (geom .asPoint ())
91
+ polygon .append (point )
92
+ if step <= 3 :
93
+ feat .addAttribute (step , QVariant (ids [index ]))
94
+ step += 1
79
95
96
+ geometry = QgsGeometry ().fromPolygon ([polygon ])
97
+ feat .setGeometry (geometry )
98
+ writer .addFeature (feat )
99
+ current += 1
100
+ progress .setPercentage (int (current * total ))
101
+
102
+ del writer
0 commit comments