3
3
import shapely .wkt
4
4
from shapely .geometry import Polygon
5
5
6
- from qgis .PyQt .QtCore import QCoreApplication
6
+ from qgis .PyQt .QtCore import QVariant
7
7
from qgis .core import (
8
8
QgsProcessingParameterExtent ,
9
9
QgsProcessingParameterVectorLayer ,
10
+ QgsWkbTypes ,
11
+ QgsField ,
10
12
)
11
13
12
14
sys .path .append (".." )
@@ -68,8 +70,8 @@ def processTc(self, tc, parameters, context):
68
70
self .traj_to_sink (traj )
69
71
70
72
71
- class ClipTrajectoriesByPolygonLayer (OverlayTrajectoriesAlgorithm ):
72
- CLIP_LAYER = "CLIP_LAYER "
73
+ class ClipTrajectoriesByPolygonLayerAlgorithm (OverlayTrajectoriesAlgorithm ):
74
+ OVERLAY_LAYER = "OVERLAY_LAYER "
73
75
74
76
def __init__ (self ):
75
77
super ().__init__ ()
@@ -78,7 +80,7 @@ def initAlgorithm(self, config=None):
78
80
super ().initAlgorithm (config )
79
81
self .addParameter (
80
82
QgsProcessingParameterVectorLayer (
81
- name = self .CLIP_LAYER ,
83
+ name = self .OVERLAY_LAYER ,
82
84
description = self .tr ("Overlay layer" ),
83
85
optional = False ,
84
86
)
@@ -105,10 +107,105 @@ def helpUrl(self):
105
107
return "https://movingpandas.org/units"
106
108
107
109
def processTc (self , tc , parameters , context ):
108
- vlayer = self .parameterAsVectorLayer (parameters , self .CLIP_LAYER , context )
110
+ vlayer = self .parameterAsVectorLayer (parameters , self .OVERLAY_LAYER , context )
109
111
for feature in vlayer .getFeatures ():
110
- extent = shapely .wkt .loads (feature .geometry ().asWkt ())
111
- clipped = tc .clip (extent )
112
+ shapely_feature = shapely .wkt .loads (feature .geometry ().asWkt ())
113
+ clipped = tc .clip (shapely_feature )
112
114
self .tc_to_sink (clipped )
113
115
for traj in clipped :
114
116
self .traj_to_sink (traj )
117
+
118
+
119
+ class IntersectWithPolygonLayerAlgorithm (OverlayTrajectoriesAlgorithm ):
120
+ OVERLAY_LAYER = "OVERLAY_LAYER"
121
+
122
+ def __init__ (self ):
123
+ super ().__init__ ()
124
+
125
+ def initAlgorithm (self , config = None ):
126
+ super ().initAlgorithm (config )
127
+ self .addParameter (
128
+ QgsProcessingParameterVectorLayer (
129
+ name = self .OVERLAY_LAYER ,
130
+ description = self .tr ("Overlay layer" ),
131
+ optional = False ,
132
+ )
133
+ )
134
+
135
+ def name (self ):
136
+ return "intersect_traj_vector"
137
+
138
+ def displayName (self ):
139
+ return self .tr ("Intersect trajectories with polygon layer" )
140
+
141
+ def shortHelpString (self ):
142
+ return self .tr (
143
+ "<p>Creates a trajectory point layers with speed and direction information "
144
+ "as well as a trajectory line layer which ihntersects the specified vector layer.</p>"
145
+ "<p><b>Speed</b> is calculated based on the input layer CRS information and "
146
+ "converted to the desired speed units. For more info on the supported units, "
147
+ "see https://movingpandas.org/units</p>"
148
+ "<p><b>Direction</b> is calculated between consecutive locations. Direction "
149
+ "values are in degrees, starting North turning clockwise.</p>"
150
+ )
151
+
152
+ def helpUrl (self ):
153
+ return "https://movingpandas.org/units"
154
+
155
+ def setup_pt_sink (self , parameters , context , tc , crs ):
156
+ self .fields_pts = self .get_pt_fields (
157
+ [
158
+ QgsField (tc .get_speed_col (), QVariant .Double ),
159
+ QgsField (tc .get_direction_col (), QVariant .Double ),
160
+ ],
161
+ )
162
+
163
+ vlayer = self .parameterAsVectorLayer (parameters , self .OVERLAY_LAYER , context )
164
+ for field in vlayer .fields ():
165
+ field .setName (f'intersecting_{ field .name ()} ' )
166
+ self .fields_pts .append (field )
167
+
168
+ (self .sink_pts , self .dest_pts ) = self .parameterAsSink (
169
+ parameters ,
170
+ self .OUTPUT_PTS ,
171
+ context ,
172
+ self .fields_pts ,
173
+ QgsWkbTypes .Point ,
174
+ crs ,
175
+ )
176
+
177
+ def setup_traj_sink (self , parameters , context , crs ):
178
+ self .fields_trajs = self .get_traj_fields ()
179
+
180
+ vlayer = self .parameterAsVectorLayer (parameters , self .OVERLAY_LAYER , context )
181
+ for field in vlayer .fields ():
182
+ field .setName (f'intersecting_{ field .name ()} ' )
183
+ self .fields_trajs .append (field )
184
+
185
+ (self .sink_trajs , self .dest_trajs ) = self .parameterAsSink (
186
+ parameters ,
187
+ self .OUTPUT_TRAJS ,
188
+ context ,
189
+ self .fields_trajs ,
190
+ QgsWkbTypes .LineStringM ,
191
+ crs ,
192
+ )
193
+
194
+ def processTc (self , tc , parameters , context ):
195
+ vlayer = self .parameterAsVectorLayer (parameters , self .OVERLAY_LAYER , context )
196
+ layer_fields = vlayer .fields ()
197
+ field_names = [field .name () for field in layer_fields ]
198
+ field_names_to_add = [f'intersecting_{ name } ' for name in field_names ]
199
+
200
+ for feature in vlayer .getFeatures ():
201
+ attrs = feature .attributes ()
202
+
203
+ shapely_feature = {
204
+ "geometry" : shapely .wkt .loads (feature .geometry ().asWkt ()),
205
+ "properties" : dict (zip (field_names , attrs )),
206
+ }
207
+ intersecting = tc .intersection (shapely_feature )
208
+
209
+ self .tc_to_sink (intersecting , field_names_to_add = field_names_to_add )
210
+ for traj in intersecting :
211
+ self .traj_to_sink (traj , attr_first_to_add = field_names_to_add )
0 commit comments