Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import osmnx as ox
- import numpy as np
- import json
- import networkx as nx
- from datetime import timedelta
- def matr(place, transport, start_point, end_point):
- graph_area = place
- # 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=transport)
- # 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, "sample.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 = start_point
- destination_coordinates = end_point
- # 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.distance.nearest_nodes(G, origin_coordinates[1], origin_coordinates[0])
- destination_node = ox.distance.nearest_nodes(G, destination_coordinates[1], destination_coordinates[0])
- # 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)
- def editing(cord):
- cord = cord[0][:-1]
- return cord
- def jsoned():
- f = open('matr.json')
- data = json.load(f)
- transport = data["network_type"]
- state = data["place"]
- start = list(data["start_point"].split(" "))
- edited_start = editing(start)
- s = float(edited_start)
- e = float(start[1])
- start_list = [s, e]
- end = list(data["end_point"].split(" "))
- edited_end = editing(end)
- m = float(edited_end)
- a = float(end[1])
- end_list = [m, a]
- return matr(state, transport, start_list, end_list)
- jsoned()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement