Created
November 6, 2024 12:12
-
-
Save andreybleme/3d281f8899560489f3ed1287edb30d51 to your computer and use it in GitHub Desktop.
Script python que imprime geocoordenadas apartir de uma lista de endereços
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from geopy.geocoders import Nominatim | |
from geopy.exc import GeocoderTimedOut | |
import time | |
# Function to get coordinates from an address | |
def get_coordinates(address): | |
geolocator = Nominatim(user_agent="geoapiExercises") | |
try: | |
location = geolocator.geocode(address) | |
if location: | |
return (location.latitude, location.longitude) | |
else: | |
return "Address not found" | |
except GeocoderTimedOut: | |
return "Geocoding service timed out" | |
# List of addresses | |
addresses = [ | |
"Rua Tinguassu, 486, Novo Eldorado, Contagem, Brazil", | |
"350 Fifth Avenue, New York, NY" | |
] | |
# Dictionary to store addresses and their coordinates | |
coordinates_dict = {} | |
for address in addresses: | |
# Get coordinates for each address | |
coordinates = get_coordinates(address) | |
coordinates_dict[address] = coordinates | |
time.sleep(1) # Adding a short delay to avoid request overload | |
# Print the results | |
for address, coords in coordinates_dict.items(): | |
print(f"Address: {address}\nCoordinates: {coords}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment