This repository was archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm_connector.py
More file actions
69 lines (62 loc) · 2.83 KB
/
Copy pathosm_connector.py
File metadata and controls
69 lines (62 loc) · 2.83 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
import osmnx as ox
import networkx as nx
class OSMConnector:
def __init__(
self,
location_coordinates: tuple,
graph_file_path="foo.graphml",
dist=1000,
simplify=True,
network_type="drive",
):
"""
This class is the connection to OpenStreetMaps (OSM).
It allows to easily get the shortest distance / the fastest travel time needed between to coordinates.
Coordinates are represented by a tuple, which looks like this: (lat, long)
These are the importing parameters:
location_coordinates: tuple => the general location of the node_system (coordinates)
graph_file_path: str => the string to save the graph data to. Allows to reload the data.
Please note though, that should the variables like location_coordinates change, this file has to be deleted manually.
dist: int => the amount of distance in meters around the location coordinates
simplify: bool => should the graph be simplified? makes it less accurate, but also faster
network_type: str => mode of transportation
"""
assert graph_file_path.endswith(".graphml")
"The graph file path is invalid and needs to end with '.graphml"
try:
self.graph = ox.load_graphml(graph_file_path)
except FileNotFoundError:
self.graph = ox.graph_from_point(
location_coordinates,
dist=dist,
simplify=simplify,
network_type=network_type,
)
self.graph = ox.add_edge_speeds(self.graph)
self.graph = ox.add_edge_travel_times(self.graph)
ox.save_graphml(self.graph, graph_file_path)
def get_fastest_time_in_seconds(self, origin_coordinates, destination_coordinates):
"""
Returns the time needed to drive through the fastest route
"""
origin_node = self._convert_coordinates_to_node(origin_coordinates)
destination_node = self._convert_coordinates_to_node(destination_coordinates)
return nx.shortest_path_length(
self.graph, origin_node, destination_node, weight="travel_time"
)
def get_shortest_distance_in_meters(
self, origin_coordinates, destination_coordinates
):
"""
Returns the time needed to drive the fastest route
"""
origin_node = self._convert_coordinates_to_node(origin_coordinates)
destination_node = self._convert_coordinates_to_node(destination_coordinates)
return nx.shortest_path_length(
self.graph, origin_node, destination_node, weight="length"
)
def _convert_coordinates_to_node(self, coordinates):
"""
Returns the nearest node that is in the graph
"""
return ox.nearest_nodes(self.graph, Y=coordinates[0], X=coordinates[1])