Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / generate_cities_bin.py
generate_cities_bin.py94 lines · 3.0 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """Offline generator: cities.csv -> cities.bin (packed binary). |
| 3 | |
| 4 | Reads the full geonames-style cities.csv and emits a compact binary asset |
| 5 | containing only (name -> timezone) pairs for cities with population > 100k, |
| 6 | with an interned timezone string table. |
| 7 | |
| 8 | Binary format (all integers big-endian to match java.io.DataInputStream): |
| 9 | magic : 4 bytes = b"CTB1" |
| 10 | tzCount : int32 |
| 11 | repeat tzCount: |
| 12 | tzLen : uint8 (timezone id length in UTF-8 bytes) |
| 13 | tzBytes : UTF-8 |
| 14 | cityCount : int32 |
| 15 | repeat cityCount: |
| 16 | nameLen : uint16 (city name length in UTF-8 bytes) |
| 17 | nameBytes : UTF-8 |
| 18 | tzIndex : uint16 (index into the timezone table) |
| 19 | |
| 20 | City records are emitted in the SAME order as the source CSV so that duplicate |
| 21 | city names resolve identically to the original `associate { name to tz }` |
| 22 | (last occurrence wins). |
| 23 | """ |
| 24 | |
| 25 | import csv |
| 26 | import struct |
| 27 | import sys |
| 28 | from pathlib import Path |
| 29 | |
| 30 | NAME_COL = 1 |
| 31 | POP_COL = 14 |
| 32 | TZ_COL = 15 |
| 33 | POP_THRESHOLD = 100_000 |
| 34 | |
| 35 | |
| 36 | def generate(csv_path: Path, bin_path: Path) -> None: |
| 37 | tz_table: list[str] = [] |
| 38 | tz_index: dict[str, int] = {} |
| 39 | cities: list[tuple[str, int]] = [] |
| 40 | |
| 41 | with csv_path.open("r", encoding="utf-8", newline="") as f: |
| 42 | reader = csv.reader(f) |
| 43 | next(reader, None) # drop header |
| 44 | for row in reader: |
| 45 | if len(row) <= TZ_COL: |
| 46 | continue |
| 47 | pop_raw = row[POP_COL] |
| 48 | try: |
| 49 | pop = float(pop_raw) |
| 50 | except (ValueError, TypeError): |
| 51 | continue |
| 52 | if pop <= POP_THRESHOLD: |
| 53 | continue |
| 54 | name = row[NAME_COL] |
| 55 | tz = row[TZ_COL] |
| 56 | idx = tz_index.get(tz) |
| 57 | if idx is None: |
| 58 | idx = len(tz_table) |
| 59 | tz_index[tz] = idx |
| 60 | tz_table.append(tz) |
| 61 | cities.append((name, idx)) |
| 62 | |
| 63 | with bin_path.open("wb") as out: |
| 64 | out.write(b"CTB1") |
| 65 | out.write(struct.pack(">i", len(tz_table))) |
| 66 | for tz in tz_table: |
| 67 | b = tz.encode("utf-8") |
| 68 | if len(b) > 0xFF: |
| 69 | raise ValueError(f"timezone too long: {tz!r}") |
| 70 | out.write(struct.pack(">B", len(b))) |
| 71 | out.write(b) |
| 72 | out.write(struct.pack(">i", len(cities))) |
| 73 | for name, idx in cities: |
| 74 | b = name.encode("utf-8") |
| 75 | if len(b) > 0xFFFF: |
| 76 | raise ValueError(f"name too long: {name!r}") |
| 77 | out.write(struct.pack(">H", len(b))) |
| 78 | out.write(b) |
| 79 | out.write(struct.pack(">H", idx)) |
| 80 | |
| 81 | print(f"cities: {len(cities)} timezones: {len(tz_table)}") |
| 82 | print(f"wrote {bin_path} ({bin_path.stat().st_size} bytes)") |
| 83 | |
| 84 | |
| 85 | def main() -> None: |
| 86 | root = Path(__file__).resolve().parent.parent |
| 87 | assets = root / "clock" / "src" / "main" / "assets" |
| 88 | csv_path = Path(sys.argv[1]) if len(sys.argv) > 1 else assets / "cities.csv" |
| 89 | bin_path = Path(sys.argv[2]) if len(sys.argv) > 2 else assets / "cities.bin" |
| 90 | generate(csv_path, bin_path) |
| 91 | |
| 92 | |
| 93 | if __name__ == "__main__": |
| 94 | main() |