| """Inject MADS-format map_data into a USDZ for alpasim consumption. |
| |
| Reads from <mads_dir>: |
| clip.parquet, lane.parquet, road_boundary.parquet, |
| association.parquet, wait_line.parquet, traffic_sign.parquet |
| |
| Writes them into the USDZ at `map_data/<name>.parquet` (alpasim's loader scans |
| for any file under `map_data/` and feeds the directory to trajdata). |
| """ |
| from __future__ import annotations |
|
|
| import argparse |
| import shutil |
| import sys |
| import zipfile |
| from pathlib import Path |
|
|
| MADS_FILES = [ |
| "clip.parquet", |
| "lane.parquet", |
| "road_boundary.parquet", |
| "association.parquet", |
| "wait_line.parquet", |
| "traffic_sign.parquet", |
| ] |
|
|
|
|
| def inject(src_usdz: Path, mads_dir: Path, dst_usdz: Path) -> None: |
| |
| if src_usdz != dst_usdz: |
| shutil.copy2(src_usdz, dst_usdz) |
|
|
| |
| |
| if dst_usdz != src_usdz: |
| |
| with zipfile.ZipFile(dst_usdz, "r") as zin: |
| existing = set(zin.namelist()) |
| has_old_map = any(n.startswith("map_data/") for n in existing) |
| else: |
| has_old_map = False |
|
|
| if has_old_map: |
| |
| tmp = dst_usdz.with_suffix(".tmp.usdz") |
| with zipfile.ZipFile(dst_usdz, "r") as zin, \ |
| zipfile.ZipFile(tmp, "w", compression=zipfile.ZIP_STORED) as zout: |
| for info in zin.infolist(): |
| if info.filename.startswith("map_data/"): |
| continue |
| zout.writestr(info, zin.read(info.filename)) |
| tmp.replace(dst_usdz) |
|
|
| |
| with zipfile.ZipFile(dst_usdz, "a", compression=zipfile.ZIP_STORED) as zf: |
| for name in MADS_FILES: |
| src_file = mads_dir / name |
| if not src_file.exists(): |
| raise FileNotFoundError(src_file) |
| arc = f"map_data/{name}" |
| zf.write(src_file, arcname=arc) |
| print(f" added: {arc} ({src_file.stat().st_size} bytes)") |
|
|
|
|
| def main() -> int: |
| p = argparse.ArgumentParser() |
| p.add_argument("src", type=Path, help="Source USDZ") |
| p.add_argument("mads_dir", type=Path, |
| help="Dir with MADS files (clip/lane/road_boundary/association/wait_line/traffic_sign.parquet)") |
| p.add_argument("dst", type=Path, help="Destination USDZ") |
| args = p.parse_args() |
|
|
| print(f"inject {args.mads_dir} -> {args.src} => {args.dst}") |
| inject(args.src, args.mads_dir, args.dst) |
| print(f"wrote {args.dst} ({args.dst.stat().st_size} bytes)") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|