Skip to content

Commit d166004

Browse files
Merge pull request #62 from simulate-digital-rail/lint
Lint
2 parents e7a5e11 + 25e3988 commit d166004

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
poetry run isort -c .
3535
- name: Lint with pylint
3636
run: |
37-
poetry run pylint orm_importer
37+
poetry run pylint orm_importer --fail-under 8
3838
3939
test:
4040

orm_importer/importer.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import defaultdict
2-
from typing import List
2+
from typing import Any, List, Optional
33

44
import networkx as nx
55
import overpy
@@ -16,11 +16,11 @@
1616
get_additional_signals,
1717
get_opposite_edge_pairs,
1818
get_signal_classification_number,
19+
get_signal_direction,
1920
get_signal_function,
2021
get_signal_kind,
2122
get_signal_name,
2223
get_signal_states,
23-
getSignalDirection,
2424
is_end_node,
2525
is_same_edge,
2626
is_signal,
@@ -35,15 +35,18 @@ def __init__(self):
3535
self.top_nodes: list[OverpyNode] = []
3636
self.node_data: dict[str, OverpyNode] = {}
3737
self.ways: dict[str, List[overpy.Way]] = defaultdict(list)
38-
self.paths: dict[str, List[List]] = defaultdict(list)
38+
self.paths: dict[tuple[Optional[Any], Optional[Any]], List[List]] = defaultdict(list)
3939
self.api = overpy.Overpass(url="https://osm.hpi.de/overpass/api/interpreter")
4040
self.topology = Topology()
4141

4242
def _get_track_objects(self, polygon: str, railway_option_types: list[str]):
4343
query_parts = ""
44-
for type in railway_option_types:
45-
query_parts = query_parts + f'way["railway"="{type}"](poly: "{polygon}");node(w)(poly: "{polygon}");'
46-
query = f'({query_parts});out body;'
44+
for _type in railway_option_types:
45+
query_parts = (
46+
query_parts
47+
+ f'way["railway"="{_type}"](poly: "{polygon}");node(w)(poly: "{polygon}");'
48+
)
49+
query = f"({query_parts});out body;"
4750
print(query)
4851
return self._query_api(query)
4952

@@ -52,21 +55,21 @@ def _query_api(self, query):
5255
return result
5356

5457
def _build_graph(self, track_objects):
55-
G = nx.Graph()
58+
graph = nx.Graph()
5659
for way in track_objects.ways:
5760
previous_node = None
5861
for idx, node_id in enumerate(way._node_ids):
5962
try:
6063
node = track_objects.get_node(node_id)
6164
self.node_data[node_id] = node
6265
self.ways[str(node_id)].append(way)
63-
G.add_node(node.id)
66+
graph.add_node(node.id)
6467
if previous_node:
65-
G.add_edge(previous_node.id, node.id)
68+
graph.add_edge(previous_node.id, node.id)
6669
previous_node = node
6770
except overpy.exception.DataIncomplete:
6871
continue
69-
return G
72+
return graph
7073

7174
def _get_next_top_node(self, node, edge: "tuple[str, str]", path):
7275
node_to_id = edge[1]
@@ -115,7 +118,7 @@ def _add_signals(self, path, edge: model.Edge, node_before, node_after):
115118
signal_geo_point
116119
),
117120
side_distance=dist_edge(node_before, node_after, node),
118-
direction=getSignalDirection(
121+
direction=get_signal_direction(
119122
edge, self.ways, path, node.tags["railway:signal:direction"]
120123
),
121124
function=get_signal_function(node),
@@ -134,8 +137,8 @@ def _get_edge_speed(self, edge: Edge):
134137
common_ways = ways_a.intersection(ways_b)
135138
if len(common_ways) != 1:
136139
return None
137-
maxspeed = common_ways.pop().tags.get("maxspeed", None)
138-
return int(maxspeed) if maxspeed else None
140+
max_speed = common_ways.pop().tags.get("maxspeed", None)
141+
return int(max_speed) if max_speed else None
139142

140143
def _should_add_edge(self, node_a: model.Node, node_b: model.Node, path: list[int]):
141144
edge_not_present = not self.topology.get_edge_by_nodes(node_a, node_b)
@@ -151,7 +154,8 @@ def run(self, polygon, railway_option_types: list[str] = None):
151154
track_objects = self._get_track_objects(polygon, railway_option_types)
152155
self.graph = self._build_graph(track_objects)
153156

154-
# ToDo: Check whether all edges really link to each other in ORM or if there might be edges missing for nodes that are just a few cm from each other
157+
# ToDo: Check whether all edges really link to each other in ORM or if there might be
158+
# edges missing for nodes that are just a few cm from each other
155159
# Only nodes with max 1 edge or that are a switch can be top nodes
156160
for node_id in self.graph.nodes:
157161
node = self.node_data[node_id]
@@ -205,7 +209,8 @@ def run(self, polygon, railway_option_types: list[str] = None):
205209
e for e in self.topology.edges.values() if e.node_a == node or e.node_b == node
206210
]
207211

208-
# merge edges, this means removing the switch and allowing only one path for each origin
212+
# merge edges, this means removing the switch and
213+
# allowing only one path for each origin
209214
edge_pair_1, edge_pair_2 = get_opposite_edge_pairs(connected_edges, node)
210215
new_edge_1 = merge_edges(*edge_pair_1, node)
211216
new_edge_2 = merge_edges(*edge_pair_2, node)
@@ -230,8 +235,10 @@ def run(self, polygon, railway_option_types: list[str] = None):
230235
except DataIncomplete:
231236
nodes = way.get_nodes(resolve_missing=True)
232237
for candidate in nodes:
233-
# we are only interested in nodes outside the bounding box as every node
234-
# that has been previously known was already visited as part of the graph
238+
# we are only interested in nodes outside the
239+
# bounding box as every node that has been
240+
# previously known was already visited as
241+
# part of the graph
235242
if (
236243
candidate.id != int(node.name)
237244
and candidate.id not in self.node_data.keys()
@@ -250,9 +257,9 @@ def run(self, polygon, railway_option_types: list[str] = None):
250257
break
251258
if not substitute_found:
252259
# if no substitute was found, the third node seems to be inside the bounding box
253-
# this can happen when a node is connected to the same node twice (e.g. station on
254-
# lines with only one track). WARNING: this produced weird results in the past.
255-
# It should be okay to do it after the check above.
260+
# this can happen when a node is connected to the same node twice (e.g.
261+
# station on lines with only one track). WARNING: this produced weird
262+
# results in the past. It should be okay to do it after the check above.
256263
connected_edges = [
257264
e
258265
for e in self.topology.edges.values()

orm_importer/utils.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
def dist_edge(node_before, node_after, signal):
2222
# Calculate distance from point(signal) to edge between node before and after
2323
# TODO: Validate that this is really correct!
24-
return 3.95
2524
p1 = np.array((node_before.lat, node_before.lon))
2625
p2 = np.array((node_after.lat, node_after.lon))
2726
p3 = np.array((signal.lat, signal.lon))
@@ -41,7 +40,8 @@ def is_end_node(node, graph):
4140

4241
def is_signal(node):
4342
# we cannot use railway=signal as a condition, as buffer stops violate this assumption.
44-
# Instead, we check for the signal direction as we cannot create a signal without a direction anyway
43+
# Instead, we check for the signal direction as we cannot create a
44+
# signal without a direction anyway
4545
return "railway:signal:direction" in node.tags.keys()
4646

4747

@@ -61,7 +61,7 @@ def is_same_edge(e1: tuple, e2: tuple):
6161
return False
6262

6363

64-
def getSignalDirection(edge: Edge, ways: dict[str, List[Way]], path, signal_direction_tag: str):
64+
def get_signal_direction(edge: Edge, ways: dict[str, List[Way]], path, signal_direction_tag: str):
6565
edge_is_forward = None
6666
for way in ways[edge.node_a.name]:
6767
node_a = int(edge.node_a.name)
@@ -90,12 +90,12 @@ def getSignalDirection(edge: Edge, ways: dict[str, List[Way]], path, signal_dire
9090
not edge_is_forward and signal_direction_tag == "backward"
9191
):
9292
return "in"
93-
else:
94-
return "gegen"
93+
return "gegen"
9594

9695

9796
def get_signal_states(signal_tags: dict):
98-
# Sh0 is tagged as Hp0 in OSM since a few years, but not all tags have been replaced so we convert them
97+
# Sh0 is tagged as Hp0 in OSM since a few years, but not all tags
98+
# have been replaced so we convert them
9999
raw_states = []
100100
raw_states += signal_tags.get("railway:signal:main:states", "").split(";")
101101
raw_states += signal_tags.get("railway:signal:combined:states", "").split(";")
@@ -160,12 +160,11 @@ def get_signal_function(signal: Node) -> str:
160160
tag = next(t for t in signal.tags.keys() if t.endswith(":function"))
161161
if signal.tags[tag] == "entry":
162162
return "Einfahr_Signal"
163-
elif signal.tags[tag] == "exit":
163+
if signal.tags[tag] == "exit":
164164
return "Ausfahr_Signal"
165-
elif signal.tags[tag] == "block":
165+
if signal.tags[tag] == "block":
166166
return "Block_Signal"
167-
else:
168-
return "andere"
167+
return "andere"
169168
except StopIteration:
170169
return "andere"
171170

@@ -176,36 +175,33 @@ def get_signal_kind(signal: Node) -> str:
176175
# ORM Reference: https://wiki.openstreetmap.org/wiki/OpenRailwayMap/Tagging/Signal
177176
if "railway:signal:main" in signal.tags.keys():
178177
return "Hauptsignal"
179-
elif "railway:signal:distant" in signal.tags.keys():
178+
if "railway:signal:distant" in signal.tags.keys():
180179
return "Vorsignal"
181-
elif "railway:signal:combined" in signal.tags.keys():
180+
if "railway:signal:combined" in signal.tags.keys():
182181
return "Mehrabschnittssignal"
183-
elif "railway:signal:shunting" in signal.tags.keys() or (
182+
if "railway:signal:shunting" in signal.tags.keys() or (
184183
"railway:signal:minor" in signal.tags.keys()
185184
and (
186185
signal.tags["railway:signal:minor"] == "DE-ESO:sh0"
187186
or signal.tags["railway:signal:minor"] == "DE-ESO:sh2"
188187
)
189188
):
190189
return "Sperrsignal"
191-
elif (
192-
"railway:signal:main" in signal.tags.keys() and "railway:signal:minor" in signal.tags.keys()
193-
):
190+
if "railway:signal:main" in signal.tags.keys() and "railway:signal:minor" in signal.tags.keys():
194191
return "Hauptsperrsignal"
195192
# Names in comment are not yet supported by PlanPro generator
196-
elif "railway:signal:main_repeated" in signal.tags.keys():
193+
if "railway:signal:main_repeated" in signal.tags.keys():
197194
return "andere" # 'Vorsignalwiederholer'
198-
elif "railway:signal:minor" in signal.tags.keys():
195+
if "railway:signal:minor" in signal.tags.keys():
199196
return "andere" # 'Zugdeckungssignal'
200-
elif "railway:signal:crossing" in signal.tags.keys():
197+
if "railway:signal:crossing" in signal.tags.keys():
201198
return "andere" # 'Überwachungssignal'
202-
elif (
199+
if (
203200
"railway:signal:combined" in signal.tags.keys()
204201
and "railway:signal:minor" in signal.tags.keys()
205202
):
206203
return "andere" # 'Mehrabschnittssperrsignal'
207-
else:
208-
return "andere"
204+
return "andere"
209205

210206

211207
def get_signal_name(node: Node):

0 commit comments

Comments
 (0)