62 lines
3.0 KiB
Python
62 lines
3.0 KiB
Python
import requests
|
|
|
|
from geopy.geocoders import Nominatim
|
|
# Chiavi API e coordinate per Luino e Varese
|
|
|
|
def TempoDistanza(coords_P, coords_A):
|
|
api_key = '5b3ce3597851110001cf62488face5f515964f4daefdf7fc2d25b9fb'
|
|
# Fai la richiesta all'API per ottenere la distanza e la durata
|
|
# url='https://api.openrouteservice.org/v2/directions/driving-car?api_key=5b3ce3597851110001cf62488face5f515964f4daefdf7fc2d25b9fb&start=45.994972037248836,8.742566658491763&end=45.82024694372096,8.825195530680334'
|
|
# url='https://api.openrouteservice.org/v2/directions/driving-car?api_key=5b3ce3597851110001cf62488face5f515964f4daefdf7fc2d25b9fb&start=45.94354415,8.818079449681923&end=45.817549,8.8263532'
|
|
url = f'https://api.openrouteservice.org/v2/directions/driving-car?api_key={api_key}&start={coords_P}&end={coords_A}'
|
|
print(url)
|
|
response = requests.get(url)
|
|
print("partenza da: ")
|
|
coordinate_reverse(coords_P)
|
|
print("arrivo a: ")
|
|
coordinate_reverse(coords_A)
|
|
|
|
print(response)
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
distance = round(data['features'][0]['properties']['segments'][0]['distance'] / 1000, 2) # Distanza in km
|
|
duration = round(data['features'][0]['properties']['segments'][0]['duration'] / 60, 2) # Durata in minuti
|
|
# print(f'Distanza tra Luino e Varese: {distance} km')
|
|
# print(f'Tempistica stimata: {duration} minuti')
|
|
return (distance,duration)
|
|
else:
|
|
return (response.status_code,0)
|
|
|
|
def coordinate(nome):
|
|
# Crea un oggetto geolocator
|
|
geolocator = Nominatim(user_agent="myGeocoder")
|
|
|
|
# Ottieni le coordinate di un luogo specifico (ad esempio, Varese)
|
|
# location = geolocator.geocode("Varese, Italy")
|
|
# location = geolocator.geocode("Libertà 59, Cugliate-Fabiasco, 21030 Varese, Italy")
|
|
location=geolocator.geocode(nome)
|
|
if location:
|
|
return f"{location.latitude},{location.longitude}"
|
|
else:
|
|
return "Posizione non trovata"
|
|
|
|
def coordinate_reverse(latitudine_longitudine):
|
|
#Via Libertà, Cugliate, Cugliate-Fabiasco, Comunità Montana del Piambello, Varese, Lombardia, 21035, Italia
|
|
# Crea un oggetto geolocator
|
|
geolocator = Nominatim(user_agent="myGeocoder")
|
|
location = geolocator.reverse(f"{latitudine_longitudine}", exactly_one=True)
|
|
if location:
|
|
print(f"Coordinate {latitudine_longitudine}: {location.address}")
|
|
else:
|
|
print("Posizione non trovata")
|
|
|
|
# partenza = '45.994972037248836, 8.742566658491763' # Longitudine,Latitudine di Luino
|
|
# arrivo = '45.82024694372096,8.825195530680334' # Longitudine,Latitudine di Varese
|
|
#coordinate_reverse('45.94189037867244, 8.821503777125791')#cugliate fabiasco
|
|
#partenza = coordinate("via libertà 59, cugliate-fabiasco, 21030 Varese, Italy")
|
|
partenza = coordinate("Cugliate-Fabiasco, Varese, Italy")
|
|
arrivo = coordinate("Varese, Italy")
|
|
dati=TempoDistanza(partenza, arrivo)
|
|
print(f"distanza = {dati[0]} tempo = {dati[1]}")
|
|
#coordinate_reverse(arrivo)
|