Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python GeoPy Package: Find the location address of a specified latitude and longitude

Default Options Object (Nominatim API): Exercise-5 with Solution

Write a Python program to find the location address of a specified latitude and longitude using Nominatim API and Geopy package.

Sample Solution:

Python Code:

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")
lald = "47.470706, -99.704723"
print("Latitude and Longitude:",lald)
location = geolocator.geocode(lald)
print("Location address of the said Latitude and Longitude:")
print(location)
lald = "34.05728435, -117.194132331602"
print("\nLatitude and Longitude:",lald)
location = geolocator.geocode(lald)
print("Location address of the said Latitude and Longitude:")
print(location)
lald = "38.8976998, -77.0365534886228"
print("\nLatitude and Longitude:",lald)
location = geolocator.geocode(lald)
print("Location address of the said Latitude and Longitude:")
print(location)
lald = "55.7558° N, 37.6173° E"
print("\nLatitude and Longitude:",lald)
location = geolocator.geocode(lald)
print("Location address of the said Latitude and Longitude:")
print(location)
lald = "35.6762° N, 139.6503° E"
print("\nLatitude and Longitude:",lald)
location = geolocator.geocode(lald)
print("Location address of the said Latitude and Longitude:")
print(location)
lald = "41.9185° N, 45.4777° E"
print("\nLatitude and Longitude:",lald)
location = geolocator.geocode(lald)
print("Location address of the said Latitude and Longitude:")
print(location)

Sample Output:

Latitude and Longitude: 47.470706, -99.704723
Location address of the said Latitude and Longitude:
399, Stanford Avenue, Bowdon, Wells County, North Dakota, 58418, USA

Latitude and Longitude: 34.05728435, -117.194132331602
Location address of the said Latitude and Longitude:
Esri Building C, 380, New York Street, Esri Inc., Redlands, San Bernardino County, California, 92373, USA

Latitude and Longitude: 38.8976998, -77.0365534886228
Location address of the said Latitude and Longitude:
White House, 1600, Pennsylvania Avenue Northwest, Golden Triangle, Washington, D.C., 20500, USA

Latitude and Longitude: 55.7558° N, 37.6173° E
Location address of the said Latitude and Longitude:
Государственный исторический музей, 1, Красная площадь, Китай-город, Тверской район, Центральный административный округ, Москва, ЦФО, 109012, РФ

Latitude and Longitude: 35.6762° N, 139.6503° E
Location address of the said Latitude and Longitude:
高円寺, 杉並区, 東京都, 関東地方, 168-0063, 日本

Latitude and Longitude: 41.9185° N, 45.4777° E
Location address of the said Latitude and Longitude:
გვირაბის ქუჩა (Gvirabi St), თელავი, კახეთი, 2200, საქართველო

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the latitude and longitude of a given location using Nominatim API and Geopy package.
Next: Write a Python function to get the city, state and country name of a specified latitude and longitude using Nominatim API and Geopy package.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m