Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite8c4869

Browse files
committed
added getting geolocation tutorial
1 parentcff9e58 commite8c4869

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
fromgeopy.geocodersimportNominatim
2+
importtime
3+
frompprintimportpprint
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+
defget_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+
returnapp.geocode(address).raw
19+
except:
20+
returnget_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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
geopy
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
fromgeopy.geocodersimportNominatim
2+
frompprintimportpprint
3+
importtime
4+
5+
app=Nominatim(user_agent="tutorial")
6+
7+
defget_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+
returnapp.reverse(coordinates,language=language).raw
16+
except:
17+
returnget_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)

[8]ページ先頭

©2009-2025 Movatter.jp