How to Get Geolocation in Python using GeoPy?

What is Geolocation?

Geolocation refers to a physical location. It identifies and tracks the location of linked electronic devices using various location technologies such as GPS and IP addresses.

Python’s GeoPy module is used for this purpose. When you enter a location name, it returns all info such as the postal code, city, state, and nation, as well as latitudes and longitudes. When we enter the coordinates, it returns the name of the location.

GeoPy is not a built-in Python library. It must be explicitly installed.

Installation:

pip install geopy

Installation in Anaconda:

conda install geopy

Every geolocation service, such as Google Maps or Nominatim, has its own class in geopy.geocoders that are used to abstract the API of the service. We’ll be using Nominatim, a free API service tool, in this case.

Geocodes of some places in India:

Place                                          Latitude                       Longitude

Telangana, India                       17.123184                         79.208824

Kerala, India                             10.850516                         76.271080

Tamil Nadu, India                   11.127123                           78.656891

Getting Geolocation in Python using GeoPy

1)Getting information about the specified location using Location name

Approach:

  • Import Nominatim from geocoders of geopy module using the import keyword
  • Create an object for Nominatim() class by passing some random user_agent as an argument to it.
  • Here user_agent is the app name to which it is provides services.
  • Pass some random location to the geocode() function and apply it on the above location(Nominatim)
  • Here it gives geopy.location.Location object which contains the address and the coordinate
  • Get the address of the above-given location using the address attribute
  • Get the latitude of the above-given location using the latitude attribute
  • Get the longitude of the above-given location using the longitude attribute
  • Get the altitude of the above-given location using the altitude attribute.
  • The Exit of the Program.

Below is the implementation:

# Import Nominatim from geocoders of geopy module using the import keyword 
from geopy.geocoders import Nominatim
# Create an object for Nominatim() class by passing some random user_agent as an argument to it.
# Here user_agent is the app name to which it is provides services.
location = Nominatim(user_agent= "GetLocationdetails")

# Pass some random location to the geocode() function and apply it on the above location(Nominatim)
# Here it gives geopy.location.Location object which contains the address and the coordinate
Locationinfo = location.geocode("Mumbai Maharastra")
# Get the address of the above given location using the address attribute
print("Address = ", Locationinfo.address)
# Get the latitude of the above given location using the latitude attribute
print("Latitude = ", Locationinfo.latitude)
# Get the longitude of the above given location using the longitude attribute
print("Longitude = ", Locationinfo.longitude)
# Get the altitude of the above given location using the altitude attribute
print("Altitude = ", Locationinfo.altitude)

Output:

Address = Mumbai, Mumbai Metropolitan Region, Mumbai Suburban, Maharashtra, India
Latitude = 19.0759899
Longitude = 72.8773928
Altitude = 0.0

2)Getting information about the specified location using Latitudes, Longitudes

Approach:

  • Import Nominatim from geocoders of geopy module using the import keyword
  • Create an object for Nominatim() class by passing some random user_agent as an argument to it.
  • Here user_agent is the app name to which it is provides services.
  • Pass latitudes and longitudes coordinates as arguments to the reverse() function to get the location name and store it in a variable.
  • Get the address of the above-given location using the address attribute.
  • The Exit of the Program.

Below is the implementation:

# Import Nominatim from geocoders of geopy module using the import keyword 
from geopy.geocoders import Nominatim
# Create an object for Nominatim() class by passing some random user_agent as an argument to it.
# Here user_agent is the app name to which it is provides services.
location = Nominatim(user_agent= "GetLocationdetails")

# Pass latitudes and longitudes coordinates as arguments to the reverse() function to get the
# location name and store it in a variable.
loc_name = location.reverse("19.0759899,  72.8773928")
# Get the address of the above given location using the address attribute
print("The Address of the  from the given latitudes and longitudes: ")
print(loc_name.address)

Output:

The Address of the from the given latitudes and longitudes: 
Santa Cruz – Chembur Link Road, Hallow Pul, L Ward, Zone 5, Mumbai, Mumbai Metropolitan Region, Mumbai Suburban, Maharashtra, 400070, India