Advertisement
Falexom

Untitled

Aug 17th, 2022
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. import osmnx as ox
  2. import numpy as np
  3. import json
  4. import networkx as nx
  5. from datetime import timedelta
  6.  
  7.  
  8. def matr(place, transport, start_point, end_point):
  9.     graph_area = place
  10.  
  11.     # Create the graph of the area from OSM data. It will download the data and create the graph
  12.     G = ox.graph_from_place(graph_area, network_type=transport)
  13.  
  14.     # OSM data are sometime incomplete so we use the speed module of osmnx to add missing edge speeds and travel times
  15.     G = ox.add_edge_speeds(G)
  16.     G = ox.add_edge_travel_times(G)
  17.  
  18.     # Save graph to disk if you want to reuse it
  19.     ox.save_graphml(G, "sample.graphml")
  20.  
  21.     # Load the graph
  22.     # G = ox.load_graphml("Manhattan.graphml")
  23.  
  24.     # Plot the graph
  25.     fig, ax = ox.plot_graph(G, figsize=(10, 10), node_size=0, edge_color='y', edge_linewidth=0.2)
  26.  
  27.     # Two pairs of (lat,lng) coordinates
  28.     origin_coordinates = start_point
  29.     destination_coordinates = end_point
  30.  
  31.     # If you want to take an address (osmx will use Nominatim service for this)
  32.     # origin_coordinates = ox.geocode("2 Broad St, New York, NY 10005")
  33.  
  34.     # In the graph, get the nodes closest to the points
  35.     origin_node = ox.distance.nearest_nodes(G, origin_coordinates[1], origin_coordinates[0])
  36.     destination_node = ox.distance.nearest_nodes(G, destination_coordinates[1], destination_coordinates[0])
  37.  
  38.     # Get the shortest route by distance
  39.     shortest_route_by_distance = ox.shortest_path(G, origin_node, destination_node, weight='length')
  40.  
  41.     # Plot the shortest route by distance
  42.     fig, ax = ox.plot_graph_route(G, shortest_route_by_distance, route_color='y', route_linewidth=6, node_size=0)
  43.  
  44.     # Get the shortest route by travel time
  45.     shortest_route_by_travel_time = ox.shortest_path(G, origin_node, destination_node, weight='length')
  46.  
  47.     # Plot the shortest route by travel time
  48.     fig, ax = ox.plot_graph_route(G, shortest_route_by_travel_time, route_color='y', route_linewidth=6, node_size=0)
  49.  
  50.     # Plot the 2 routes
  51.     fig, ax = ox.plot_graph_routes(G, routes=[shortest_route_by_distance, shortest_route_by_travel_time],
  52.                                    route_colors=['r', 'y'], route_linewidth=6, node_size=0)
  53.  
  54.     # Get the travel time, in seconds
  55.     # Note here that we use "nx" (networkx), not "ox" (osmnx)
  56.     travel_time_in_seconds = nx.shortest_path_length(G, origin_node, destination_node, weight='travel_time')
  57.     print(travel_time_in_seconds)
  58.  
  59.     # The travel time in "HOURS:MINUTES:SECONDS" format
  60.     travel_time_in_hours_minutes_seconds = str(timedelta(seconds=travel_time_in_seconds))
  61.     print(travel_time_in_hours_minutes_seconds)
  62.  
  63.     # Get the distance in meters
  64.     distance_in_meters = nx.shortest_path_length(G, origin_node, destination_node, weight='length')
  65.     print(distance_in_meters)
  66.     # Distance in kilometers
  67.     distance_in_kilometers = distance_in_meters / 1000
  68.     print(distance_in_kilometers)
  69.  
  70.  
  71.  
  72. def editing(cord):
  73.     cord = cord[0][:-1]
  74.     return cord
  75.  
  76.  
  77. def jsoned():
  78.     f = open('matr.json')
  79.     data = json.load(f)
  80.     transport = data["network_type"]
  81.     state = data["place"]
  82.  
  83.     start = list(data["start_point"].split(" "))
  84.     edited_start = editing(start)
  85.     s = float(edited_start)
  86.     e = float(start[1])
  87.     start_list = [s, e]
  88.  
  89.     end = list(data["end_point"].split(" "))
  90.     edited_end = editing(end)
  91.     m = float(edited_end)
  92.     a = float(end[1])
  93.     end_list = [m, a]
  94.  
  95.     return matr(state, transport, start_list, end_list)
  96.  
  97.  
  98. jsoned()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement