Skip to content

Commit 2aa6107

Browse files
Handle tracks in operations
1 parent ce41553 commit 2aa6107

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

yaramo/operations/split.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Node,
1010
Signal,
1111
Topology,
12+
Track,
1213
Wgs84GeoNode,
1314
)
1415
from .operationshelper import OperationsHelper
@@ -86,6 +87,9 @@ def _add_elements_to_topology(
8687
Split._assign_routes_to_topologies(
8788
topology, topology_a, topology_b, edge_labels, signal_labels
8889
)
90+
Split._assign_tracks_to_topologies(
91+
topology, topology_a, topology_b, edge_labels, new_end_nodes
92+
)
8993

9094
Split._validate_for_data_loss(topology, topology_a, topology_b, split_edges)
9195

@@ -320,6 +324,61 @@ def _assign_routes_to_topologies(
320324
else:
321325
topology_b.add_route(route)
322326

327+
@staticmethod
328+
def _assign_tracks_to_topologies(
329+
topology: Topology,
330+
topology_a: Topology,
331+
topology_b: Topology,
332+
edge_labels: Dict[Edge, Label],
333+
new_end_nodes: Dict[Edge, Tuple[Node, Node]],
334+
):
335+
def _assign_track_by_label(_track: Track, _label: Label):
336+
if _label == Label.A_Topology:
337+
topology_a.add_track(_track)
338+
else:
339+
topology_b.add_track(_track)
340+
341+
for track in topology.tracks.values():
342+
if not new_end_nodes:
343+
# Nothing separated, so tracks stays connected
344+
_assign_track_by_label(track, edge_labels[track.edges[0]])
345+
continue
346+
347+
any_split_edge_in_track: bool = False
348+
for edge in new_end_nodes.keys():
349+
if edge in track.edges:
350+
any_split_edge_in_track = True
351+
if not any_split_edge_in_track:
352+
# No split edge is in track, so track stays connected
353+
_assign_track_by_label(track, edge_labels[track.edges[0]])
354+
continue
355+
356+
# we need to split the track:
357+
current_track = Track(track_type=track.track_type)
358+
for edge in track.get_edges_in_order():
359+
if edge in new_end_nodes.keys():
360+
node_a = new_end_nodes[edge][0]
361+
edge_a = node_a.connected_edge_on_head
362+
edge_a.update_length()
363+
node_b = new_end_nodes[edge][1]
364+
edge_b = node_b.connected_edge_on_head
365+
edge_b.update_length()
366+
367+
if current_track.is_node_in_track(edge_a.get_other_node(node_a)):
368+
current_track.add_edge_section(edge_a, 0.0, edge_a.length)
369+
_assign_track_by_label(current_track, edge_labels[edge_a])
370+
current_track = Track(track_type=track.track_type)
371+
current_track.add_edge_section(edge_b, 0.0, edge_b.length)
372+
_assign_track_by_label(current_track, edge_labels[edge_b])
373+
else:
374+
current_track.add_edge_section(edge_b, 0.0, edge_b.length)
375+
_assign_track_by_label(current_track, edge_labels[edge_b])
376+
current_track = Track(track_type=track.track_type)
377+
current_track.add_edge_section(edge_a, 0.0, edge_a.length)
378+
_assign_track_by_label(current_track, edge_labels[edge_a])
379+
else:
380+
current_track.edge_sections[edge] = track.edge_sections[edge]
381+
323382
@staticmethod
324383
def _assign_missing_elements_to_label(
325384
topology: Topology,

yaramo/operations/union.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def union(
4747
topology_ab.routes.update(topology_b.routes)
4848
topology_ab.vacancy_sections.update(topology_a.vacancy_sections)
4949
topology_ab.vacancy_sections.update(topology_b.vacancy_sections)
50+
topology_ab.tracks.update(topology_a.tracks)
51+
topology_ab.tracks.update(topology_b.tracks)
5052

5153
for node_a, node_b in node_matching.items():
5254
union_edge_a: Edge = node_a.connected_edges[0]

0 commit comments

Comments
 (0)