2 Easy Python Codes to Access Latitude & Longitude.

Well, we all are well aware that Python is a general-purpose programming language which means we can do a variety of tasks with the help of Python like web development, software development, data analysis, etc. Python has a wide range of libraries depending on the industry and requirements of the user.

For example, libraries like Healthcareai and PyMedtermino for the healthcare sector, and Quantlib and Pyfolio for the finance sector. Similarly, in the logistics sector, we use libraries like NetworkX and Geopy to calculate latitude-longitude, the distance between 2 points, and so on.

So in this blog, we will create three Python codes using geopy library.

1. Python program to get the latitude and longitude of a place given by the user.

a. Install the necessary library geopy from pip

b. In the editor screen, import geocoder as a submodule from geopy library and Nominatim class which will help us to convert locations into geographical coordinates.

c. Creating a function by the name get_lat_long with location name as a parameter.

def get_lat_long(location_name):
    geolocator = Nominatim(user_agent="geopy_example",timeout=10)
    location = geolocator.geocode(location_name)

    if location:
        latitude, longitude = location.latitude, location.longitude
        return latitude, longitude
    else:
        return None
  • Creating a geolocator object of the Nominatim class, specifying a user agent (“geopy_example”) and a timeout of 10 seconds for each geocoding request.
  • Changing the location name(provided by the user) into a location object with the help of the geocode method. the location object contains the latitude and longitude information.
  • This conditional statement checks if a valid location object was returned. If a valid location is found, the code extracts the latitude and longitude.
  • If a valid location is found, this line extracts the latitude and longitude from the location object.
  • The function returns a tuple containing the latitude and longitude if a valid location is found.
  • If the geocoding process fails to find a location (for example, if the provided address is not valid), the function returns None.

d. Receiving input from the user, checking for the result, and printing the final output.

location_name = input("Enter a location name: ")
result = get_lat_long(location_name)

if result:
    latitude, longitude = result
    print(f"Latitude: {latitude}, Longitude: {longitude}")
else:
    print(f"Could not find coordinates for {location_name}")
  • Receiving the name of the location from the user of the code has to calculate latitude and longitude and store the data in the variable location_name.
  • Calling the function get_lat_long with the argument location_name and storing the output of the function in the variable result as a tuple.
  • Checking if the result contains data, distributing the items of the tuple(result) in latitude and longitude, and then printing it separately
  • If there is no data in the result, the code displays the message,” could not find coordinates”.

2. Python program for reverse geocoding

a. We need to install geopy library from pip, the same way we have done before.

b. In the editor screen, import geocoder as a submodule from geopy library and Nominatim class which will help us to convert locations into geographical coordinates.

c. Creating a function named reverse_geocode with latitude and longitude as a parameter.

def reverse_geocode(latitude, longitude):
    geolocator = Nominatim(user_agent="geopy_example")
    location = geolocator.reverse((latitude, longitude))

    if location:
        address = location.address
        return address
    else:
        return None
  • Creating a geolocator object of the Nominatim class, specifying a user agent (“geopy_example”).
  • In this line, we use the reverse method of the geolocator object to perform reverse geocoding with the given latitude and longitude. The result is stored in the location variable.
  • This conditional statement checks if a valid location was returned. The code extracts the human-readable address from the address attribute.
  • The code extracts the human-readable address from the address attribute and stores the output in the address variable.
  • The function returns a variable address(a tuple containing the latitude and longitude) if a valid location is found.
  • If the geocoding process fails to find a location (for example, if the provided latitude and longitude are not valid), the function returns None.

d. Receiving input from the user, checking for the result, and printing the final output.

latitude = float(input("Enter latitude: "))
longitude = float(input("Enter longitude: "))

result = reverse_geocode(latitude, longitude)

if result:
    print(f"Address: {result}")
else:
    print(f"Could not find address for the provided coordinates.")
  • Receiving latitudes and longitude from the user and store the data in the variables.
  • Calling the function reverse_geocode with the argument latitude and logitude and storing the output of the function in the variable result as a tuple.
  • Checking if the result contains data, and printing the address.
  • If there is no data in the result, the code displays the message,” could not find coordinates”.

Want to have more cool codes like these, follow us on: Facebook,Instagram, Linkedin.

Apart from Python translator, we all know that Python is a general-purpose programming language which means Python can be used in various fields like data science, web development, game development, IOT(Internet of Things), etc.
If you wish to learn more about data science or want to curve your career in the data science field feel free to join our free workshop on Masters in Data Science with PowerBI, where you will get to know how exactly data science fieldwork and why companies are ready to pay handsome salaries in this field.

In this workshop, you will get to know each tool and technology from scratch that will make you skillfully eligible for any data science profile.

To join this workshop, register yourself on consoleflare and we will call you back.

Leave a Reply

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

Back To Top