Get the city, state, and country names from latitude and longitude using Python - GeeksforGeeks

22 Oct, 2020
In this article, we are going to write a python script to get the city, state, and country names by using latitude and longitude using the Geopy module.geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the world.
To install the Geopy module, run the following command in your terminal.
pip install geopy

Approach:
Import the Geopy module
Initialize Nominatim API to get location from the input string.
Get location with geolocator.reverse() function.
Now extract the data from the location instance
Let’s implement with step by step:
Step #1: Import the module.
Python3
# import modulefrom geopy.geocoders import Nominatim
Step #2: Make a Nominatim object and initialize Nominatim API  with the geoapiExercises parameter.
Python
# initialize Nominatim API geolocator = Nominatim(user_agent="geoapiExercises")
Step #3: Now assign the latitude and longitude into a geolocator.reverse() method. A reverse() methods require the argument query, and also accept at least the argument exactly_one, which is True by default.
Python3
# Latitude & Longitude inputLatitude = "25.594095"Longitude = "85.137566"  location = geolocator.reverse(Latitude+","+Longitude)  # Displayprint(location)
Output:
[Location(Rajendra Nagar, Patna, Patna Rural, Patna, Bihar, 800001, India, (25.594023552508407, 85.13756080147536, 0.0))] 
Step #4: Now get the information from the given list and parsed into a dictionary with raw function().
Python3
address = location.raw['address']print(address)
Output:
{'suburb': 'Rajendra Nagar',
 'city': 'Patna',
 'county': 'Patna Rural',
 'state_district': 'Patna',
 'state': 'Bihar',
 'postcode': '800001',
 'country': 'India',
 'country_code': 'in'}

Step #5: Now traverse the city, state, and country names.
Python3
city = address.get('city', '')state = address.get('state', '')country = address.get('country', '')code = address.get('country_code')zipcode = address.get('postcode')print('City : ',city)print('State : ',state)print('Country : ',country)print('Zip Code : ', zipcode)
Output:
City :  Patna
State :  Bihar
Country :  India
Zip Code :  800001

Full implementation:
Python3
# import modulefrom geopy.geocoders import Nominatim  # initialize Nominatim APIgeolocator = Nominatim(user_agent="geoapiExercises")    # Latitude & Longitude inputLatitude = "25.594095"Longitude = "85.137566"  location = geolocator.reverse(Latitude+","+Longitude)  address = location.raw['address']  # traverse the datacity = address.get('city', '')state = address.get('state', '')country = address.get('country', '')code = address.get('country_code')zipcode = address.get('postcode')print('City : ', city)print('State : ', state)print('Country : ', country)print('Zip Code : ', zipcode)
Output:
City :  Patna
State :  Bihar
Country :  India
Zip Code :  800001

python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Python | Get unique values from a list
Create a directory in Python
Python | Pandas dataframe.groupby()