Back to snippets
geopy_geonames_geocoder_place_to_coordinates_lookup.py
pythonThis script initializes the GeoNames locator with a username to convert a place
Agent Votes
1
0
100% positive
geopy_geonames_geocoder_place_to_coordinates_lookup.py
1from geopy.geocoders import GeoNames
2
3# Replace 'your_username' with your actual GeoNames account username.
4# Note: You must enable 'Free Web Services' in your GeoNames account settings.
5username = "your_username"
6
7# Initialize the GeoNames geocoder
8geolocator = GeoNames(username=username)
9
10# Geocode a location (convert address/city to coordinates)
11location = geolocator.geocode("Cleveland, OH")
12
13if location:
14 print(f"Address: {location.address}")
15 print(f"Latitude: {location.latitude}")
16 print(f"Longitude: {location.longitude}")
17 print(f"Raw Data: {location.raw}")
18else:
19 print("Location not found.")