Python | GeoPy to Fetch Geographic Locations



Today's Agenda

In this post, we will learn how to set up a Python function to get some geographic information using some parameters like Latitude, Longitude, Zip code, or an address. We will require some Python packages that would make it possible for us to get the required results.

Prerequisite

This post has been prepared for the audience who : 
  1. Have a basic knowledge of Python.
  2. Have a Python 3 compiler that can be used to run the code.
  3. And finally, who are eager to learn and try such useful functions.

Let's get started

In this post, we will set up a Python function named "location" to fetch geographic information like distance between two cities, and/or full address of a location using just the latitude, longitude, or zip code, etc. We will use Python Version: 3.6 (or higher will be compatible). Follow the code below for the above use case and do read the comments to get crux for each block of code.


# import all the required libraries

import geopy

from geopy import Nominatim

from geopy import distance

 

# function to carry out the specific geographic operations

def location():

    """

    :return: using the Nominatim API and GeoPy package to give following solutions

   """

    geolocator = Nominatim(user_agent="geoapiExercises")


    # To search the Street address, Name from a given location information

    add1 = "Koramangala, Bengaluru"

    print("Location address:", add1)

 

    location = geolocator.geocode(add1)

    print("Street address, street name is: ", location.address)


    # To search the Country Name from given state name

    state1 = "Uttarakhand"

    print("State Name:", state1)

 

    location = geolocator.geocode(state1)

    print("State Name/Country Name is: ", location.address)


    # To search the details of a given Zip Code

    zipcode1 = "248001"

    print("\nZipcode:", zipcode1)

 

    location = geolocator.geocode(zipcode1)

    print("Details of the said pincode are:", location.address)


    # To search the Latitude and Longitude of a given location

    lat_long = "Whitefield, Bengaluru"

    print("Location address:", lat_long)

 

    location = geolocator.geocode(lat_long)

    print("Latitude and Longitude of the said address is:",             (location.latitude, location.longitude))


    # To search the location address of a specified Latitude and Longitude

    la_ld = "47.470706, -99.704723"

    print("Latitude and Longitude:", la_ld)

 

    location = geolocator.geocode(la_ld)

    print("Location address of the said Latitude and Longitude is:",     location)


    # To get the City, State and Country name of a specified Latitude and Longitude

    coordinates = "47.470706, -99.704723"

    location1 = geolocator.reverse(coordinates, exactly_one=True)

 

    address1 = location1.raw['address']

    city = address1.get('city', '')

    state = address1.get('state', '')

    country = address1.get('country', '')

 

    print("City:", city, "State:", state, "Country:", country)


    # To calculate the distance between London and New York city

    london = "51.5074° N, 0.1278° W"

    new_york = "40.7128° N, 74.0060° W"

 

    print("Distance between London and New York city (in km) is:")

    print(distance.distance(london, new_york).km, " kms")

 

  # To convert an address and return its geographic coordinates

    add = "1600 Amphitheatre Parkway, Mountain View, CA"

    location = geolocator.geocode(add)

 

    print("Latitude = {}, Longitude = {}".format(location.latitude,     location.longitude))

 

# function call

if __name__ == "__main__"

    location()






Comments