-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_schools.py
60 lines (49 loc) · 1.73 KB
/
get_schools.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
import overpy
from geopy.geocoders import Nominatim
import ssl
import json
from decimal import Decimal
ssl._create_default_https_context = ssl._create_unverified_context
geolocator = Nominatim(user_agent="schooler")
def find_schools_osm(latitude, longitude, radius):
api = overpy.Overpass()
# json query
query = f"""
[out:json];
node
["amenity"="school"]
(around:{radius},{latitude},{longitude});
out body;
"""
result = api.query(query)
schools = []
for node in result.nodes:
school = {
'name': node.tags.get('name', 'N/A'),
'latitude': float(node.lat), # decimal to float so that results can be stored as JSON file
'longitude': float(node.lon),
'address': node.tags.get('addr:full', 'N/A')
}
schools.append(school)
return schools
address = str(input("address (street number, street name, city, state abbreviation): "))
# ex. 1600 Amphitheatre Parkway. Mountain View, CA
location = geolocator.geocode(address)
if location:
latitude, longitude = location.latitude, location.longitude
radius = int(input("search radius (in meters): ")) # 5 km
schools = find_schools_osm(latitude, longitude, radius)
# save to json
file_path = "school_results.json"
with open(file_path, "w") as file:
json.dump(schools, file, indent=4, default=str) # Use default=str to handle Decimals
print(f"Search results saved to {file_path}")
print()
# Print results
for school in schools:
print(f"Name: {school['name']}")
print(f"Location: ({school['latitude']}, {school['longitude']})")
print(f"Address: {school['address']}")
print()
else:
print("Address not found.")