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:00
Bash
ls -F
capetown_clinics.csv
0:11
Write
/work/solve.py
content · 55 lines · py
import pandas as pd
import json
def main():
# Load the data
df = pd.read_csv('capetown_clinics.csv')
# Assuming the CSV has latitude and longitude columns.
# Let's check column names first.
print("Columns:", df.columns.tolist())
# We need to identify the lat/lon columns and the subdistrict column.
# Based on the prompt, we expect 'latitude', 'longitude', and 'subdistrict' (or similar).
# I'll assume 'latitude', 'longitude', and 'subdistrict' for now,
# but I'll inspect the data if it fails.
# Check if common names exist.
lat_col = next((c for c in df.columns if 'lat' in c.lower()), None)
lon_col = next((c for c in df.columns if 'lon' in c.lower() or 'lng' in c.lower()), None)
sub_col = next((c for c in df.columns if 'subdistrict' in c.lower()), None)
if not lat_col or not lon_col or not sub_col:
# If common names aren't found, we might need to adjust.
# Let's print the first few rows to debug if this happens.
print(df.head())
raise ValueError(f"Could not automatically find lat/lon/subdistrict columns. Found: {df.columns.tolist()}")
# Total count
total_count = len(df)
# Bounding box [minx, miny, maxx, maxy]
min_lon = df[lon_col].min()
min_lat = df[lat_col].min()
max_lon = df[lon_col].max()
max_lat = df[lat_col].max()
bbox = [min_lon, min_lat, max_lon, max_lat]
# Count per subdistrict
counts_per_subdistrict = df[sub_col].value_counts().to_dict()
# Prepare JSON
output = {
"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(output, f, indent=4)
print("Successfully created clinic_inventory.json")
if __name__ == "__main__":
main()
Wrote /work/solve.py (1917 chars).
0:11
Bash
python solve.py
Columns: ['clinic_id', 'name', 'subdistrict', 'wkt_geom'] clinic_id ... wkt_geom 0 1 ... POINT(18.478309 -33.888756) 1 2 ... POINT(18.421127 -33.924929) 2 3 ... POINT(18.389237 -33.976099) 3 4 ... POINT(18.47354 -33.890933) 4 5 ... POINT(18.433735 -33.921306) [5 rows x 4 columns]
Traceback (most recent call last):
File "/work/solve.py", line 54, in <module>
main()
~~~~^^
File "/work/solve.py", line 26, in main
raise ValueError(f"Could not automatically find lat/lon/subdistrict columns. Found: {df.columns.tolist()}")
ValueError: Could not automatically find lat/lon/subdistrict columns. Found: ['clinic_id', 'name', 'subdistrict', 'wkt_geom']
0:19
Write
/work/solve.py
content · 47 lines · py
import pandas as pd
import json
from shapely import wkt
def main():
# Load the data
df = pd.read_csv('capetown_clinics.csv')
# The columns are: ['clinic_id', 'name', 'subdistrict', 'wkt_geom']
# We need to extract coordinates from 'wkt_geom'
# Convert WKT to shapely geometries
df['geometry'] = df['wkt_geom'].apply(wkt.loads)
# Total count
total_count = len(df)
# Bounding box [minx, miny, maxx, maxy]
# We can use the combined bounds of all geometries
all_bounds = df['geometry'].apply(lambda g: g.bounds)
# all_bounds returns (minx, miny, maxx, maxy) for each row
# Let's stack them and get min/max
min_lon = all_bounds.iloc[:, 0].min()
min_lat = all_bounds.iloc[:, 1].min()
max_lon = all_bounds.iloc[:, 2].max()
max_lat = all_bounds.iloc[:, 3].max()
bbox = [min_lon, min_lat, max_lon, max_lat]
# Count per subdistrict
sub_col = 'subdistrict'
counts_per_subdistrict = df[sub_col].value_counts().to_dict()
# Prepare JSON
output = {
"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(output, f, indent=4)
print("Successfully created clinic_inventory.json")
if __name__ == "__main__":
main()
Wrote /work/solve.py (1407 chars).
0:20
Bash
python solve.py
Traceback (most recent call last):
File "/work/solve.py", line 46, in <module>
main()
~~~~^^
File "/work/solve.py", line 23, in main
min_lon = all_bounds.iloc[:, 0].min()
~~~~~~~~~~~~~~~^^^^^^
File "/usr/local/lib/python3.14/site-packages/pandas/core/indexing.py", line 1200, in __getitem__
return self._getitem_tuple(key)
~~~~~~~~~~~~~~~~~~~^^^^^
File "/usr/local/lib/python3.14/site-packages/pandas/core/indexing.py", line 1711, in _getitem_tuple
tup = self._validate_tuple_indexer(tup)
File "/usr/local/lib/python3.14/site-packages/pandas/core/indexing.py", line 989, in _validate_tuple_indexer
key = self._validate_key_length(key)
File "/usr/local/lib/python3.14/site-packages/pandas/core/indexing.py", line 1027, in _validate_key_length
raise IndexingError("Too many indexers")
pandas.errors.IndexingError: Too many indexers
0:27
Write
/work/solve.py
content · 45 lines · py
import pandas as pd
import json
from shapely import wkt
def main():
# Load the data
df = pd.read_csv('capetown_clinics.csv')
# The columns are: ['clinic_id', 'name', 'subdistrict', 'wkt_geom']
# Convert WKT to shapely geometries
df['geometry'] = df['wkt_geom'].apply(wkt.loads)
# Total count
total_count = len(df)
# Bounding box [minx, miny, maxx, maxy]
# Let's calculate min and max manually to avoid pandas indexing issues
lons = []
lats = []
for geom in df['geometry']:
lons.append(geom.x)
lats.append(geom.y)
bbox = [min(lons), min(lats), max(lons), max(lats)]
# Count per subdistrict
sub_col = 'subdistrict'
counts_per_subdistrict = df[sub_col].value_counts().to_dict()
# Prepare JSON
output = {
"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(output, f, indent=4)
print("Successfully created clinic_inventory.json")
if __name__ == "__main__":
main()
Wrote /work/solve.py (1175 chars).
0:27
Bash
python solve.py
Successfully created clinic_inventory.json
0:30
Bash
ls -F
capetown_clinics.csv clinic_inventory.json solve.py
0:31
Read
clinic_inventory.json
1 {
2 "count": 80,
3 "bbox": [
4 18.380309,
5 -34.073855,
6 18.819549,
7 -33.701317
8 ],
9 "count_per_subdistrict": {
10 "Western": 12,
11 "Southern": 12,
12 "Tygerberg": 11,
13 "Northern": 10,
14 "Eastern": 10,
15 "Klipfontein": 9,
16 "Mitchells Plain": 8,
17 "Khayelitsha": 8
18 }
19 }
0:33