dd-l1-capetown-clinics-bbox
Map (reference ← swipe → agent)
0:00
I'm about to push the `capetown_clinics.csv` export through to our case-management system and I want a quick inventory check first. Can you produce a small JSON file with the total clinic count, the overall bounding box as a bbox array, and the count per subdistrict, so I can confirm every health subdistrict is represented? Please write it to `clinic_inventory.json` with keys `count`, `bbox`, and `count_per_subdistrict`.
0:00
System info
{
"adapter": "openrouter",
"model": "google/gemma-4-26b-a4b-it",
"base_url": "https://openrouter.ai/api/v1"
}
0:02
Bash
ls -F
capetown_clinics.csv
0:53
Write
/work/solve.py
content · 68 lines · py
import pandas as pd
import json
def main():
# Load the CSV
df = pd.read_csv('capetown_clinics.csv')
# Total clinic count
total_count = len(df)
# Bounding box
# Assuming columns are 'latitude' and 'longitude' or similar.
# Let's check columns first.
print("Columns in CSV:", df.columns.tolist())
# We need to identify lat/lon columns. Common names: latitude, longitude, lat, lon
lat_col = None
lon_col = None
for col in df.columns:
if col.lower() in ['latitude', 'lat']:
lat_col = col
if col.lower() in ['longitude', 'lon']:
lon_col = col
if not lat_col or not lon_col:
print(f"Error: Could not find lat/lon columns. Found: {df.columns.tolist()}")
return
min_lat = df[lat_col].min()
max_lat = df[lat_col].max()
min_lon = df[lon_col].min()
max_lon = df[lon_col].max()
# bbox format: [min_lon, min_lat, max_lon, max_lat] or [min_lat, min_lon, max_lat, max_lon]
# Usually, bbox is [min_x, min_y, max_x, max_y] which is [min_lon, min_lat, max_lon, max_lat]
bbox = [min_lon, min_lat, max_lon, max_lat]
# Count per subdistrict
# We need to identify the subdistrict column.
# Let's guess common names: 'subdistrict', 'health_subdistrict', 'sub_district'
subdistrict_col = None
for col in df.columns:
if 'subdistrict' in col.lower():
subdistrict_col = col
break
if not subdistrict_col:
print(f"Error: Could not find subdistrict column. Found: {df.columns.tolist()}")
return
counts_per_subdistrict = df[subdistrict_col].value_counts().to_dict()
# Prepare JSON
inventory = {
"count": int(total_count),
"bbox": bbox,
"count_per_subdistrict": {str(k): int(v) for k, v in counts_per_subdistrict.items()}
}
with open('clinic_inventory.json', 'w') as f:
json.dump(inventory, f, indent=4)
print("Successfully created clinic_inventory.json")
if __name__ == "__main__":
main()
Wrote /work/solve.py (2110 chars).
0:54
Bash
python solve.py
Columns in CSV: ['clinic_id', 'name', 'subdistrict', 'wkt_geom'] Error: Could not find lat/lon columns. Found: ['clinic_id', 'name', 'subdistrict', 'wkt_geom']
1:00
Write
/work/solve.py
content · 65 lines · py
import pandas as pd
import json
from shapely import wkt
def main():
# Load the CSV
df = pd.read_csv('capetown_clinics.csv')
# Total clinic count
total_count = len(df)
# Identify geometry column
geom_col = None
for col in df.columns:
if 'wkt' in col.lower() or 'geom' in col.lower():
geom_col = col
break
if not geom_col:
print(f"Error: Could not find geometry column. Found: {df.columns.tolist()}")
return
# Parse geometries and calculate bbox
# We can use geopandas for easier handling or just shapely
geoms = df[geom_col].apply(wkt.loads)
min_x, min_y, max_x, max_y = float('inf'), float('inf'), float('-inf'), float('-inf')
for geom in geoms:
bounds = geom.bounds # (minx, miny, maxx, maxy)
if bounds[0] < min_x: min_x = bounds[0]
if bounds[1] < min_y: min_y = bounds[1]
if bounds[2] > max_x: max_x = bounds[2]
if bounds[3] > max_y: max_y = bounds[3]
bbox = [min_x, min_y, max_x, max_y]
# Count per subdistrict
subdistrict_col = None
for col in df.columns:
if 'subdistrict' in col.lower():
subdistrict_col = col
break
if not subdistrict_col:
print(f"Error: Could not find subdistrict column. Found: {df.columns.tolist()}")
return
counts_per_subdistrict = df[subdistrict_col].value_counts().to_dict()
# Prepare JSON
inventory = {
"count": int(total_count),
"bbox": bbox,
"count_per_subdistrict": {str(k): int(v) for k, v in counts_per_subdistrict.items()}
}
with open('clinic_inventory.json', 'w') as f:
json.dump(inventory, f, indent=4)
print("Successfully created clinic_inventory.json")
if __name__ == "__main__":
main()
Wrote /work/solve.py (1881 chars).
1:01
Bash
python solve.py
Successfully created clinic_inventory.json
1:03
Bash
cat clinic_inventory.json
{
"count": 80,
"bbox": [
18.380309,
-34.073855,
18.819549,
-33.701317
],
"count_per_subdistrict": {
"Western": 12,
"Southern": 12,
"Tygerberg": 11,
"Northern": 10,
"Eastern": 10,
"Klipfontein": 9,
"Mitchells Plain": 8,
"Khayelitsha": 8
}
}
1:18