Skip to content

Commit e8c4869

Browse files
committed
added getting geolocation tutorial
1 parent cff9e58 commit e8c4869

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
6868
- [How to Convert Python Files into Executables](https://www.thepythoncode.com/article/building-python-files-into-stand-alone-executables-using-pyinstaller)
6969
- [How to Get the Size of Directories in Python](https://www.thepythoncode.com/article/get-directory-size-in-bytes-using-python). ([code](general/calculate-directory-size))
7070
- [How to Play and Record Audio in Python](https://www.thepythoncode.com/article/play-and-record-audio-sound-in-python). ([code](general/recording-and-playing-audio))
71+
- [How to Get Geographic Locations in Python](https://www.thepythoncode.com/article/get-geolocation-in-python). ([code](general/geolocation))
7172

7273

7374
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)

general/geolocation/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Get Geographic Locations in Python](https://www.thepythoncode.com/article/get-geolocation-in-python)

general/geolocation/geolocator.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from geopy.geocoders import Nominatim
2+
import time
3+
from pprint import pprint
4+
5+
# instantiate a new Nominatim client
6+
app = Nominatim(user_agent="tutorial")
7+
# get location raw data
8+
location = app.geocode("Nairobi, Kenya").raw
9+
# print raw data
10+
pprint(location)
11+
12+
13+
def get_location_by_address(address):
14+
"""This function returns a location as raw from an address
15+
will repeat until success"""
16+
time.sleep(1)
17+
try:
18+
return app.geocode(address).raw
19+
except:
20+
return get_location_by_address(address)
21+
22+
address = "Makai Road, Masaki, Dar es Salaam, Tanzania"
23+
location = get_location_by_address(address)
24+
latitude = location["lat"]
25+
longitude = location["lon"]
26+
print(f"{latitude}, {longitude}")
27+
# print all returned data
28+
pprint(location)

general/geolocation/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
geopy
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from geopy.geocoders import Nominatim
2+
from pprint import pprint
3+
import time
4+
5+
app = Nominatim(user_agent="tutorial")
6+
7+
def get_address_by_location(latitude, longitude, language="en"):
8+
"""This function returns an address as raw from a location
9+
will repeat until success"""
10+
# build coordinates string to pass to reverse() function
11+
coordinates = f"{latitude}, {longitude}"
12+
# sleep for a second to respect Usage Policy
13+
time.sleep(1)
14+
try:
15+
return app.reverse(coordinates, language=language).raw
16+
except:
17+
return get_address_by_location(latitude, longitude)
18+
19+
# define your coordinates
20+
latitude = 36.723
21+
longitude = 3.188
22+
# get the address info
23+
address = get_address_by_location(latitude, longitude)
24+
# print all returned data
25+
pprint(address)

0 commit comments

Comments
 (0)