File size: 5,994 Bytes
92cf271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""Fetch ward boundary polygons for major cities from OpenStreetMap.

Usage:
    python scripts/data/fetch_ward_boundaries.py [--city chennai]

Fetches admin boundary polygons at admin_level=10 (wards) from Overpass.
"""

from __future__ import annotations

import argparse
import json
import sys
import time
from pathlib import Path

try:
    import httpx
except ImportError:
    print("ERROR: httpx required. Run: pip install httpx")
    sys.exit(1)

PROJECT_ROOT = Path(__file__).resolve().parents[2]
OUTPUT_DIR = PROJECT_ROOT / 'data' / 'civic_intel' / 'ward_boundaries'
BBOXES_FILE = PROJECT_ROOT / 'data' / 'civic_intel' / 'city_bboxes.json'

OVERPASS_URL = 'https://overpass-api.de/api/interpreter'

# City-specific admin_level for ward boundaries
# India: admin_level 10 = ward, 8 = municipal corporation, 9 = zone
CITY_ADMIN_LEVELS = {
    'chennai': 10,
    'mumbai': 10,
    'delhi': 10,
    'bengaluru': 10,
    'hyderabad': 10,
    'kolkata': 10,
    'pune': 10,
    'ahmedabad': 10,
}


def build_ward_query(bbox: list[float], admin_level: int = 10) -> str:
    """Build Overpass query for ward boundaries."""
    s, w, n, e = bbox
    return f"""[out:json][timeout:180];
(
  relation["boundary"="administrative"]["admin_level"="{admin_level}"]({s},{w},{n},{e});
);
out body;
>;
out skel qt;"""


def elements_to_geojson(elements: list[dict]) -> dict:
    """Convert Overpass elements to GeoJSON FeatureCollection.
    
    This handles the raw node/way/relation format from Overpass.
    """
    # Index nodes by id
    nodes = {}
    ways_map = {}
    relations = []

    for el in elements:
        if el['type'] == 'node':
            nodes[el['id']] = [el['lon'], el['lat']]
        elif el['type'] == 'way':
            ways_map[el['id']] = el.get('nodes', [])
        elif el['type'] == 'relation':
            relations.append(el)

    features = []
    for rel in relations:
        tags = rel.get('tags', {})
        name = tags.get('name', tags.get('name:en', f'Ward {rel["id"]}'))

        # Build polygon from relation members
        outer_coords = []
        for member in rel.get('members', []):
            if member.get('role') == 'outer' and member.get('type') == 'way':
                way_id = member['ref']
                if way_id in ways_map:
                    way_nodes = ways_map[way_id]
                    coords = [nodes[nid] for nid in way_nodes if nid in nodes]
                    if coords:
                        outer_coords.extend(coords)

        if len(outer_coords) >= 3:
            # Close the polygon if needed
            if outer_coords[0] != outer_coords[-1]:
                outer_coords.append(outer_coords[0])

            feature = {
                'type': 'Feature',
                'properties': {
                    'ward_name': name,
                    'ward_number': tags.get('ref', tags.get('admin_ref', '')),
                    'admin_level': tags.get('admin_level', ''),
                    'osm_relation_id': rel['id'],
                    'name_local': tags.get('name:ta', tags.get('name:hi', tags.get('name:te', ''))),
                    'population': tags.get('population', ''),
                },
                'geometry': {
                    'type': 'Polygon',
                    'coordinates': [outer_coords],
                }
            }
            features.append(feature)

    return {
        'type': 'FeatureCollection',
        'features': features,
    }


def main():
    parser = argparse.ArgumentParser(description='Fetch ward boundary polygons')
    parser.add_argument('--city', default='chennai', help='City name (default: chennai)')
    parser.add_argument('--all', action='store_true', help='Fetch all configured cities')
    args = parser.parse_args()

    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

    if not BBOXES_FILE.exists():
        print(f"ERROR: {BBOXES_FILE} not found")
        sys.exit(1)

    with open(BBOXES_FILE, 'r', encoding='utf-8') as f:
        raw_bboxes = json.load(f)

    metro_data = raw_bboxes.get('metros', raw_bboxes)
    all_bboxes = {}
    for city_name, city_info in metro_data.items():
        if isinstance(city_info, dict) and 'bbox' in city_info:
            all_bboxes[city_name] = city_info['bbox']
        elif isinstance(city_info, list):
            all_bboxes[city_name] = city_info

    if args.all:
        cities = [c for c in CITY_ADMIN_LEVELS if c in all_bboxes]
    else:
        cities = [args.city]

    print()
    print('=' * 56)
    print('  SafeVixAI Ward Boundary Fetcher')
    print('=' * 56)

    for city in cities:
        if city not in all_bboxes:
            print(f"  SKIP {city}: no bbox data")
            continue

        admin_level = CITY_ADMIN_LEVELS.get(city, 10)
        print(f"\n  Fetching {city} (admin_level={admin_level})...")

        bbox = all_bboxes[city]
        query = build_ward_query(bbox, admin_level)

        try:
            with httpx.Client(follow_redirects=True) as c:
                r = c.get(
                    OVERPASS_URL,
                    params={'data': query},
                    headers={'User-Agent': 'SafeVixAI-CivicIntel/1.0'},
                    timeout=180,
                )
                r.raise_for_status()
                data = r.json()
                elements = data.get('elements', [])

                if elements:
                    geojson = elements_to_geojson(elements)
                    outfile = OUTPUT_DIR / f'{city}.geojson'
                    outfile.write_text(json.dumps(geojson), encoding='utf-8')
                    kb = outfile.stat().st_size / 1024
                    print(f"  OK  {city}: {len(geojson['features'])} wards, {kb:.1f} KB")
                else:
                    print(f"  EMPTY {city}: no ward boundaries found at admin_level={admin_level}")

        except Exception as e:
            print(f"  ERR  {city}: {e}")

        time.sleep(3)  # Overpass courtesy

    print()


if __name__ == '__main__':
    main()