dd-l1-capetown-clinics-bbox

Status: done
Score: 1.000
Duration: 1:02
Cost: 0.15¢
Model: google/gemma-4-26b-a4b-it

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:03
Bash ls -F
capetown_clinics.csv
0:20
Write /work/solve.py
content · 60 lines · py
import pandas as pd
import json

def main():
    # Load the data
    try:
        df = pd.read_csv('capetown_clinics.csv')
    except Exception as e:
        print(f"Error reading CSV: {e}")
        return

    # Total clinic count
    total_count = len(df)

    # Calculate Bounding Box
    # Assuming columns are 'latitude' and 'longitude' or similar. 
    # Let's check columns first.
    print(f"Columns: {df.columns.tolist()}")
    
    # Find lat/lon columns
    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)

    if not lat_col or not lon_col:
        print("Could not find latitude or longitude columns.")
        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 = [min_lon, min_lat, max_lon, max_lat] # [minx, miny, maxx, maxy]

    # Count per subdistrict
    # Assuming 'subdistrict' is the column name or similar.
    subdistrict_col = next((c for c in df.columns if 'subdistrict' in c.lower()), None)
    
    if not subdistrict_col:
        print("Could not find subdistrict column.")
        # If not found, let's just list all columns to help debug
        return

    counts_per_subdistrict = df[subdistrict_col].value_counts().to_dict()

    # Construct the JSON output
    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("Inventory file 'clinic_inventory.json' created successfully.")

if __name__ == "__main__":
    main()
Wrote /work/solve.py (1790 chars).
0:21
Bash python solve.py
Columns: ['clinic_id', 'name', 'subdistrict', 'wkt_geom']
Could not find latitude or longitude columns.
0:38
Write /work/solve.py
content · 63 lines · py
import pandas as pd
import json
from shapely import wkt

def main():
    # Load the data
    try:
        df = pd.read_csv('capetown_clinics.csv')
    except Exception as e:
        print(f"Error reading CSV: {e}")
        return

    # Total clinic count
    total_count = len(df)

    # We have 'wkt_geom' column which contains geometries
    if 'wkt_geom' not in df.columns:
        print("Could not find 'wkt_geom' column.")
        return

    # Convert WKT to shapely objects
    df['geometry'] = df['wkt_geom'].apply(wkt.loads)

    # Calculate Bounding Box
    # Get bounds of all geometries combined
    all_geoms = df['geometry'].tolist()
    
    if not all_geoms:
        print("No geometries found.")
        return

    # Calculate the bounding box manually or via a collection
    from shapely.ops import unary_union
    combined = unary_union(all_geoms)
    minx, miny, maxx, maxy = combined.bounds
    
    bbox = [minx, miny, maxx, maxy]

    # Count per subdistrict
    subdistrict_col = 'subdistrict'
    if subdistrict_col not in df.columns:
        print(f"Could not find subdistrict column. Available: {df.columns.tolist()}")
        return

    counts_per_subdistrict = df[subdistrict_col].value_counts().to_dict()

    # Construct the JSON output
    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(f"Inventory file 'clinic_inventory.json' created successfully.")
    print(f"Total Count: {total_count}")
    print(f"BBox: {bbox}")

if __name__ == "__main__":
    main()
Wrote /work/solve.py (1726 chars).
0:39
Bash python solve.py
Inventory file 'clinic_inventory.json' created successfully.
Total Count: 80
BBox: [18.380309, -34.073855, 18.819549, -33.701317]
0:42
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:01