-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_buildings.py
More file actions
40 lines (33 loc) · 1.36 KB
/
api_buildings.py
File metadata and controls
40 lines (33 loc) · 1.36 KB
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
import requests
import json
import sys
min_lat = sys.argv[1]
min_lon = sys.argv[2]
max_lat = sys.argv[3]
max_lon = sys.argv[4]
# # These represent the region of interest, and we can change them here or control them elsewhere using like a website or smth like before
# min_lat, min_lon = 42.29025, -83.71978
# max_lat, max_lon = 42.29422, -83.71205
# this is good for one-off buildings / sampling. should not be used for the actual pipeline
OVERPASS_URL = "https://overpass-api.de/api/interpreter"
# directly using the OSM API is not suitable for analysis, its more primitive
# Next things would be to change query to get the roads as well as buildings
# This is the overpass query format in order to get the data (ChatGPT this cuz i dunno the format)
query_buildings = f"""
[out:json][timeout:25];
way["building"]({min_lat},{min_lon},{max_lat},{max_lon});
out body geom;
"""
# Do a post request (Chat says that post request works better with larger queries)
r = requests.post(OVERPASS_URL, data=query_buildings)
r.raise_for_status()
data_buildings = r.json()
# Convert to json format
# Encode boundaries into the JSOn as well
data_buildings["max_lat"] = max_lat
data_buildings["min_lat"] = min_lat
data_buildings["max_lon"] = max_lon
data_buildings["min_lon"] = min_lon
# Write to the JSON output
with open("osm_data_buildings.json", "w") as f:
json.dump(data_buildings, f, indent=2)