Skip to content

Commit e015d0a

Browse files
Allow union with just one topology to union in that topology
1 parent 371d666 commit e015d0a

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

test/union_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,9 @@ def test_union_in_one_topology():
449449
node_6 = Node(geo_node=EuclideanGeoNode(30, 0))
450450
edge_1 = Edge(node_1, node_2)
451451
edge_2 = Edge(node_2, node_3)
452-
edge_3 = Edge(node_2, node_5, intermediate_geo_nodes=[EuclideanGeoNode(13, 5), EuclideanGeoNode(17, 5)])
452+
edge_3 = Edge(
453+
node_2, node_5, intermediate_geo_nodes=[EuclideanGeoNode(13, 5), EuclideanGeoNode(17, 5)]
454+
)
453455
edge_4 = Edge(node_5, node_4)
454456
edge_5 = Edge(node_5, node_6)
455457
topology_a.add_nodes([node_1, node_2, node_3, node_4, node_5, node_6])

yaramo/operations/union.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,44 @@ class Union:
99
def union(
1010
topology_a: Topology, node_matching: Dict[Node, Node], topology_b: Topology = None
1111
) -> Topology:
12-
if not (
13-
Union._are_all_nodes_in_topology(topology_a, node_matching.keys())
14-
and Union._are_all_nodes_in_topology(topology_b, node_matching.values())
15-
):
16-
raise ValueError(
17-
"The node matching contains nodes, that are not inside the corresponding topology. Abort."
18-
)
12+
if topology_b is None:
13+
if not (
14+
Union._are_all_nodes_in_topology(topology_a, node_matching.keys())
15+
and Union._are_all_nodes_in_topology(topology_a, node_matching.values())
16+
):
17+
raise ValueError(
18+
"The node matching contains nodes, that are not inside the given topology. Abort."
19+
)
20+
else:
21+
if not (
22+
Union._are_all_nodes_in_topology(topology_a, node_matching.keys())
23+
and Union._are_all_nodes_in_topology(topology_b, node_matching.values())
24+
):
25+
raise ValueError(
26+
"The node matching contains nodes, that are not inside the corresponding topology. Abort."
27+
)
1928

2029
if not Union._are_all_nodes_ends(node_matching):
2130
raise ValueError(
2231
"Some of the nodes in the matching are points. All nodes have to be ends. Abort."
2332
)
2433

25-
topology_ab = Topology()
26-
OperationsHelper.copy_topology_metadata(topology_a, topology_ab)
27-
28-
topology_ab.nodes.update(topology_a.nodes)
29-
topology_ab.nodes.update(topology_b.nodes)
30-
topology_ab.edges.update(topology_a.edges)
31-
topology_ab.edges.update(topology_b.edges)
32-
topology_ab.signals.update(topology_a.signals)
33-
topology_ab.signals.update(topology_b.signals)
34-
topology_ab.routes.update(topology_a.routes)
35-
topology_ab.routes.update(topology_b.routes)
36-
topology_ab.vacancy_sections.update(topology_a.vacancy_sections)
37-
topology_ab.vacancy_sections.update(topology_b.vacancy_sections)
34+
topology_ab = topology_a
35+
36+
if topology_b is not None:
37+
topology_ab = Topology()
38+
OperationsHelper.copy_topology_metadata(topology_a, topology_ab)
39+
40+
topology_ab.nodes.update(topology_a.nodes)
41+
topology_ab.nodes.update(topology_b.nodes)
42+
topology_ab.edges.update(topology_a.edges)
43+
topology_ab.edges.update(topology_b.edges)
44+
topology_ab.signals.update(topology_a.signals)
45+
topology_ab.signals.update(topology_b.signals)
46+
topology_ab.routes.update(topology_a.routes)
47+
topology_ab.routes.update(topology_b.routes)
48+
topology_ab.vacancy_sections.update(topology_a.vacancy_sections)
49+
topology_ab.vacancy_sections.update(topology_b.vacancy_sections)
3850

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

0 commit comments

Comments
 (0)