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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# import module
from geopy.geocoders import Nominatim

chevron_right


Step #2: Make a Nominatim object and initialize Nominatim API  with the geoapiExercises parameter.

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# initialize Nominatim API 
geolocator = Nominatim(user_agent="geoapiExercises")

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Latitude & Longitude input
Latitude = "25.594095"
Longitude = "85.137566"
  
location = geolocator.reverse(Latitude+","+Longitude)
  
# Display
print(location)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

address = location.raw['address']
print(address)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)

chevron_right


Output:

City :  Patna
State :  Bihar
Country :  India
Zip Code :  800001

Full implementation:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import module
from geopy.geocoders import Nominatim
  
# initialize Nominatim API
geolocator = Nominatim(user_agent="geoapiExercises")
  
  
# Latitude & Longitude input
Latitude = "25.594095"
Longitude = "85.137566"
  
location = geolocator.reverse(Latitude+","+Longitude)
  
address = location.raw['address']
  
# traverse the data
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)

chevron_right


Output:

City :  Patna
State :  Bihar
Country :  India
Zip Code :  800001

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.