25
25
26
26
__revision__ = '$Format:%H$'
27
27
28
+ import os
28
29
from datetime import datetime
29
30
from datetime import timedelta
30
31
40
41
from processing .parameters .ParameterString import ParameterString
41
42
#from processing.parameters.ParameterNumber import ParameterNumber
42
43
from processing .outputs .OutputVector import OutputVector
43
- from processing .tools import dataobjects , vector
44
+ from processing .outputs .OutputDirectory import OutputDirectory
45
+ from processing .tools import dataobjects , vector , system
46
+
44
47
45
48
class PointsToPaths (GeoAlgorithm ):
46
49
@@ -49,7 +52,8 @@ class PointsToPaths(GeoAlgorithm):
49
52
ORDER_FIELD = 'ORDER_FIELD'
50
53
DATE_FORMAT = 'DATE_FORMAT'
51
54
#GAP_PERIOD = 'GAP_PERIOD'
52
- OUTPUT = 'OUTPUT'
55
+ OUTPUT_LINES = 'OUTPUT_LINES'
56
+ OUTPUT_TEXT = 'OUTPUT_TEXT'
53
57
54
58
def defineCharacteristics (self ):
55
59
self .name = 'Points to path'
@@ -65,7 +69,8 @@ def defineCharacteristics(self):
65
69
#self.addParameter(ParameterNumber(
66
70
# self.GAP_PERIOD,
67
71
# 'Gap period (if order field is DateTime)', 0, 60, 0))
68
- self .addOutput (OutputVector (self .OUTPUT , 'Paths' ))
72
+ self .addOutput (OutputVector (self .OUTPUT_LINES , 'Paths' ))
73
+ self .addOutput (OutputDirectory (self .OUTPUT_TEXT , 'Directory' ))
69
74
70
75
def processAlgorithm (self , progress ):
71
76
layer = dataobjects .getObjectFromUri (
@@ -74,12 +79,13 @@ def processAlgorithm(self, progress):
74
79
orderField = self .getParameterValue (self .ORDER_FIELD )
75
80
dateFormat = unicode (self .getParameterValue (self .DATE_FORMAT ))
76
81
#gap = int(self.getParameterValue(self.GAP_PERIOD))
82
+ dirName = self .getOutputValue (self .OUTPUT_TEXT )
77
83
78
84
fields = QgsFields ()
79
85
fields .append (QgsField ('group' , QVariant .String , '' , 254 , 0 ))
80
86
fields .append (QgsField ('begin' , QVariant .String , '' , 254 , 0 ))
81
87
fields .append (QgsField ('end' , QVariant .String , '' , 254 , 0 ))
82
- writer = self .getOutputFromName (self .OUTPUT ).getVectorWriter (
88
+ writer = self .getOutputFromName (self .OUTPUT_LINES ).getVectorWriter (
83
89
fields , QGis .WKBLineString , layer .dataProvider ().crs ())
84
90
85
91
points = dict ()
@@ -100,22 +106,49 @@ def processAlgorithm(self, progress):
100
106
101
107
progress .setPercentage (0 )
102
108
109
+ da = QgsDistanceArea ()
110
+
103
111
count = 0
104
112
total = 100.0 / len (points )
105
- for k , v in points .iteritems ():
106
- v .sort ()
113
+ for group , vertices in points .iteritems ():
114
+ vertices .sort ()
107
115
f = QgsFeature ()
108
116
f .initAttributes (len (fields ))
109
117
f .setFields (fields )
110
- f ['group' ] = k
111
- f ['begin' ] = v [0 ][0 ]
112
- f ['end' ] = v [- 1 ][0 ]
118
+ f ['group' ] = group
119
+ f ['begin' ] = vertices [0 ][0 ]
120
+ f ['end' ] = vertices [- 1 ][0 ]
121
+
122
+ if dirName == '' :
123
+ fileName = system .getTempFilenameInTempFolder ('%s.txt' % group )
124
+ else :
125
+ fileName = os .path .join (dirName , '%s.txt' % group )
126
+
127
+ fl = open (fileName , 'w' )
128
+ fl .write ('angle=Azimuth\n ' )
129
+ fl .write ('heading=Coordinate_System\n ' )
130
+ fl .write ('dist_units=Default\n ' )
131
+
113
132
line = []
114
- for node in v :
133
+ i = 0
134
+ for node in vertices :
115
135
line .append (node [1 ])
136
+
137
+ if i == 0 :
138
+ fl .write ('startAt=%f;%f;90\n ' % (node [1 ].x (), node [1 ].y ()))
139
+ fl .write ('survey=Polygonal\n ' )
140
+ fl .write ('[data]\n ' )
141
+ else :
142
+ angle = line [i - 1 ].azimuth (line [i ])
143
+ distance = da .measureLine (line [i - 1 ], line [i ])
144
+ fl .write ('%f;%f;90\n ' % (angle , distance ))
145
+
146
+ i += 1
147
+
116
148
f .setGeometry (QgsGeometry .fromPolyline (line ))
117
149
writer .addFeature (f )
118
150
count += 1
119
151
progress .setPercentage (int (count * total ))
120
152
121
153
del writer
154
+ fl .close ()
0 commit comments