35
35
SectionSorter ,
36
36
get_pt_object_coord ,
37
37
generate_id ,
38
- get_overriding_mode ,
39
38
)
40
39
from jormungandr .street_network .utils import crowfly_distance_between
41
40
from jormungandr .fallback_modes import FallbackModes , all_fallback_modes
@@ -247,6 +246,34 @@ def _extend_with_car_park(
247
246
dp_journey .distances .walking += int (walking_speed * car_park_crowfly_duration )
248
247
249
248
249
+ def append_path_item_with_access_point (path_items , stop_point , access_point ):
250
+ via = path_items .add ()
251
+ via .duration = access_point .traversal_time
252
+ via .length = access_point .length
253
+ via .name = access_point .name
254
+ # Use label in stead of name???
255
+ via .instruction = u"Then Enter {} via {}." .format (stop_point .label , access_point .name )
256
+ via .via_uri = access_point .uri
257
+
258
+
259
+ def prepend_path_item_with_access_point (path_items , stop_point , access_point ):
260
+ via = path_items .add ()
261
+ via .duration = access_point .traversal_time
262
+ via .length = access_point .length
263
+ via .name = access_point .name
264
+ # Use label in stead of name???
265
+ via .instruction = u"Exit {} via {}." .format (stop_point .label , access_point .name )
266
+ via .via_uri = access_point .uri
267
+
268
+ # we cannot insert an element at the beginning of a list :(
269
+ # a little algo to move the last element to the beginning
270
+ tmp_item = response_pb2 .PathItem ()
271
+ for i in range (len (path_items )):
272
+ tmp_item .CopyFrom (path_items [i ])
273
+ path_items [i ].CopyFrom (path_items [- 1 ])
274
+ path_items [- 1 ].CopyFrom (tmp_item )
275
+
276
+
250
277
def _extend_with_via_access_point (fallback_dp , pt_object , fallback_type , via_access_point ):
251
278
if via_access_point is None :
252
279
return
@@ -266,37 +293,21 @@ def _extend_with_via_access_point(fallback_dp, pt_object, fallback_type, via_acc
266
293
dp_journey .sections [0 ].street_network .duration += traversal_time
267
294
dp_journey .sections [0 ].street_network .length += length
268
295
269
- via = dp_journey .sections [- 1 ].street_network .path_items .add ()
270
- via .duration = traversal_time
271
- via .length = length
272
- via .name = via_access_point .name
273
- # Use label in stead of name???
274
- via .instruction = u"Then Enter {} via {}." .format (pt_object .stop_point .label , via_access_point .name )
275
- via .via_uri = via_access_point .uri
296
+ append_path_item_with_access_point (
297
+ dp_journey .sections [- 1 ].street_network .path_items ,
298
+ pt_object .stop_point ,
299
+ via_access_point .access_point ,
300
+ )
276
301
277
302
elif fallback_type == StreetNetworkPathType .ENDING_FALLBACK :
278
303
dp_journey .sections [- 1 ].end_date_time += traversal_time
279
304
dp_journey .sections [- 1 ].duration += traversal_time
280
305
dp_journey .sections [- 1 ].street_network .duration += traversal_time
281
306
dp_journey .sections [- 1 ].street_network .length += length
282
307
283
- path_items = dp_journey .sections [0 ].street_network .path_items
284
-
285
- via = dp_journey .sections [0 ].street_network .path_items .add ()
286
- via .duration = traversal_time
287
- via .length = length
288
- via .name = via_access_point .name
289
- # Use label in stead of name???
290
- via .instruction = u"Exit {} via {}." .format (pt_object .stop_point .label , via_access_point .name )
291
- via .via_uri = via_access_point .uri
292
-
293
- # we cannot insert an element at the beginning of a list :(
294
- # a little algo to move the last element to the beginning
295
- tmp_item = response_pb2 .PathItem ()
296
- for i in range (len (path_items )):
297
- tmp_item .CopyFrom (path_items [i ])
298
- path_items [i ].CopyFrom (path_items [- 1 ])
299
- path_items [- 1 ].CopyFrom (tmp_item )
308
+ prepend_path_item_with_access_point (
309
+ dp_journey .sections [0 ].street_network .path_items , pt_object .stop_point , via_access_point .access_point
310
+ )
300
311
301
312
302
313
def _update_fallback_sections (journey , fallback_dp , fallback_period_extremity , fallback_type , via_access_point ):
@@ -766,3 +777,49 @@ def complete_transfer(pt_journey, transfer_pool):
766
777
if section .type != response_pb2 .TRANSFER :
767
778
continue
768
779
transfer_pool .wait_and_complete (section )
780
+
781
+
782
+ def is_valid_direct_path_streetwork (dp ):
783
+ if not dp or not dp .journeys or not dp .journeys [0 ].sections :
784
+ return False
785
+
786
+ # a valid journey's must comprise at least two coordinates
787
+ nb_coords = sum ((len (sec .street_network .coordinates ) for sec in dp .journeys [0 ].sections ))
788
+ if nb_coords < 2 :
789
+ return False
790
+ return True
791
+
792
+
793
+ def prepend_first_coord (dp , pt_obj ):
794
+ """
795
+ prepend pt_object's coord to journeys' coordinates (geojson)
796
+ """
797
+ if not is_valid_direct_path_streetwork (dp ):
798
+ return
799
+
800
+ starting_coords = dp .journeys [0 ].sections [0 ].street_network .coordinates
801
+ # we are inserting the coord of the origin at the beginning of the geojson
802
+ coord = get_pt_object_coord (pt_obj )
803
+ if starting_coords and coord != starting_coords [0 ]:
804
+ starting_coords .add (lon = coord .lon , lat = coord .lat )
805
+ # we cannot insert an element at the beginning of a list :(
806
+ # a little algo to move the last element to the beginning
807
+ tmp = type_pb2 .GeographicalCoord ()
808
+ for i in range (len (starting_coords )):
809
+ tmp .CopyFrom (starting_coords [i ])
810
+ starting_coords [i ].CopyFrom (starting_coords [- 1 ])
811
+ starting_coords [- 1 ].CopyFrom (tmp )
812
+
813
+
814
+ def append_last_coord (dp , pt_obj ):
815
+ """
816
+ append pt_object's coord to journeys' coordinates (geojson)
817
+ """
818
+ if not is_valid_direct_path_streetwork (dp ):
819
+ return
820
+
821
+ ending_coords = dp .journeys [0 ].sections [- 1 ].street_network .coordinates
822
+ # we are appending the coord of the destination at the end of the geojson
823
+ coord = get_pt_object_coord (pt_obj )
824
+ if ending_coords and coord != ending_coords [- 1 ]:
825
+ ending_coords .add (lon = coord .lon , lat = coord .lat )
0 commit comments