File size: 2,358 Bytes
d710a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
sample_pmgsy.py
===============
Samples a representative subset of PMGSY roads from the full
pmgsy_roads.geojson (867K features) and writes a smaller GeoJSON
that import_official_road_sources.py can import without timing out.

Strategy: Take up to `max_per_state` roads per state so all 29 states
are represented, then cap the total at `total_limit`.

Usage:
    cd backend/
    python scripts/sample_pmgsy.py [--limit 5000] [--per-state 200]
"""
from __future__ import annotations

import argparse
import json
from collections import defaultdict
from pathlib import Path

ROOT       = Path(__file__).resolve().parents[1]
CHATBOT    = ROOT.parent / "chatbot_service" / "data"
SRC        = CHATBOT / "roads" / "pmgsy_roads.geojson"
OUT_DIR    = ROOT / "datasets" / "roads"
OUT_DIR.mkdir(parents=True, exist_ok=True)
OUT        = OUT_DIR / "pmgsy_sampled.geojson"


def sample(total_limit: int = 5000, per_state: int = 200) -> None:
    print(f"Loading {SRC.name} ... (this takes ~30s for 867K features)")
    with SRC.open(encoding="utf-8") as fh:
        data = json.load(fh)

    all_features = data.get("features", [])
    print(f"Total features: {len(all_features):,}")

    buckets: dict[str, list] = defaultdict(list)
    for feat in all_features:
        state = feat.get("properties", {}).get("pmgsy_state", "Unknown")
        buckets[state].append(feat)

    selected = []
    for state, feats in sorted(buckets.items()):
        chosen = feats[:per_state]
        selected.extend(chosen)
        if len(selected) >= total_limit:
            break

    selected = selected[:total_limit]
    print(f"Selected {len(selected):,} features from {len(buckets)} states")

    fc = {"type": "FeatureCollection", "features": selected}
    OUT.write_text(json.dumps(fc, ensure_ascii=False), encoding="utf-8")
    size_mb = OUT.stat().st_size / 1_048_576
    print(f"Written: {OUT.relative_to(ROOT)}  ({size_mb:.1f} MB)")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--limit",     type=int, default=5000, help="Max total roads")
    parser.add_argument("--per-state", type=int, default=200,  help="Max roads per state")
    args = parser.parse_args()
    sample(args.limit, args.per_state)
    print("\nDone. Now update scripts/road_sources.json to use:")
    print(f"  datasets/roads/pmgsy_sampled.geojson")