-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_data.py
More file actions
165 lines (153 loc) · 5.23 KB
/
Copy pathcollect_data.py
File metadata and controls
165 lines (153 loc) · 5.23 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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
yeye — Step 1: OpenStreetMap Data Collection for Togo
Pulls emergency-relevant infrastructure using the Overpass API.
"""
import requests
import json
import time
import pandas as pd
import os
OVERPASS_URL = "https://overpass-api.de/api/interpreter"
OUTPUT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "raw")
# Togo bounding box: south, west, north, east
TOGO_BBOX = "6.1,-0.15,11.14,1.8"
QUERIES = {
"hospitals": f"""
[out:json][timeout:60];
(
node["amenity"="hospital"]({TOGO_BBOX});
way["amenity"="hospital"]({TOGO_BBOX});
node["amenity"="clinic"]({TOGO_BBOX});
node["healthcare"="hospital"]({TOGO_BBOX});
node["healthcare"="clinic"]({TOGO_BBOX});
);
out center tags;
""",
"fire_stations": f"""
[out:json][timeout:60];
(
node["amenity"="fire_station"]({TOGO_BBOX});
way["amenity"="fire_station"]({TOGO_BBOX});
);
out center tags;
""",
"police": f"""
[out:json][timeout:60];
(
node["amenity"="police"]({TOGO_BBOX});
way["amenity"="police"]({TOGO_BBOX});
);
out center tags;
""",
"fuel_stations": f"""
[out:json][timeout:60];
(
node["amenity"="fuel"]({TOGO_BBOX});
way["amenity"="fuel"]({TOGO_BBOX});
);
out center tags;
""",
"pharmacies": f"""
[out:json][timeout:60];
(
node["amenity"="pharmacy"]({TOGO_BBOX});
way["amenity"="pharmacy"]({TOGO_BBOX});
);
out center tags;
""",
"roads_main": f"""
[out:json][timeout:90];
(
way["highway"~"motorway|trunk|primary|secondary"]({TOGO_BBOX});
);
out geom tags;
"""
}
def fetch_osm(name, query, retries=3):
print(f" Fetching {name}...")
os.makedirs(OUTPUT_DIR, exist_ok=True)
for attempt in range(retries):
try:
resp = requests.post(OVERPASS_URL, data={"data": query}, timeout=120)
if resp.status_code in (429, 504):
wait = 30 * (attempt + 1)
print(f" ! {name}: HTTP {resp.status_code}, retrying in {wait}s...")
time.sleep(wait)
continue
resp.raise_for_status()
data = resp.json()
path = os.path.join(OUTPUT_DIR, f"osm_{name}.json")
with open(path, "w") as f:
json.dump(data, f)
count = len(data.get("elements", []))
print(f" ✓ {name}: {count} elements saved")
return count
except Exception as e:
print(f" ✗ {name} failed: {e}")
if attempt < retries - 1:
time.sleep(15)
return 0
def parse_to_csv(name):
"""Convert OSM JSON to a clean CSV with lat/lon and key tags."""
path = os.path.join(OUTPUT_DIR, f"osm_{name}.json")
if not os.path.exists(path):
return
with open(path) as f:
data = json.load(f)
rows = []
for el in data.get("elements", []):
# Nodes have lat/lon directly; ways have a center
lat = el.get("lat") or (el.get("center") or {}).get("lat")
lon = el.get("lon") or (el.get("center") or {}).get("lon")
if not lat or not lon:
continue
tags = el.get("tags", {})
rows.append({
"osm_id": el.get("id"),
"type": el.get("type"),
"lat": lat,
"lon": lon,
"name": tags.get("name", tags.get("name:fr", "")),
"name_en": tags.get("name:en", ""),
"amenity": tags.get("amenity", ""),
"healthcare": tags.get("healthcare", ""),
"operator": tags.get("operator", ""),
"beds": tags.get("beds", ""),
"emergency": tags.get("emergency", ""),
"phone": tags.get("phone", tags.get("contact:phone", "")),
"opening_hours": tags.get("opening_hours", ""),
"highway": tags.get("highway", ""),
"surface": tags.get("surface", ""),
"category": name
})
if rows:
df = pd.DataFrame(rows)
csv_path = os.path.join(OUTPUT_DIR, f"osm_{name}.csv")
df.to_csv(csv_path, index=False)
print(f" ✓ {name}: {len(df)} records → CSV")
return df
return None
if __name__ == "__main__":
print("\n=== yeye: OSM Data Collection ===\n")
results = {}
for name, query in QUERIES.items():
count = fetch_osm(name, query)
results[name] = count
time.sleep(5) # Be polite to Overpass API
print("\n--- Parsing to CSV ---")
dfs = {}
for name in QUERIES:
if name != "roads_main":
df = parse_to_csv(name)
if df is not None:
dfs[name] = df
# Merge all point features into one master file
if dfs:
master = pd.concat(dfs.values(), ignore_index=True)
master_path = os.path.join(OUTPUT_DIR, "togo_infrastructure_master.csv")
master.to_csv(master_path, index=False)
print(f"\n✓ Master file: {len(master)} total infrastructure points")
print(f" Saved to: {master_path}")
print("\n=== Summary ===")
for name, count in results.items():
print(f" {name}: {count} elements")