Как найти путь и расстояние между двумя координатами с помощью OSM и python?

Мне нужно получить время в пути и расстояние между двумя наборами координат, используя Python и картографический сервис с открытым исходным кодом (предпочтительно OSM).

Я нашел много разных библиотек Python, которые могут рассчитать расстояние между двумя заданными точками (местоположениями), но это не расстояние движения.

Я также заметил, что с помощью Google Distance Matrix API и интерпретатора JSON я могу сделать это в значительной степени, но я не хочу использовать Google для этого проекта. Посоветуйте подходящую библиотеку, которая использует сеть Open Street Map и рассчитывает время и расстояние в пути и, желательно, позволяет создать карту выбранного маршрута.

пс. Я заметил, что аналогичная задача выполняется с помощью OSM, но не с помощью python


person M.T    schedule 03.05.2016    source источник


Ответы (2)


Вы можете использовать OSMnx.

Вот пример кода, который делает то, что вы просите:

import osmnx as ox
import networkx as nx
from datetime import timedelta


# The place where your 2 points are located. It will be used to create a graph from the OSM data
# In this example, the 2 points are two addresses in Manhattan, so we choose "Manhattan"
# It could be a bounding box too, or an area around a point
graph_area = ("Manhattan, New York, USA")

# Create the graph of the area from OSM data. It will download the data and create the graph
G = ox.graph_from_place(graph_area, network_type='drive')

# OSM data are sometime incomplete so we use the speed module of osmnx to add missing edge speeds and travel times
G = ox.add_edge_speeds(G)
G = ox.add_edge_travel_times(G)

# Save graph to disk if you want to reuse it
ox.save_graphml(G, "Manhattan.graphml")

# Load the graph
#G = ox.load_graphml("Manhattan.graphml")

# Plot the graph
fig, ax = ox.plot_graph(G, figsize=(10, 10), node_size=0, edge_color='y', edge_linewidth=0.2)

# Two pairs of (lat,lng) coordinates
origin_coordinates = (40.70195053163349, -74.01123198479581)
destination_coordinates = (40.87148739347057, -73.91517498611597)

# If you want to take an address (osmx will use Nominatim service for this)
# origin_coordinates = ox.geocode("2 Broad St, New York, NY 10005")

# In the graph, get the nodes closest to the points
origin_node = ox.get_nearest_node(G, origin_coordinates)
destination_node = ox.get_nearest_node(G, destination_coordinates)


# Get the shortest route by distance
shortest_route_by_distance = ox.shortest_path(G, origin_node, destination_node, weight='length')

# Plot the shortest route by distance
fig, ax = ox.plot_graph_route(G, shortest_route_by_distance, route_color='y', route_linewidth=6, node_size=0)

# Get the shortest route by travel time
shortest_route_by_travel_time = ox.shortest_path(G, origin_node, destination_node, weight='length')

# Plot the shortest route by travel time
fig, ax = ox.plot_graph_route(G, shortest_route_by_travel_time, route_color='y', route_linewidth=6, node_size=0)

# Plot the 2 routes
fig, ax = ox.plot_graph_routes(G, routes=[shortest_route_by_distance, shortest_route_by_travel_time], route_colors=['r', 'y'], route_linewidth=6, node_size=0)

# Get the travel time, in seconds
# Note here that we use "nx" (networkx), not "ox" (osmnx)
travel_time_in_seconds = nx.shortest_path_length(G, origin_node, destination_node, weight='travel_time')
print(travel_time_in_seconds)

#The travel time in "HOURS:MINUTES:SECONDS" format
travel_time_in_hours_minutes_seconds = str(timedelta(seconds=travel_time_in_seconds))
print(travel_time_in_hours_minutes_seconds)

# Get the distance in meters
distance_in_meters = nx.shortest_path_length(G, origin_node, destination_node, weight='length')
print(distance_in_meters)
# Distance in kilometers
distance_in_kilometers = distance_in_meters / 1000
print(distance_in_kilometers)

И, кстати, спасибо Джеффу Боингу за эту замечательную библиотеку!

person Rivers    schedule 18.01.2021

Только что искал вас, я не проверял, но, похоже, это то, что вы ищете: http://wiki.openstreetmap.org/wiki/PyrouteLib

person Loïc    schedule 03.05.2016
comment
Спасибо, что поделились ссылкой. Я также наткнулся на эту библиотеку в своем поиске, но, похоже, не помог. В частности, потому что не было ни адекватной документации, ни достаточного количества примеров выполнения такого запроса в коде. Однако мне показалось интересным, что у них есть такой интерактивный графический интерфейс в almien.co.uk/OSM/. Маршрутизация - person M.T; 03.05.2016