Files
ncc/NCC/prova.py
luca.mustacchio@outlook.it b45a0ce26f Aggiungere i file di progetto.
2026-01-31 14:23:18 +01:00

98 lines
4.4 KiB
Python

import osmnx as ox
import networkx as nx
from datetime import timedelta
from datetime import datetime
print("inizio caricamento mappa")
orario_corrente = datetime.now().time()
# 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 = ("Varese, Lombardia, Italy")
# 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')
# (For a better accuracy, create a graph with lot more nodes:)
#G = ox.graph_from_place(graph_area, network_type='drive', simplify=False)
# 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, "varese.graphml")
# Load the graph
G = ox.load_graphml("lombardia.graphml")
orario_fine = datetime.now().time()
differenza = datetime.combine(datetime.min, orario_fine) - datetime.combine(datetime.min, orario_corrente)
print(f"fine caricamento mappa tempo:{differenza}")
# 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)
def Partenza_Arrivo(partenza ="via libertà 59, cugliate-fabiasco", arrivo="via asmara, Luino"):
if partenza=="":
partenza ="via libertà 59, cugliate-fabiasco"
if arrivo=="":
arrivo ="viale belforte , Varese"
print(f"partenza {partenza}, arrivo {arrivo}")
# 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")
#origin_coordinates = ox.geocode("via libertà 59, cugliate fabiasco")
#destination_coordinates = ox.geocode("luino, Varese, italy")
origin_coordinates = ox.geocode(partenza)
destination_coordinates = ox.geocode(arrivo)
# In the graph, get the nodes closest to the points
origin_node = ox.nearest_nodes(G, Y=origin_coordinates[0], X=origin_coordinates[1])
destination_node = ox.nearest_nodes(G, Y=destination_coordinates[0], X=destination_coordinates[1])
# 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='travel_time')
# 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", 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", 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_meters)
# Distance in kilometers
distance_in_kilometers = distance_in_meters / 1000
print("distance in kilometers", distance_in_kilometers)
while True:
partenza = input("Inserisci luogo di partenza (via, paese, provincia, nazione: ")
arrivo = input("Inserisci luogo di arrivo (via, paese, provincia, nazione: ")
Partenza_Arrivo(partenza, arrivo)
#if risposta.lower() == 'fine':
# print("Terminato.")
# break
#else:
# print(f"Hai inserito: {risposta}")