#!/usr/bin/env python3 """Build timezone_grid.bin from timezone-boundary-builder's timezones.geojson: timezone_grid.py combined.json [out]""" import json import lzma import math import struct import sys from pathlib import Path import numpy as np from PIL import Image, ImageDraw from openpilot.common.timezone_lookup import COLS, ROWS, MAGIC, RES, DEFAULT_PATH def polygons(geometry): if geometry["type"] == "Polygon": return [geometry["coordinates"]] return geometry["coordinates"] def to_px(ring): return [((lon + 180.) / RES, (90. - lat) / RES) for lon, lat in ring] def area(ring): xs, ys = np.asarray(ring).T return abs(np.dot(xs, np.roll(ys, 1)) - np.dot(ys, np.roll(xs, 1))) / 2 def build(features): names = sorted({f["properties"]["tzid"] for f in features}) index = {name: i + 1 for i, name in enumerate(names)} shapes = [(area(poly[0]), index[f["properties"]["tzid"]], poly) for f in features for poly in polygons(f["geometry"])] # Largest first so enclaves drawn later win over the zone around them. shapes.sort(key=lambda s: -s[0]) img = Image.new("I", (COLS, ROWS), 0) for _, zone, poly in shapes: exterior = to_px(poly[0]) xs, ys = zip(*exterior, strict=True) left, top = max(0, math.floor(min(xs))), max(0, math.floor(min(ys))) right, bottom = min(COLS, math.floor(max(xs)) + 1), min(ROWS, math.floor(max(ys)) + 1) if right <= left or bottom <= top: continue mask = Image.new("1", (right - left, bottom - top)) draw = ImageDraw.Draw(mask) for i, ring in enumerate(poly): draw.polygon([(x - left, y - top) for x, y in to_px(ring)], fill=int(i == 0)) img.paste(zone, (left, top, right, bottom), mask) grid = np.asarray(img, dtype=np.int32) ends, zones, offsets = [], [], [0] for row in grid: change = np.flatnonzero(np.diff(row)) + 1 ends.extend(np.append(change, COLS).tolist()) zones.extend(row[np.insert(change, 0, 0)].tolist()) offsets.append(len(ends)) name_blob = "\n".join(names).encode() raw = b"".join([ MAGIC, struct.pack(" 2 else DEFAULT_PATH with open(sys.argv[1]) as f: data = build(json.load(f)["features"]) out.write_bytes(data) print(f"wrote {out} ({len(data) / 1e6:.2f} MB)")