-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_osm.py
More file actions
1651 lines (1435 loc) · 83.5 KB
/
import_osm.py
File metadata and controls
1651 lines (1435 loc) · 83.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Parses and processes OpenStreetMap (OSM) railway data for the camera localization pipeline.
Defines classes for track segments, nodes, and the railway map.
Provides methods for extracting, visualizing, and analyzing railway network data from OSM files.
Author: Cornelius von Einem, ETH Zurich - project supervisor for Semester Project related to his PhD research
"""
import xml.etree.ElementTree as ET
import utm
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as patches
from matplotlib.lines import Line2D
import matplotlib.path as mplPath
from matplotlib.colors import ListedColormap
# from ekf import ExtendedPathConstrainedKF
import plotly.graph_objects as go
from itertools import combinations
import re
from tqdm import tqdm
from collections import namedtuple
from math import sqrt
class track_segment:
"""
Represents a railway track segment with nodes, coordinates, and properties.
Contains information about track geometry, speed limits, and connectivity
for use in the camera localization pipeline.
"""
def __init__(self, id, osm_id):
"""
Initializes a track segment
:param id: track segment id
:type id: int
:param osm_id: osm id
:type osm_id: str
"""
self.id = id # Track ID (also index in list of tracks)
self.osm_id = osm_id # OSM Track ID (not needed)
self.track_node_ids = [] # ordered list of nodes along track
self.x_cors = [] # Coordinates of nodes
self.y_cors = []
self.arc_length = [] # Integrated arclength along track
self.curvature = [] # not used
self.max_speed = 0 # Speedlimit from OSM
self.type = '' # not used,
self.num_tracks = 1 # Issue with OSM map, where multiple tracks are represented as one. Nothing I can do
self.is_transition = False # Transition segment or not
self.transitions = [] # Transition Regions
self.is_bridge = False
def print(self):
print("---")
print(("ID: ").ljust(27, ' ') + str(self.id))
print(("OSM-ID: ").ljust(27, ' ') + self.osm_id)
print(("Type: ").ljust(27, ' ') + self.type)
print(("Speed: ").ljust(27, ' ') + str(self.max_speed))
print(("Number of Tracks: ").ljust(27, ' ') + str(self.num_tracks))
print(("Type: ").ljust(27, ' ') + self.type)
print(("Nodes: ").ljust(27, ' ') + str(self.track_node_ids))
print(("X: ").ljust(27, ' ') + str(self.x_cors))
print(("Y: ").ljust(27, ' ') + str(self.y_cors))
def plot_segment(self,axis,color):
axis.plot(self.x_cors,self.y_cors,c=color)
axis.scatter(self.x_cors[0],self.y_cors[0],c=color)
axis.scatter(self.x_cors[-1],self.y_cors[-1],c=color)
# Node that defines tracks
class track_node:
"""
Represents a railway track node with coordinates and properties.
Contains information about node geometry, position, and connectivity
for use in the camera localization pipeline.
"""
def __init__(self,id,osm_id):
self.id = id # ID and index
self.osm_id = osm_id # OSM ID, not used
self.lat = 0.0
self.lon = 0.0
self.x = 0.0 #UTM coordinates
self.y = 0.0
self.zone = ''
def print(self):
print("---")
print(("ID: ").ljust(27, ' ') + str(self.id))
print(("OSM-ID: ").ljust(27, ' ') + str(self.osm_id))
print(("Lat: ").ljust(27, ' ') + self.lat)
print(("Lon: ").ljust(27, ' ') + self.lon)
print(("X: ").ljust(27, ' ') + str(self.x))
print(("Y: ").ljust(27, ' ') + str(self.y))
print(("Zone: ").ljust(27, ' ') + self.zone)
# print(("Nodes: ").ljust(27, ' ') + str(self.nodes))
def assign_lat_long(self,lat,lon):
# Convert lat,lon to UTM
self.lat = lat
self.lon = lon
u = utm.from_latlon(float(lat), float(lon))
self.x = u[0]
self.y = u[1]
self.zone = str(u[2]) + u[3]
# Whole Map object
class railway_map:
"""
Represents the entire railway map with nodes, tracks, and transitions.
Contains information about the railway network, including track geometry,
node connectivity, and transition segments.
"""
def __init__(self,name):
self.name = name # Name
self.file = "" # OSM Filename
self.osm_track_ids = [] # List of osm track ids (not used)
self.osm_node_ids = [] # List of osm node ids (not used)
self.nodes_to_segment_assignment = [] # Assignment list of which nodes belong to which tracks
self.segment_to_node_assignment = [] # Assignment of which tracks connect to a node
self.direct_neighbours_of_nodes = [] # Which nodes are directly connected to each other
self.edge_to_edge_connectivits = [] # Which tracks are directly connected to each other
self.railway_tracks = [] # Acutal list of tracks
self.railway_nodes = [] # Actual list of nodes
self.transition_length = 0 # Length of a transition segment (actual half length, length per track that is connected)
def print(self):
print(self.name, " - " , self.file)
# Plotting OSM Railway data
def plotly(self,figure):
# Plot with plotly
end_points_x = []
end_points_y = []
map_fig = go.Figure()
# Get Zone
zone_num = re.findall(r'\d+', self.railway_nodes[0].zone)[0]
zone_letter = re.findall(r'[a-zA-Z]', self.railway_nodes[0].zone)[0]
for way in self.railway_tracks:
if not way.is_transition:
end_points_x.append(way.x_cors[0])
end_points_x.append(way.x_cors[-1])
end_points_y.append(way.y_cors[0])
end_points_y.append(way.y_cors[-1])
lat = []
lon = []
for i in range(len(way.x_cors)):
x_p = way.x_cors[i]
y_p = way.y_cors[i]
res = utm.to_latlon(x_p, y_p, int(zone_num), zone_letter)
lat.append(res[0])
lon.append(res[1])
map_fig.add_trace(go.Scattermapbox(
mode = "lines",
lon = lon,
lat = lat,
marker = {'size': 10},
line=dict(color='blue', width=2)))
figure.add_trace(go.Scatter(x=way.x_cors, y=way.y_cors, name='Segment '+str(way.id),mode='lines',line=dict(color='royalblue', width=2),showlegend=False))
# figure.add_trace(go.Scatter(x=way.x_cors, y=way.y_cors, name='Segment '+str(way.id),mode='lines',line=dict(color='royalblue', width=2),showlegend=True))
# figure.add_trace(go.Scatter(x=way.x_cors, y=way.y_cors, name='Segment '+str(way.id),mode='lines',line=dict(color='royalblue', width=2),showlegend=False,line_shape='spline'))
figure.add_trace(go.Scatter(x=end_points_x, y=end_points_y, name='Segment-End-Points',mode='markers',line=dict(color='royalblue', width=2),showlegend=True))
map_fig.add_trace(go.Scattermapbox(
mode="markers",
lon=[end_points_x],
lat=[end_points_y],
marker={'size': 10},
line=dict(color='red', width=2)))
map_fig.update_layout(
margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
# 'center': {'lon': 10, 'lat': 10},
'style': "stamen-terrain",
'center': {'lat': 47.367, 'lon': 8.54},
'zoom': 10})
map_fig.show()
# Generate transition segments
def generate_transitions(self, trans_len, figure=None):
"""
Generate transition segments
:param trans_len: transition segment length
:type trans_len: int
:param figure: plot if figure is insterted
:type figure: plotly figure
"""
self.transition_length = trans_len
transition_length = self.transition_length # Length of transition space on each track, in meters
# Find all transition points
transition_point_ids = [ x for x in range(len(self.nodes_to_segment_assignment)) if len(self.nodes_to_segment_assignment[x]) > 1]
# print(transition_point_ids)
transition_point_x = [self.railway_nodes[x].x for x in transition_point_ids]
transition_point_y = [self.railway_nodes[x].y for x in transition_point_ids]
if figure is not None:
# Plot transition points
figure.add_trace(go.Scatter(x=transition_point_x, y=transition_point_y, name='Transition Points',mode='markers',line=dict(color='red', width=2),showlegend=True,visible = "legendonly"))
# Determine which segments to create
segments_to_generate = []
for transition_point in transition_point_ids:
if transition_point == 2635:
h = 1+2
# Get all possible combinations of transitions at an intersection point
comb = combinations(self.nodes_to_segment_assignment[transition_point], 2)
# print("---")
# print(self.nodes_to_segment_assignment[transition_point])
for com in comb:
segment = [transition_point,com[0],com[1]]
# print(segment)
segments_to_generate.append(segment)
# print(com[1])
# print(len(segments_to_generate))
# Create transition segments
generated_segments = []
for trans_seg in segments_to_generate:
# id of point, origin track, target track
point_id = trans_seg[0]
track_1_id = trans_seg[1]
track_2_id = trans_seg[2]
if track_1_id == 910 or track_2_id == 910:
h = 1+1
# Point on Track 1
x_0 = []
y_0 = []
arc_0 = []
# Transition Point
x_1 = self.railway_nodes[point_id].x
y_1 = self.railway_nodes[point_id].y
center_point_idx = self.railway_tracks[track_1_id].track_node_ids.index(point_id)
arc_1_1 = self.railway_tracks[track_1_id].arc_length[center_point_idx]
center_point_idx = self.railway_tracks[track_2_id].track_node_ids.index(point_id)
arc_1_2 = self.railway_tracks[track_2_id].arc_length[center_point_idx]
# Point on Track 2
x_2 = []
y_2 = []
arc_2 = []
# 3 possibilities, the point can be at the beginning, middle or end of each segment...
# Find the next point on track 1 that connects to the transition point. needed to define transition segment shape
# First point
if point_id == self.railway_tracks[track_1_id].track_node_ids[0]:
# print("Beginning Point")
track_position = 0 + transition_length
x_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
arc_0.append(track_position)
if point_id == self.railway_tracks[track_1_id].track_node_ids[-1]:
# print("End Point")
track_position = self.railway_tracks[track_1_id].arc_length[-1] - transition_length
x_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
arc_0.append(track_position)
if point_id != self.railway_tracks[track_1_id].track_node_ids[0] and point_id != self.railway_tracks[track_1_id].track_node_ids[-1]:
# print("Center Point, I will deal with this later")
arc_len_sub = arc_1_1 - transition_length
arc_len_add = arc_1_1 + transition_length
arc_0.append(arc_len_sub)
arc_0.append(arc_len_add)
x_0.append(np.interp(arc_len_sub, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(arc_len_sub, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
x_0.append(np.interp(arc_len_add, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(arc_len_add, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
# Find the next point on track 1 that connects to the transition point. needed to define transition segment shape
# Second Point
if point_id == self.railway_tracks[track_2_id].track_node_ids[0]:
# print("Beginning Point")
track_position = 0 + transition_length
x_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
arc_2.append(track_position)
if point_id == self.railway_tracks[track_2_id].track_node_ids[-1]:
# print("End Point")
track_position = self.railway_tracks[track_2_id].arc_length[-1] - transition_length
x_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
arc_2.append(track_position)
if point_id != self.railway_tracks[track_2_id].track_node_ids[0] and point_id != self.railway_tracks[track_2_id].track_node_ids[-1]:
# print("Center Point, I will deal with this later")
arc_len_sub = arc_1_2 - transition_length
arc_len_add = arc_1_2 + transition_length
arc_2.append(arc_len_sub)
arc_2.append(arc_len_add)
x_2.append(np.interp(arc_len_sub, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(arc_len_sub, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
x_2.append(np.interp(arc_len_add, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(arc_len_add, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
# Find all combinations
for i in range(len(x_0)):
for j in range(len(x_2)):
# Check angle acceptable, feasable for a train:
is_acceptable, angle = is_angle_acceptable([x_1,y_1], [x_0[i],y_0[i]], [x_2[j],y_2[j]])
if is_acceptable:
x_points = [x_0[i],x_1,x_2[j]]
y_points = [y_0[i],y_1,y_2[j]]
transition_1 = [np.min([arc_0[i],arc_1_1]),np.max([arc_0[i],arc_1_1]),track_1_id]
transition_2 = [np.min([arc_2[j],arc_1_2]),np.max([arc_2[j],arc_1_2]),track_2_id]
generated_segments.append([x_points,y_points,transition_1,transition_2,angle])
# print(generated_segments)
# Clear Edge-to-Edge Connectivity
# Because we are working with transition segments, we don't need the normal connectivity graph
# Instead, each connection is to a transition, and from there to the next edge, so we have to redo the graph
self.edge_to_edge_connectivits.clear()
total_num_tracks = len(self.railway_tracks) + len(generated_segments)
# Generate empty edges
for i in range(total_num_tracks):
self.edge_to_edge_connectivits.append([])
plot_transition_x = []
plot_transition_y = []
for segment in generated_segments:
# print(segment)
new_id = len(self.railway_tracks)
# print("--")
# print(new_id)
# print(segment[4])
for i in segment[0]:
plot_transition_x.append(i)
for i in segment[1]:
plot_transition_y.append(i)
plot_transition_x.append(np.nan)
plot_transition_y.append(np.nan)
if figure is not None:
figure.add_trace(go.Scatter(x=segment[0], y=segment[1], name='Trans ' + str(new_id),mode='lines',line=dict(color='red', width=2)))
# Create Segment
new_transition_track = track_segment(new_id,"0")
new_transition_track.is_transition = True
# # new_transition_track.track_node_ids = []
new_transition_track.x_cors = segment[0]
new_transition_track.y_cors = segment[1]
new_transition_track.arc_length = [0,transition_length,2*transition_length]
# # new_transition_track.curvature = []
new_transition_track.transitions.append([0,transition_length,segment[2][2]])
new_transition_track.transitions.append([transition_length,2*transition_length,segment[3][2]])
# print(new_transition_track.transitions)
self.railway_tracks.append(new_transition_track)
# Add Transition Data to existing track
self.railway_tracks[segment[2][2]].transitions.append([segment[2][0],segment[2][1],new_id])
self.railway_tracks[segment[3][2]].transitions.append([segment[3][0],segment[3][1],new_id])
# Fillind edge_to_edge connectivity graph
self.edge_to_edge_connectivits[new_id].append(segment[2][2])
self.edge_to_edge_connectivits[new_id].append(segment[3][2])
self.edge_to_edge_connectivits[segment[2][2]].append(new_id)
self.edge_to_edge_connectivits[segment[3][2]].append(new_id)
if figure is not None:
figure.add_trace(go.Scatter(x=plot_transition_x, y=plot_transition_y, name='Transitions ',mode='lines',line=dict(color='red', width=2),visible = "legendonly"))
def circles_from_p1p2r(self, p1, p2, r):
"""
Calculate arc for curved transition segments
:param p1: point 1
:type p1: np.array
:param p2: point2
:type p2: np.array
:param r: radius
:type r: float
:return:
:rtype:
"""
Pt = namedtuple('Pt', 'x, y')
Circle = Cir = namedtuple('Circle', 'x, y, r')
'Following explanation at http://mathforum.org/library/drmath/view/53027.html'
if r == 0.0:
raise ValueError('radius of zero')
(x1, y1), (x2, y2) = p1, p2
if p1 == p2:
raise ValueError('coincident points gives infinite number of Circles')
# delta x, delta y between points
dx, dy = x2 - x1, y2 - y1
# dist between points
q = sqrt(dx ** 2 + dy ** 2)
if q > 2.0 * r:
raise ValueError('separation of points > diameter')
# halfway point
x3, y3 = (x1 + x2) / 2, (y1 + y2) / 2
# distance along the mirror line
d = sqrt(r ** 2 - (q / 2) ** 2)
# One answer
c1 = Cir(x=x3 - d * dy / q,
y=y3 + d * dx / q,
r=abs(r))
# The other answer
c2 = Cir(x=x3 + d * dy / q,
y=y3 - d * dx / q,
r=abs(r))
return c1, c2
def circle_func(self, x_points, radius, center):
y_new_1 = [-np.sqrt(radius ** 2 - (x - center[0]) ** 2) + center[1] for x in x_points]
y_new_2 = [np.sqrt(radius ** 2 - (x - center[0]) ** 2) + center[1] for x in x_points]
return y_new_1, y_new_2
def find_candidate(self, x_points, node_point, circles):
distances = []
for circle in range(len(circles)):
d = 0
for point in range(len(x_points)):
d += np.sqrt((x_points[point] - node_point[0]) ** 2 + (circles[circle][point] - node_point[1]) ** 2)
distances.append(d)
min_arg = np.argmin(distances)
return min_arg
def find_arc_points(self, point1, point2, node_point, radius=1.0, number_points=5):
x_min = np.min([point1[0], point2[0]])
x_max = np.max([point1[0], point2[0]])
c1, c2 = self.circles_from_p1p2r(point1, point2, radius)
x_new = np.linspace(x_min, x_max, number_points)
y_new_c1_1, y_new_c1_2 = self.circle_func(x_new.copy(), c1.r, (c1.x, c1.y))
y_new_c2_1, y_new_c2_2 = self.circle_func(x_new.copy(), c2.r, (c2.x, c2.y))
circle_array = [y_new_c1_1, y_new_c1_2, y_new_c2_1, y_new_c2_2]
index = self.find_candidate(x_new, node_point, circle_array)
return x_new, circle_array[index]
def distance_points(self, point1, point2, smooth=0, floor=True):
dist = np.sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
if floor is True:
if dist < 1:
dist = dist + smooth
else:
dist = np.floor(dist) + smooth
return dist
def generate_transitions_vehicle_bent(self,trans_len, figure=None):
"""
Generate arc transition segments for vehicles
:param trans_len: trans segment length
:type trans_len: int
:param figure: plot if figure is inserted
:type figure: plotly figure
"""
self.transition_length = trans_len
# How long should straight segment be before curve
self.transition_straight = 3
self.transition_length_curve_start = self.transition_length - self.transition_straight
# Find all transition points
transition_point_ids = [ x for x in range(len(self.nodes_to_segment_assignment)) if len(self.nodes_to_segment_assignment[x]) > 1]
# print(transition_point_ids)
transition_point_x = [self.railway_nodes[x].x for x in transition_point_ids]
transition_point_y = [self.railway_nodes[x].y for x in transition_point_ids]
if figure is not None:
# Plot transition points
figure.add_trace(go.Scatter(x=transition_point_x, y=transition_point_y, name='Transition Points',mode='markers',line=dict(color='red', width=2),showlegend=True,visible = "legendonly"))
# Determine which segments to create
segments_to_generate = []
for transition_point in transition_point_ids:
# Get all possible combinations of transitions at an intersection point
comb = combinations(self.nodes_to_segment_assignment[transition_point], 2)
# print("---")
# print(self.nodes_to_segment_assignment[transition_point])
for com in comb:
segment = [transition_point,com[0],com[1]]
# print(segment)
segments_to_generate.append(segment)
# print(com[1])
# print(len(segments_to_generate))
# Create transition segments
generated_segments = []
for trans_seg in segments_to_generate:
# id of point, origin track, target track
point_id = trans_seg[0]
track_1_id = trans_seg[1]
track_2_id = trans_seg[2]
# Point on Track 1
x_0 = []
y_0 = []
arc_0 = []
# Arc start on Track 1
x_0_a = []
y_0_a = []
arc_0_a = []
# Transition Point
x_1 = self.railway_nodes[point_id].x
y_1 = self.railway_nodes[point_id].y
arc_1_1_is_center = False
center_point_idx = self.railway_tracks[track_1_id].track_node_ids.index(point_id)
arc_1_1 = self.railway_tracks[track_1_id].arc_length[center_point_idx] #+ self.transition_length_curve_start
if arc_1_1 == 0:
arc_1_1 = arc_1_1 + self.transition_length_curve_start
elif arc_1_1 > 0:
if center_point_idx == len(self.railway_tracks[track_1_id].arc_length) - 1:
arc_1_1 = arc_1_1 - self.transition_length_curve_start
else:
arc_1_1_is_center = True
arc_1_2_is_center = False
center_point_idx = self.railway_tracks[track_2_id].track_node_ids.index(point_id)
arc_1_2 = self.railway_tracks[track_2_id].arc_length[center_point_idx] #- self.transition_length_curve_start
if arc_1_2 == 0:
#arc_1_2 = arc_1_2 + self.transition_length * (2/3)
arc_1_2 = arc_1_2 + self.transition_length_curve_start
elif arc_1_2 > 0:
if center_point_idx == len(self.railway_tracks[track_2_id].arc_length) - 1:
arc_1_2 = arc_1_2 - self.transition_length_curve_start
else:
arc_1_2_is_center = True
# Point on Track 2
x_2 = []
y_2 = []
arc_2 = []
# Arc start on Track 2
x_2_a = []
y_2_a = []
arc_2_a = []
# 3 possibilities, the point can be at the beginning, middle or end of each segment...
# Find the next point on track 1 that connects to the transition point. needed to define transition segment shape
# First point
if point_id == self.railway_tracks[track_1_id].track_node_ids[0]:
# print("Beginning Point")
#track_position = 0 + transition_length
track_position = self.transition_length
#track_position_arc_start = self.transition_length * (2/3)#self.transition_length_curve_start
track_position_arc_start = self.transition_length_curve_start # self.transition_length_curve_start
x_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
arc_0.append(track_position)
x_0_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
arc_0_a.append(track_position_arc_start)
elif point_id == self.railway_tracks[track_1_id].track_node_ids[-1]:
# print("End Point")
#track_position = self.railway_tracks[track_1_id].arc_length[-1] - transition_length
track_position = self.railway_tracks[track_1_id].arc_length[-1] - self.transition_length
track_position_arc_start = self.railway_tracks[track_1_id].arc_length[-1] - self.transition_length_curve_start
x_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(track_position, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
arc_0.append(track_position)
x_0_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
arc_0_a.append(track_position_arc_start)
else:
# print("Center Point, I will deal with this later")
arc_len_sub = arc_1_1 - self.transition_length
arc_len_add = arc_1_1 + self.transition_length
arc_0.append(arc_len_sub)
arc_0.append(arc_len_add)
#arc_len_sub_a = arc_1_1 - self.transition_length*(2/3)
arc_len_sub_a = arc_1_1 - self.transition_length_curve_start
#arc_len_add_a = arc_1_1 + self.transition_length*(2/3)
arc_len_add_a = arc_1_1 + self.transition_length_curve_start
arc_0_a.append(arc_len_sub_a)
arc_0_a.append(arc_len_add_a)
x_0.append(np.interp(arc_len_sub, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(arc_len_sub, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
x_0.append(np.interp(arc_len_add, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0.append(np.interp(arc_len_add, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
x_0_a.append(np.interp(arc_len_sub_a, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0_a.append(np.interp(arc_len_sub_a, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
x_0_a.append(np.interp(arc_len_add_a, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].x_cors))
y_0_a.append(np.interp(arc_len_add_a, self.railway_tracks[track_1_id].arc_length, self.railway_tracks[track_1_id].y_cors))
# Find the next point on track 1 that connects to the transition point. needed to define transition segment shape
# Second Point
if point_id == self.railway_tracks[track_2_id].track_node_ids[0]:
# print("Beginning Point")
track_position = self.transition_length
track_position_arc_start = self.transition_length_curve_start # self.transition_length_curve_start
x_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
arc_2.append(track_position)
x_2_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_2_id].arc_length,
self.railway_tracks[track_2_id].x_cors))
y_2_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_2_id].arc_length,
self.railway_tracks[track_2_id].y_cors))
arc_2_a.append(track_position_arc_start)
elif point_id == self.railway_tracks[track_2_id].track_node_ids[-1]:
track_position = self.railway_tracks[track_2_id].arc_length[-1] - self.transition_length
track_position_arc_start = self.railway_tracks[track_2_id].arc_length[
-1] - self.transition_length_curve_start
x_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(track_position, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
arc_2.append(track_position)
x_2_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2_a.append(np.interp(track_position_arc_start, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
arc_2_a.append(track_position_arc_start)
else:
arc_len_sub = arc_1_2 - self.transition_length
arc_len_add = arc_1_2 + self.transition_length
arc_len_sub_a = arc_1_2 - self.transition_length_curve_start
arc_len_add_a = arc_1_2 + self.transition_length_curve_start
arc_2.append(arc_len_sub)
arc_2.append(arc_len_add)
x_2.append(np.interp(arc_len_sub, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(arc_len_sub, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
x_2.append(np.interp(arc_len_add, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2.append(np.interp(arc_len_add, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
arc_2_a.append(arc_len_sub_a)
arc_2_a.append(arc_len_add_a)
x_2_a.append(np.interp(arc_len_sub_a, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2_a.append(np.interp(arc_len_sub_a, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
x_2_a.append(np.interp(arc_len_add_a, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].x_cors))
y_2_a.append(np.interp(arc_len_add_a, self.railway_tracks[track_2_id].arc_length, self.railway_tracks[track_2_id].y_cors))
# Find all combinations
for i in range(len(x_0)):
for j in range(len(x_2)):
# Check angle acceptable, feasable for a train:
is_acceptable, angle = is_angle_acceptable([x_1,y_1], [x_0[i],y_0[i]], [x_2[j],y_2[j]])
is_acceptable =True
if is_acceptable:
x_points = [x_0[i],x_1,x_2[j]]
y_points = [y_0[i],y_1,y_2[j]]
x_points_arc = [x_0_a[i], x_1, x_2_a[j]]
y_points_arc = [y_0_a[i], y_1, y_2_a[j]]
if arc_1_1_is_center is True:
if i == 0:
transition_1 = [np.min([arc_0[i],arc_1_1 - self.transition_length_curve_start ]),np.max([arc_0[i],arc_1_1 - self.transition_length_curve_start]),track_1_id]
else:
transition_1 = [np.min([arc_0[i],arc_1_1 + - self.transition_length_curve_start]),np.max([arc_0[i],arc_1_1 + - self.transition_length_curve_start]),track_1_id]
else:
transition_1 = [np.min([arc_0[i],arc_1_1]),np.max([arc_0[i],arc_1_1]),track_1_id]
if arc_1_2_is_center is True:
if j == 0:
transition_2 = [np.min([arc_2[j],arc_1_2 - self.transition_length_curve_start]),np.max([arc_2[j],arc_1_2 - self.transition_length_curve_start]),track_2_id]
else:
transition_2 = [np.min([arc_2[j], arc_1_2 + self.transition_length_curve_start]),
np.max([arc_2[j], arc_1_2 + self.transition_length_curve_start]),
track_2_id]
else:
transition_2 = [np.min([arc_2[j],arc_1_2]),np.max([arc_2[j],arc_1_2]),track_2_id]
generated_segments.append([x_points,y_points,transition_1,transition_2,angle, x_points_arc, y_points_arc])
# print(generated_segments)
# Clear Edge-to-Edge Connectivity
# Because we are working with transition segments, we don't need the normal connectivity graph
# Instead, each connection is to a transition, and from there to the next edge, so we have to redo the graph
self.edge_to_edge_connectivits.clear()
total_num_tracks = len(self.railway_tracks) + len(generated_segments)
# Generate empty edges
for i in range(total_num_tracks):
self.edge_to_edge_connectivits.append([])
plot_transition_x = []
plot_transition_y = []
for segment in generated_segments:
# print(segment)
new_id = len(self.railway_tracks)
point1 = [segment[0][0], segment[1][0]]
point2 = [segment[0][2], segment[1][2]]
point1_arc = [segment[5][0], segment[6][0]]
point2_arc = [segment[5][2], segment[6][2]]
middle_node = [segment[0][1], segment[1][1]]
angle = segment[4]
if point1_arc != point2_arc and angle < 130 and angle > 50:
try:
circle_points_x, circle_points_y = self.find_arc_points(point1_arc, point2_arc, middle_node,
radius=self.distance_points(point1_arc, point2_arc, 1.5),
number_points=10)
except:
h = 1+1
whole_segment_x = circle_points_x
whole_segment_y = circle_points_y
first_circle_point = [circle_points_x[0], circle_points_y[0]]
last_circle_point = [circle_points_x[-1], circle_points_y[-1]]
segment_distance_to_circle_arc1 = [self.distance_points(point1, first_circle_point, floor=False), self.distance_points(point1, last_circle_point, floor=False)]
point_closest = np.argmin(segment_distance_to_circle_arc1)
if point_closest == 0:
whole_segment_x = np.insert(circle_points_x, 0, point1[0])
whole_segment_x = np.insert(whole_segment_x, whole_segment_x.shape[0], point2[0])
whole_segment_y = np.insert(circle_points_y, 0, point1[1])
whole_segment_y = np.insert(whole_segment_y, whole_segment_y.shape[0], point2[1])
else:
whole_segment_x = np.insert(circle_points_x, 0, point2[0])
whole_segment_x = np.insert(whole_segment_x, whole_segment_x.shape[0], point1[0])
whole_segment_y = np.insert(circle_points_y, 0, point2[1])
whole_segment_y = np.insert(whole_segment_y, whole_segment_y.shape[0], point1[1])
else:
whole_segment_x = np.array([point1[0], point1_arc[0], point2_arc[0], point2[0]])
whole_segment_y = np.array([point1[1], point1_arc[1], point2_arc[1], point2[1]])
plot_transition_x.append(point1[0])
plot_transition_x.append(point2[0])
plot_transition_y.append(point1[1])
plot_transition_y.append(point2[1])
plot_transition_x.append(np.nan)
plot_transition_y.append(np.nan)
if figure is not None:
figure.add_trace(go.Scatter(x=whole_segment_x, y=whole_segment_y, name='Trans ' + str(new_id),mode='lines',line=dict(color='red', width=2)))
# Create Segment
new_transition_track = track_segment(new_id,"0")
new_transition_track.is_transition = True
# # new_transition_track.track_node_ids = []
new_transition_track.x_cors = whole_segment_x#[segment[0][0], segment[0][2]]
new_transition_track.y_cors = whole_segment_y#[segment[1][0], segment[1][2]]
length = [0]
total_length = 0
for i in range(len(whole_segment_x) - 1):
total_length_temp = np.sqrt((whole_segment_x[i + 1] - whole_segment_x[i])**2 + (whole_segment_y[i+1] - whole_segment_y[i])**2)
length.append(total_length + total_length_temp)
total_length = total_length + total_length_temp
# new_transition_track.x_cors = whole_segment_x[::-1] # [segment[0][0], segment[0][2]]
# new_transition_track.y_cors = whole_segment_y[::-1] # [segment[1][0], segment[1][2]]
# length = length[::-1]
new_transition_track.arc_length = length#[0, 2*transition_length]#[0,transition_length,2*transition_length]
# # new_transition_track.curvature = []
#new_transition_track.transitions.append([self.transition_length_curve_start,self.transition_length,segment[2][2]])
if point1_arc[0] > point2_arc[0]:
new_transition_track.transitions.append([0, self.transition_length_curve_start, segment[3][2]])
new_transition_track.transitions.append([length[-1] - self.transition_length_curve_start,length[-1],segment[2][2]])
else:
new_transition_track.transitions.append([0, self.transition_length_curve_start, segment[2][2]])
new_transition_track.transitions.append(
[length[-1] - self.transition_length_curve_start, length[-1], segment[3][2]])
# print(new_transition_track.transitions)
self.railway_tracks.append(new_transition_track)
# Add Transition Data to existing track
add_info = self.railway_tracks[segment[2][2]]
add_transitions = self.railway_tracks[segment[2][2]].transitions
self.railway_tracks[segment[2][2]].transitions.append([segment[2][0],segment[2][1],new_id])
self.railway_tracks[segment[3][2]].transitions.append([segment[3][0],segment[3][1],new_id])
# Fillind edge_to_edge connectivity graph
self.edge_to_edge_connectivits[new_id].append(segment[2][2])
self.edge_to_edge_connectivits[new_id].append(segment[3][2])
self.edge_to_edge_connectivits[segment[2][2]].append(new_id)
self.edge_to_edge_connectivits[segment[3][2]].append(new_id)
#figure.add_trace(go.Scatter(x=plot_transition_x, y=plot_transition_y, name='Transitions ',mode='lines',line=dict(color='red', width=2),visible = "legendonly"))
# Importing OSM file for cars
def import_from_osm_file_vehicles(self,file_name):
"""
Importing OSM file for cars
:param file_name: map filename
:type file_name: str
"""
self.file = file_name
print("Loading OSM data...")
# railway_types_to_ignore = ['disused','abandoned','construction', 'platform', 'rail']
# Ignore elements with these keys
# Ignore 'rail' when dealing with tram, and vice versa
railway_types_to_ignore = ['pedestrian', 'service', 'track', 'bus_guideway', 'escape', 'raceway', 'busway',
'tram', 'rail', 'disused','abandoned','construction', 'tram_stop', 'preserved',
'station', 'subway_entrance', 'halt', 'platform','miniature','turntable',
'traverser', 'monorail', 'narrow_gauge', 'crossing', 'level_crossing',
'buffer_stop', 'signal', 'proposed', 'footway', 'bridleway', 'steps', 'corridor',
'sidewalk', 'crossing', 'cycleway', 'share_busway', 'opposite_share_busway',
'unclassified' , 'residential']
need = ["primary", "secondary", "tertiary", "residential"]
railway_types_to_ignore = ['pedestrian', 'service', 'track', 'bus_guideway', 'escape', 'raceway', 'busway',
'tram', 'rail', 'disused','abandoned','construction', 'tram_stop', 'preserved',
'station', 'subway_entrance', 'halt', 'platform','miniature','turntable',
'traverser', 'monorail', 'narrow_gauge', 'crossing', 'level_crossing',
'buffer_stop', 'signal', 'proposed', 'footway', 'bridleway', 'steps', 'corridor',
'sidewalk', 'crossing', 'cycleway', 'share_busway', 'opposite_share_busway',
'trunk', 'motorway', 'unclassified', 'living_street',
'road', 'footway', 'bridleway', 'steps', 'corridor', 'path'
]
# Parse xml tree
tree = ET.parse(self.file)
print("File parsed...")
root = tree.getroot()
# Do first parsing pass to find all tracks
for way in tqdm(root.findall('way')):
tag_node_list = [x.tag for x in way]
for node in way:
if (node.tag != 'nd') and (node.tag != 'tag'):
print("Some unexpected nodes showed up:", node.tag)
is_railway = False
max_speed = 0
type = ''
num_tracks = 1
tagslist = [x.attrib['k'] for x in way.findall('tag')]
for node in way.findall('tag'):
# These are Attributes
if node.attrib.get('k') == 'highway':
# print('This is a railway')
is_railway = True
type = node.attrib.get('v')
if node.attrib['k'] == 'maxspeed':
max_speed = node.attrib.get('v')
try:
max_speed = int(max_speed)
except:
pass
if node.attrib['k'] == 'tracks':
try:
num_tracks = int(node.attrib.get('v'))
except:
pass
# print('Tracks: ',num_tracks)
if num_tracks > 1:
print("WARNING!! INSUFFICITENT TRACK DATA (multiple tracks in one way)")
if is_railway and not type in railway_types_to_ignore:
# Storing this as a track in osm_track_id_database
osm_way_id = way.attrib.get('id')
# If it doeasn't exist yes, create it
if not osm_way_id in self.osm_track_ids:
self.osm_track_ids.append(osm_way_id)
self.segment_to_node_assignment.append([])
self.edge_to_edge_connectivits.append([])
way_index = self.osm_track_ids.index(osm_way_id) # assign an index
new_track = track_segment(way_index,osm_way_id) # create track object
new_track.max_speed = max_speed
new_track.type = type
new_track.num_tracks = num_tracks
self.railway_tracks.append(new_track) # add to map object
# print(way_id)
else:
print("Saw this track before: ", osm_way_id)
way_index = self.osm_track_ids.index(osm_way_id)
# These are Way-Points that need to be parsed. they will be added to the track data later after parsing
for node in way.findall('nd'):
osm_node_id = node.attrib.get('ref')
# If node is new
if not osm_node_id in self.osm_node_ids:
# print("New Node: ", node_id)
self.osm_node_ids.append(osm_node_id)
node_index = self.osm_node_ids.index(osm_node_id) # Assign node index
self.nodes_to_segment_assignment.append([]) # Create placeholdes in tables
self.direct_neighbours_of_nodes.append([])
new_node_entry = track_node(node_index,osm_node_id) # create node
self.railway_nodes.append(new_node_entry) # Add to map
self.segment_to_node_assignment[way_index].append(node_index) # Fill tables
self.nodes_to_segment_assignment[node_index].append(way_index)
else:
node_index = self.osm_node_ids.index(osm_node_id)
self.segment_to_node_assignment[way_index].append(node_index)
self.nodes_to_segment_assignment[node_index].append(way_index)
# Import all the node Geometry
print("Found tracks...")
for node in tqdm(root.findall('node')):
osm_id = node.attrib['id']
if osm_id in self.osm_node_ids:
lat = node.attrib['lat']
lon = node.attrib['lon']
node_index = self.osm_node_ids.index(osm_id)
if node_index != self.railway_nodes[node_index].id:
print("NODE ID ALERT")
if osm_id != self.railway_nodes[node_index].osm_id:
print("NODE OSM-ID ALERT")
self.railway_nodes[node_index].assign_lat_long(lat,lon) # assigning geographic points to each node
# Now add track geometry to tracks:
for way in tqdm(root.findall('way')):
osm_way_id = way.attrib['id']
if osm_way_id in self.osm_track_ids:
way_index = self.osm_track_ids.index(osm_way_id)
list_of_contained_osm_node_ids = []
for node in way.findall('nd'):
list_of_contained_osm_node_ids.append(node.attrib['ref']) # get all node ids for this track
for i in range(len(list_of_contained_osm_node_ids)):
node_index = self.osm_node_ids.index(list_of_contained_osm_node_ids[i]) # get current node
if not node_index in self.segment_to_node_assignment[way_index]:
print("This node doesn't belong here?")
self.railway_tracks[way_index].track_node_ids.append(node_index) # Assign node to track
self.railway_tracks[way_index].x_cors.append(self.railway_nodes[node_index].x)
self.railway_tracks[way_index].y_cors.append(self.railway_nodes[node_index].y)
# Set Edge Connectivity DATA
for reachable_segment in self.nodes_to_segment_assignment[node_index]:
if reachable_segment != way_index:
if not reachable_segment in self.edge_to_edge_connectivits[way_index]:
self.edge_to_edge_connectivits[way_index].append(reachable_segment)
# print("....")
# print(way_index)
# print(reachable_segment)
# Set node Neighborhood Data
if i > 0:
neighbor_index = self.osm_node_ids.index(list_of_contained_osm_node_ids[i-1])
if not neighbor_index in self.direct_neighbours_of_nodes[node_index]:
self.direct_neighbours_of_nodes[node_index].append(neighbor_index)
if i < len(list_of_contained_osm_node_ids)-1:
neighbor_index = self.osm_node_ids.index(list_of_contained_osm_node_ids[i+1])
if not neighbor_index in self.direct_neighbours_of_nodes[node_index]:
self.direct_neighbours_of_nodes[node_index].append(neighbor_index)
# Computing Track ArcLength
for way in tqdm(self.railway_tracks):
# print(way.id)
way.arc_length.append(0.0)
for i in range(1,len(way.track_node_ids)):
distance = np.sqrt((way.x_cors[i]-way.x_cors[i-1])**2 + (way.y_cors[i]-way.y_cors[i-1])**2)
new_total = way.arc_length[-1] + distance
way.arc_length.append(new_total)
# Importing OSM file
def import_from_osm_file(self,file_name):
self.file = file_name
print("Loading OSM data...")
# railway_types_to_ignore = ['disused','abandoned','construction', 'platform', 'rail']
# Ignore elements with these keys
# Ignore 'rail' when dealing with tram, and vice versa
railway_types_to_ignore = ['rail', 'disused','abandoned','construction', 'preserved', 'station', 'subway_entrance', 'halt', 'platform','miniature','turntable','traverser', 'monorail', 'narrow_gauge', 'crossing', 'level_crossing', 'buffer_stop', 'signal', 'proposed']
# Parse xml tree
tree = ET.parse(self.file)
print("File parsed...")
root = tree.getroot()
# Do first parsing pass to find all tracks
for way in root.findall('way'):
for node in way:
if (node.tag != 'nd') and (node.tag != 'tag'):
print("Some unexpected nodes showed up:", node.tag)
is_railway = False
is_bridge = False
max_speed = 0
type = ''