Site icon Console Flare Blog

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

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}")

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

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.")

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.

Exit mobile version