-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape_step_0_location.py
65 lines (49 loc) · 1.7 KB
/
scrape_step_0_location.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pprint import pprint
from functools import partial
from geopy.geocoders import Nominatim
# Initialize the geolocator with a user-agent
geolocator = Nominatim(user_agent="myGeoVergil")
# Define a partial function to specify the language
geocode = partial(geolocator.geocode, language="en")
def get_place_bounds(place_name):
# Geocode the place
location = geocode(place_name)
if location is None:
raise Exception("No location found for the given place name.")
# Extract bounding box details
bbox = location.raw.get("boundingbox")
if bbox is None:
raise Exception(
"No bounding box information available for the given place name."
)
# Convert bounding box coordinates to float
min_latitude = float(bbox[0])
max_latitude = float(bbox[1])
min_longitude = float(bbox[2])
max_longitude = float(bbox[3])
return {
"min_latitude": min_latitude,
"max_latitude": max_latitude,
"min_longitude": min_longitude,
"max_longitude": max_longitude,
}
def main(place_name="varanasi"):
# Get the bounds
bounds = get_place_bounds(place_name)
# Print the results
print("Min Latitude:", bounds["min_latitude"])
print("Max Latitude:", bounds["max_latitude"])
print("Min Longitude:", bounds["min_longitude"])
print("Max Longitude:", bounds["max_longitude"])
# Optionally print raw location data
location = geolocator.geocode(place_name).raw
pprint(location)
return (
bounds["min_latitude"],
bounds["max_latitude"],
bounds["min_longitude"],
bounds["max_longitude"],
)
if __name__ == "__main__":
# Call main function with default value
main()