File size: 2,368 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
"""Download all 36 state district boundary GeoJSONs from udit-001/india-maps-data."""
import httpx
import json
import time
from pathlib import Path

OUT = Path(__file__).resolve().parents[2] / 'data' / 'civic_intel' / 'boundaries'
OUT.mkdir(parents=True, exist_ok=True)

BASE = 'https://raw.githubusercontent.com/udit-001/india-maps-data/main/geojson/states'

# All 36 states/UTs — filenames use hyphens
STATES = {
    'TN': 'tamil-nadu',
    'AP': 'andhra-pradesh',
    'UP': 'uttar-pradesh',
    'WB': 'west-bengal',
    'BR': 'bihar',
    'MP': 'madhya-pradesh',
    'OD': 'odisha',
    'JH': 'jharkhand',
    'AS': 'assam',
    'PB': 'punjab',
    'CG': 'chhattisgarh',
    'HR': 'haryana',
    'UK': 'uttarakhand',
    'HP': 'himachal-pradesh',
    'GA': 'goa',
    'MN': 'manipur',
    'ML': 'meghalaya',
    'MZ': 'mizoram',
    'NL': 'nagaland',
    'SK': 'sikkim',
    'TR': 'tripura',
    'AR': 'arunachal-pradesh',
    'JK': 'jammu-and-kashmir',
    'LA': 'ladakh',
    'CH': 'chandigarh',
    'PY': 'puducherry',
    'AN': 'andaman-and-nicobar-islands',
    'LD': 'lakshadweep',
    'DD': 'dnh-and-dd',
}

summary = {}
with httpx.Client(follow_redirects=True) as c:
    for code, slug in sorted(STATES.items()):
        outpath = OUT / f'{code.lower()}_districts.geojson'
        if outpath.exists() and outpath.stat().st_size > 100:
            data = json.loads(outpath.read_text(encoding='utf-8'))
            feats = len(data.get('features', []))
            print(f"SKIP {code}: already exists ({feats} features)")
            summary[code] = feats
            continue

        url = f'{BASE}/{slug}.geojson'
        try:
            r = c.get(url, timeout=60)
            if r.status_code == 200:
                data = r.json()
                feats = len(data.get('features', []))
                outpath.write_text(json.dumps(data), encoding='utf-8')
                kb = outpath.stat().st_size / 1024
                print(f"OK   {code}: {feats} features, {kb:.1f} KB")
                summary[code] = feats
            else:
                print(f"FAIL {code}: HTTP {r.status_code}")
                summary[code] = 0
        except Exception as e:
            print(f"ERR  {code}: {e}")
            summary[code] = 0
        time.sleep(0.3)

total = sum(summary.values())
print(f"\nTotal: {len(summary)} states, {total} district features")