PIP-Talk - Week 47

PIP-Talk - Week 47!

Every week going forward I plan to write a little about interesting modules/libraries available for Python.

The topic for this week is: Geopy

If you looking for a module/library to locate the coordinates of addresses, cities, countries, and landmarks across the globe, you will find this piptalk especially interesting.


Licence: MIT License (MIT)

Example code

Finding Geocode of an address

# Importing the Nominatim geocoder class
from geopy.geocoders import Nominatim

# address we need to geocode
loc = 'Fågelsången 3, Norrköping, Sweden'

# making an instance of Nominatim class
geolocator = Nominatim(user_agent="my_request")

# applying geocode method to get the location
location = geolocator.geocode(loc)

# printing address and coordinates
print((location.latitude, location.longitude))


Fågelsången, Oxelbergen, Norrköping, Norrköpings kommun, Östergötlands län, 602 44, Sverige
(58.586072, 16.2131922)

Getting Address Information from longitude and latitude

In this example we will lookup a address from itś location.

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="my_request")
location = geolocator.reverse(('59.3190713', '18.0751146'))

print(f'zipcode: {location.raw["address"]["postcode"]}')
print(f'suburb: {location.raw["address"]["suburb"]}')
print(f'municipality: {location.raw["address"]["municipality"]}')


>> 15, Katarinavägen, Södermalm, Södermalms stadsdelsområde, Stockholms kommun, Stockholms län, 104 65, Sverige
>>zipcode: 104 65
>>suburb: Södermalm
>>municipality: Stockholms kommun

Calculate distance

In this example we will estimate the distance between two addresses with the great circle navigation.

from geopy.distance import great_circle as GRC

Norrkoping = (58.586072, 16.2131922)
Stockholm = (59.3190713, 18.0751146)

print(f'The distance between Norrköping and Stockholm is: {GRC(Norrkoping,Stockholm)}')


>> The distance between Norrköping and Stockholm is: 134.3231410899114 km



pip install geopy