File size: 7,741 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
193
194
195
196
197
198
199
200
201
202
203
204
205
"""Fetch road network classification data from OpenStreetMap Overpass API.

Usage:
    python scripts/data/fetch_road_network.py [--cities mumbai,chennai] [--all]

Fetches road classification data (motorway/trunk/primary/secondary/tertiary/residential)
and maps each to Indian road authority ownership.
"""

from __future__ import annotations

import argparse
import csv
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' / 'road_network'
BBOXES_FILE = PROJECT_ROOT / 'data' / 'civic_intel' / 'city_bboxes.json'

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

# Indian road authority mapping
AUTHORITY_MAP = {
    'motorway': {'authority': 'NHAI', 'category': 'Expressway', 'maintenance': 'Central'},
    'motorway_link': {'authority': 'NHAI', 'category': 'Expressway Ramp', 'maintenance': 'Central'},
    'trunk': {'authority': 'NHAI', 'category': 'National Highway', 'maintenance': 'Central'},
    'trunk_link': {'authority': 'NHAI', 'category': 'NH Ramp', 'maintenance': 'Central'},
    'primary': {'authority': 'State PWD', 'category': 'State Highway', 'maintenance': 'State'},
    'primary_link': {'authority': 'State PWD', 'category': 'SH Ramp', 'maintenance': 'State'},
    'secondary': {'authority': 'State PWD', 'category': 'Major District Road', 'maintenance': 'State'},
    'secondary_link': {'authority': 'State PWD', 'category': 'MDR Link', 'maintenance': 'State'},
    'tertiary': {'authority': 'Municipal Corporation', 'category': 'Other District Road', 'maintenance': 'Municipal'},
    'tertiary_link': {'authority': 'Municipal Corporation', 'category': 'ODR Link', 'maintenance': 'Municipal'},
    'residential': {'authority': 'Municipal Corporation', 'category': 'Residential Street', 'maintenance': 'Municipal'},
    'unclassified': {'authority': 'Municipal/Panchayat', 'category': 'Village Road', 'maintenance': 'Local'},
    'living_street': {'authority': 'Municipal Corporation', 'category': 'Residential', 'maintenance': 'Municipal'},
    'service': {'authority': 'Private/Municipal', 'category': 'Service Road', 'maintenance': 'Private'},
}

ROAD_TYPES = ['motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'residential']


def build_road_query(bbox: list[float]) -> str:
    """Build Overpass query for road network."""
    s, w, n, e = bbox
    type_filter = '|'.join(ROAD_TYPES)
    return f"""[out:json][timeout:120];
(
  way["highway"~"^({type_filter})(_link)?$"]({s},{w},{n},{e});
);
out tags center;"""


def fetch_roads(city: str, bbox: list[float]) -> list[dict]:
    """Fetch road data for a city."""
    query = build_road_query(bbox)
    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=120,
            )
            r.raise_for_status()
            data = r.json()
            elements = data.get('elements', [])

            roads = []
            for el in elements:
                tags = el.get('tags', {})
                highway = tags.get('highway', '')
                center = el.get('center', {})

                auth_info = AUTHORITY_MAP.get(highway, {
                    'authority': 'Unknown',
                    'category': highway,
                    'maintenance': 'Unknown',
                })

                roads.append({
                    'osm_id': el.get('id', 0),
                    'name': tags.get('name', tags.get('ref', '')),
                    'name_local': tags.get('name:hi', tags.get('name:ta', tags.get('name:te', ''))),
                    'highway_type': highway,
                    'road_category': auth_info['category'],
                    'authority': auth_info['authority'],
                    'maintenance_level': auth_info['maintenance'],
                    'ref': tags.get('ref', ''),
                    'lanes': tags.get('lanes', ''),
                    'surface': tags.get('surface', ''),
                    'maxspeed': tags.get('maxspeed', ''),
                    'oneway': tags.get('oneway', ''),
                    'lit': tags.get('lit', ''),
                    'center_lat': center.get('lat', ''),
                    'center_lon': center.get('lon', ''),
                    'city': city,
                })
            return roads
    except Exception as e:
        print(f"  ERR {city}: {e}")
        return []


def main():
    parser = argparse.ArgumentParser(description='Fetch road network classification')
    parser.add_argument('--cities', help='Comma-separated city names')
    parser.add_argument('--all', action='store_true', help='Process all cities')
    args = parser.parse_args()

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

    # Load bboxes
    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.cities:
        cities = [c.strip() for c in args.cities.split(',')]
    elif args.all:
        cities = sorted(all_bboxes.keys())
    else:
        cities = sorted(all_bboxes.keys())[:5]  # Default: top 5

    print()
    print('=' * 56)
    print('  SafeVixAI Road Network Classifier')
    print('=' * 56)
    print(f'  Cities: {len(cities)}')
    print(f'  Output: {OUTPUT_DIR}')
    print()

    summary = {}
    csv_fields = [
        'osm_id', 'name', 'name_local', 'highway_type', 'road_category',
        'authority', 'maintenance_level', 'ref', 'lanes', 'surface',
        'maxspeed', 'oneway', 'lit', 'center_lat', 'center_lon', 'city'
    ]

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

        print(f"[{i+1}/{len(cities)}] Fetching {city}...")
        roads = fetch_roads(city, all_bboxes[city])

        if roads:
            outfile = OUTPUT_DIR / f'{city}_roads.csv'
            with open(outfile, 'w', newline='', encoding='utf-8') as f:
                writer = csv.DictWriter(f, fieldnames=csv_fields)
                writer.writeheader()
                writer.writerows(roads)

            # Count by type
            by_type = {}
            for r in roads:
                t = r['highway_type']
                by_type[t] = by_type.get(t, 0) + 1
            summary[city] = {
                'total': len(roads),
                'by_type': by_type,
            }
            top_types = sorted(by_type.items(), key=lambda x: x[1], reverse=True)[:3]
            top_str = ', '.join(f'{t}={c}' for t, c in top_types)
            print(f"  OK  {city}: {len(roads):,} roads [{top_str}]")
        else:
            print(f"  EMPTY {city}")

        time.sleep(2)  # Be respectful to Overpass

    # Save summary
    summary_file = OUTPUT_DIR / 'road_network_summary.json'
    summary_file.write_text(json.dumps(summary, indent=2), encoding='utf-8')

    total = sum(v['total'] for v in summary.values())
    print()
    print(f'  Total: {len(summary)} cities, {total:,} road segments')
    print(f'  Output: {OUTPUT_DIR}')
    print()


if __name__ == '__main__':
    main()