1
- # -*- coding: utf-8 -*-
2
- import os
3
- import sys
1
+ import sys
4
2
5
3
from qgis .PyQt .QtCore import QCoreApplication , QVariant
6
- from qgis .PyQt .QtGui import QIcon
7
-
8
- from qgis .core import (QgsField , QgsFields , QgsPointXY ,
9
- QgsGeometry ,
10
- QgsFeature ,
11
- QgsFeatureSink ,
12
- QgsFeatureRequest ,
13
- QgsProcessing ,
14
- QgsProcessingAlgorithm ,
15
- QgsProcessingParameterFeatureSource ,
16
- QgsProcessingParameterString ,
17
- QgsProcessingParameterField ,
18
- QgsProcessingParameterNumber ,
19
- QgsProcessingParameterBoolean ,
20
- QgsProcessingParameterFeatureSink ,
21
- QgsProcessingParameterEnum ,
22
- QgsWkbTypes ,
23
- QgsProcessingUtils
24
- )
4
+ from qgis .core import (
5
+ QgsField ,
6
+ QgsGeometry ,
7
+ QgsFeature ,
8
+ QgsFeatureSink ,
9
+ QgsProcessingParameterString ,
10
+ QgsWkbTypes ,
11
+ )
25
12
26
13
sys .path .append (".." )
27
14
28
- from .qgisUtils import tc_from_pt_layer , tc_to_sink , get_pt_fields , get_traj_fields
29
-
30
- pluginPath = os .path .dirname (__file__ )
15
+ from .qgisUtils import tc_to_sink , traj_to_sink
16
+ from .trajectoriesAlgorithm import TrajectoriesAlgorithm
31
17
32
18
33
- class CreateTrajectoriesAlgorithm (QgsProcessingAlgorithm ):
34
- # script parameters
35
- INPUT = 'INPUT'
36
- TRAJ_ID_FIELD = 'OBJECT_ID_FIELD'
37
- TIMESTAMP_FIELD = 'TIMESTAMP_FIELD'
38
- TIMESTAMP_FORMAT = 'TIMESTAMP_FORMAT'
39
- OUTPUT_PTS = 'OUTPUT_PTS'
40
- OUTPUT_SEGS = 'OUTPUT_SEGS'
41
- OUTPUT_TRAJS = 'OUTPUT_TRAJS'
42
- SPEED_UNIT = 'SPEED_UNIT'
19
+ class CreateTrajectoriesAlgorithm (TrajectoriesAlgorithm ):
20
+ SPEED_UNIT = "SPEED_UNIT"
43
21
44
22
def __init__ (self ):
45
23
super ().__init__ ()
46
24
47
25
def name (self ):
48
26
return "create_trajectory"
49
27
50
- def icon (self ):
51
- return QIcon (os .path .join (pluginPath , "icons" , "icon.png" ))
52
-
53
28
def tr (self , text ):
54
29
return QCoreApplication .translate ("create_trajectory" , text )
55
30
56
31
def displayName (self ):
57
32
return self .tr ("Create trajectories" )
58
33
59
- # def group(self):
60
- # return self.tr("Basic")
34
+ def group (self ):
35
+ return self .tr ("Basic" )
61
36
62
- # def groupId(self):
63
- # return "TrajectoryBasic"
37
+ def groupId (self ):
38
+ return "TrajectoryBasic"
64
39
65
40
def shortHelpString (self ):
66
41
return self .tr (
67
- "<p>Creates a trajectory point layers with speed and direction information "
42
+ "<p>Creates a trajectory point layers with speed and direction information "
68
43
"as well as a trajectory line layer.</p>"
69
- "<p><b>Speed</b> is calculated based on the input layer CRS information and "
70
- "converted to the desired speed units. For more info on the supported units, "
71
- "see https://movingpandas.org/units</p>"
72
- "<p><b>Direction</b> is calculated between consecutive locations. Direction "
44
+ "<p><b>Speed</b> is calculated based on the input layer CRS information and "
45
+ "converted to the desired speed units. For more info on the supported units, "
46
+ "see https://movingpandas.org/units</p>"
47
+ "<p><b>Direction</b> is calculated between consecutive locations. Direction "
73
48
"values are in degrees, starting North turning clockwise.</p>"
74
- )
49
+ )
75
50
76
51
def helpUrl (self ):
77
52
return "https://movingpandas.org/units"
78
53
79
54
def createInstance (self ):
80
55
return type (self )()
81
56
82
- def initAlgorithm (self , config = None ):
83
- # input layer
84
- self .addParameter (QgsProcessingParameterFeatureSource (
85
- name = self .INPUT ,
86
- description = self .tr ("Input point layer" ),
87
- types = [QgsProcessing .TypeVectorPoint ]))
88
- # fields
89
- self .addParameter (QgsProcessingParameterField (
90
- name = self .TRAJ_ID_FIELD ,
91
- description = self .tr ("Trajectory ID field" ),
92
- defaultValue = "trajectory_id" ,
93
- parentLayerParameterName = self .INPUT ,
94
- type = QgsProcessingParameterField .Any ,
95
- allowMultiple = False ,
96
- optional = False ))
97
- self .addParameter (QgsProcessingParameterField (
98
- name = self .TIMESTAMP_FIELD ,
99
- description = self .tr ("Timestamp field" ),
100
- defaultValue = "t" ,
101
- parentLayerParameterName = self .INPUT ,
102
- type = QgsProcessingParameterField .Any ,
103
- allowMultiple = False ,
104
- optional = False ))
105
- # self.addParameter(QgsProcessingParameterString(
106
- # name=self.TIMESTAMP_FORMAT,
107
- # description=self.tr("Timestamp format (e.g. %Y-%m-%d %H:%M:%S)"),
108
- # #defaultValue="%Y-%m-%d %H:%M:%S+00",
109
- # optional=True))
110
- self .addParameter (QgsProcessingParameterString (
111
- name = self .SPEED_UNIT ,
112
- description = self .tr ("Speed units (e.g. km/h, m/s)" ),
113
- defaultValue = "km/h" ,
114
- optional = False ))
115
- # output layer
116
- self .addParameter (QgsProcessingParameterFeatureSink (
117
- name = self .OUTPUT_PTS ,
118
- description = self .tr ("Trajectory points" ),
119
- type = QgsProcessing .TypeVectorPoint ))
120
- self .addParameter (QgsProcessingParameterFeatureSink (
121
- name = self .OUTPUT_TRAJS ,
122
- description = self .tr ("Trajectories" ),
123
- type = QgsProcessing .TypeVectorLine ))
124
-
125
- def processAlgorithm (self , parameters , context , feedback ):
126
- self .input_layer = self .parameterAsSource (parameters , self .INPUT , context )
127
- self .traj_id_field = self .parameterAsFields (parameters , self .TRAJ_ID_FIELD , context )[0 ]
128
- self .timestamp_field = self .parameterAsFields (parameters , self .TIMESTAMP_FIELD , context )[0 ]
129
- #timestamp_format = self.parameterAsString(parameters, self.TIMESTAMP_FORMAT, context)
130
- speed_units = self .parameterAsString (parameters , self .SPEED_UNIT , context ).split ("/" )
131
-
132
- tc = tc_from_pt_layer (self .input_layer , self .timestamp_field , self .traj_id_field )# , timestamp_format)
133
- tc .add_speed (units = tuple (speed_units ), overwrite = True )
134
- tc .add_direction (overwrite = True )
135
-
136
- self .dest_pts = self .create_points (parameters , context , tc )
137
- self .dest_trajs = self .create_trajectories (parameters , context , tc )
138
-
139
- return {self .OUTPUT_PTS : self .dest_pts , self .OUTPUT_TRAJS : self .dest_trajs }
140
-
141
- def postProcessAlgorithm (self , context , feedback ):
142
- processed_layer = QgsProcessingUtils .mapLayerFromString (self .dest_pts , context )
143
- processed_layer .loadNamedStyle (os .path .join (pluginPath , "styles" , "pts.qml" ))
144
- return {self .OUTPUT_PTS : self .dest_pts , self .OUTPUT_TRAJS : self .dest_trajs }
145
-
146
- def create_points (self , parameters , context , tc ):
147
- fields_pts = get_pt_fields (self .input_layer , self .traj_id_field )
148
- fields_pts .append (QgsField (tc .get_speed_col (), QVariant .Double ))
149
- fields_pts .append (QgsField (tc .get_direction_col (), QVariant .Double ))
150
- crs = self .input_layer .sourceCrs ()
151
-
152
- (sink , dest ) = self .parameterAsSink (
153
- parameters , self .OUTPUT_PTS , context , fields_pts , QgsWkbTypes .Point , crs )
154
-
155
- tc_to_sink (tc , sink , fields_pts , self .timestamp_field )
156
- return dest
157
-
158
- def create_trajectories (self , parameters , context , tc ):
159
- output_fields_lines = get_traj_fields (self .input_layer , self .traj_id_field )
160
- crs = self .input_layer .sourceCrs ()
161
-
162
- (sink , dest ) = self .parameterAsSink (
163
- parameters , self .OUTPUT_TRAJS , context , output_fields_lines , QgsWkbTypes .LineStringM , crs )
164
-
57
+ def processTc (self , tc , parameters , context ):
58
+ tc_to_sink (tc , self .sink_pts , self .fields_pts , self .timestamp_field )
165
59
for traj in tc .trajectories :
166
- line = QgsGeometry .fromWkt (traj .to_linestringm_wkt ())
167
- f = QgsFeature ()
168
- f .setGeometry (line )
169
- f .setAttributes ([traj .id ])
170
- sink .addFeature (f , QgsFeatureSink .FastInsert )
171
- return dest
172
-
60
+ traj_to_sink (traj , self .sink_trajs )
0 commit comments