Get Geolocation in Python using GeoPy

In this tutorial, we will learn how to get geolocation location in Python using some basic examples. Geolocation means geographic location. It uses various location technologies like GPS and IP addresses to identify and track the location of connected electronic devices. We will use the GeoPy library of Python. When you enter any location name, it returns all relevant information such as postal code, city, state, country along with latitudes and longitudes. When we provide the coordinates, it returns the location name. The GeoPy is not an inbuilt library in Python. It needs to be installed explicitly.

Installation:

pip install geopy
or
conda install geopy

Every geolocation service such as Google Maps or Nominatim has its own class in geopy.geocoders which is used for abstracting the service’s API. Here, we will be using Nominatim which is a free API service tool.

Passing location name to get relevant information with respect to the given location

Approach:

  1. Import the module Nominatim from geopy.geocoders and create an instance of the Nominatim class and pass the argument for user_agent which is used to name the app to which it is providing services.
    Note: You can pass any name for user_agent.
  2. Apply the geocode() method of Nominatim by passing the location name as an argument. It returns a geopy.location.Location object which contains the address and the coordinates. It can also be accessed as:
    Address: referenceVariable.address,         Latitude: referenceVariable.latitude,
    Longitude: referenceVariable.longitude,    Altitude: referenceVariable.altitude.

Note: Here, referenceVariable is the reference variable to the geopy.location.Location object.

from geopy.geocoders import Nominatim
location = Nominatim(user_agent="GetLoc")
getLocation = location.geocode("Kolkata West Bengal")
print(“Address : “,getLocation.address)
print(“Latitude : “,getLocation.latitude)
print(“Longitude : “,getLocation.longitude)
print(“Altitude : “,getLocation.altitude)

Output:

Address : Kolkata, Howrah, West Bengal, India
Latitude : 22.5414185
Longitude : 88.35769124388872
Altitude : 0.0

Also read: Weather Forecasting Script in Python (API Call method)

Example 2: Passing coordinates to get relevant information with respect to the given coordinates

Approach:

You can use the reverse() method of the Nominatim class which accepts the coordinates (latitude and longitude) as arguments and returns a geopy.location.Location object which contains the address and the coordinates of the location.

from geopy.geocoders import Nominatim
location = Nominatim(user_agent="GetLoc")
locationName = location.reverse("22.5414185,88.35769124388872")
print(“Address : “locationName.address)

Output:

Address : La Martinere for Girls, Acharya Jagadish Chandra Bose Road, Mallick Bazaar, Ripon Street, Kolkata, West Bengal, 700017, India

Note:

The geopy.location.Location also has a dictionary consisting of all the details of the given location. The keys are the attributes of a location. You can access the dictionary by:

referenceVariable.raw

Here, referenceVariable is the reference variable to the geopy.location.Location object.

Let me explain with an example:

We can refer to example 1. If we add the following line,

print(getLocation.raw)

we get the output as,

{'place_id': 259838636,
'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
'osm_type': 'relation',
'osm_id': 10371838,
'boundingbox': ['22.4503235', '22.6325362', '88.2406237', '88.4589549'],
'lat': '22.5414185',
'lon': '88.35769124388872',
'display_name': 'Kolkata, Howrah, West Bengal, India',
'class': 'boundary',
'type': 'administrative',
'importance': 0.9440385346278307,
'icon':'https://nominatim.openstreetmap.org/ui/mapicons//poi_boundary_administrative.p.20.png'}

Leave a Reply

Your email address will not be published. Required fields are marked *