#!/usr/bin/env python3 """Real-world City maps for JustRoads: places/.txt (level-file format) and a preview PNG per place. python3 tools/places/make_places.py # every place python3 tools/places/make_places.py sydney paris # only these python3 tools/places/make_places.py --offline # never touch the network (cache only) python3 tools/places/make_places.py --debug # also write hi-res debug images to cache/debug/ Data (both free, no API key; every download is cached under tools/places/cache/, which is git-ignored): * elevation and sea: Terrarium terrain tiles (Mapzen, hosted by AWS Open Data), https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png, elevation = R*256 + G + B/256 - 32768 meters. Sea is where the elevation is <= the place's sea level (0 unless tuned) -- but see below. * rivers, lakes, docks and the coastline: OpenStreetMap through the Overpass API, ONE query per place. If Overpass fails after the retries the place falls back to elevation only and says so. The Terrarium sea mask alone turned out to be poor in most harbors (New York's Upper Bay, Victoria Harbour, the Golden Horn and the Venice lagoon come out as a checkerboard of values just above and below 0 m), so by default the sea is taken from the OSM coastline wherever the box has one (see coast_sea) and the elevation rule only fills what the coastline does not decide. A place can ask for the pure elevation rule with sea="elev". Every map is 64x64 cells, north up, square on the ground (Web Mercator is conformal, so a square in tile pixels is a square in meters at the place's latitude). Terrain per cell: '.' grass, '~' water, '^' hill, '#' rock. Needs Python 3.8+, Pillow and numpy. See places/README.md for the licenses and the attribution the app must show. """ import hashlib import io import json import math import os import sys import time import urllib.error import urllib.parse import urllib.request from collections import deque import numpy as np from PIL import Image, ImageDraw, ImageFont HERE = os.path.dirname(os.path.abspath(__file__)) REPO = os.path.dirname(os.path.dirname(HERE)) OUT_DIR = os.path.join(REPO, "places") PREVIEW_DIR = os.path.join(HERE, "preview") CACHE_DIR = os.path.join(HERE, "cache") USER_AGENT = "places-mapgen/1.0 (offline terrain grid generator; python-urllib)" TILE_URL = "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png" OVERPASS_URLS = ["https://overpass-api.de/api/interpreter", "https://overpass.kumi.systems/api/interpreter"] CREDIT = "Terrain: Mapzen/AWS Terrain Tiles; water: © OpenStreetMap contributors" N = 64 # cells per side SUB = 8 # elevation / water samples per cell side HI = N * SUB # 512 samples per side GRASS, WATER, HILL, ROCK = 0, 1, 2, 3 GLYPH = {GRASS: ".", WATER: "~", HILL: "^", ROCK: "#"} COLOR = {GRASS: (0xE8, 0xE3, 0xD5), WATER: (0xA9, 0xD1, 0xEA), HILL: (0xC9, 0xB9, 0xA0), ROCK: (0x8E, 0x8C, 0x88)} # Per-place parameters (all optional except id, name, lat, lon, span_km): # lat, lon center of the box; span_km side of the box on the ground # sea "coast" (default): sea from the OSM coastline (flood fill from the seaward side of every coastline way); # the elevation rule still decides the samples no coastline reaches, and everything if the box has no # coastline or Overpass failed. "elev": sea = elevation <= sea_level only. # "none": no sea at all (inland cities whose low ground would read as sea). # sea_level meters (default 0) # water_frac share of a cell's samples that must be water for a water cell (default 0.5) # thin names of OSM water polygons that count from thin_frac of a cell (default 0.2) instead of water_frac: a canal # or channel narrower than a cell that the place is known by (Venice's Canal Grande) # lines Overpass filters for waterways drawn as a 1-cell line even where narrower than a cell (a defining river) # min_water water bodies smaller than this many cells are dropped (default 3); min_land islands likewise (default 2) # hill_frac share of the land that may become hill before the clean-up (default 0.14) # hill_floor lowest hill score that counts (default 0.5; score = slope / 0.10 + height above the local base / 60 m), so a # flat city gets few or no hills instead of noise # rock_slope mean slope of a rock cell (default 0.30), rock_max share of the land that may be rock (default 0.02) PLACES = [ dict(id="sydney", name="Sydney Harbour", lat=-33.870, lon=151.220, span_km=18), dict(id="san-francisco", name="San Francisco", lat=37.780, lon=-122.390, span_km=22), dict(id="new-york", name="New York Harbor", lat=40.700, lon=-73.960, span_km=22), dict(id="venice", name="Venice Lagoon", lat=45.457, lon=12.300, span_km=12, water_frac=0.62, thin=["Canal Grande"]), dict(id="hong-kong", name="Hong Kong", lat=22.317, lon=114.170, span_km=16), dict(id="istanbul", name="Istanbul", lat=41.030, lon=28.985, span_km=15), dict(id="stockholm", name="Stockholm", lat=59.342, lon=18.070, span_km=10.5, water_frac=0.42, thin=["Djurgårdsbrunnskanalen"]), dict(id="rio", name="Rio de Janeiro", lat=-22.900, lon=-43.190, span_km=26), dict(id="vancouver", name="Vancouver", lat=49.265, lon=-123.100, span_km=22), dict(id="cape-town", name="Cape Town", lat=-34.000, lon=18.470, span_km=36), dict(id="seattle", name="Seattle", lat=47.610, lon=-122.330, span_km=22), dict(id="london", name="London", lat=51.520, lon=-0.050, span_km=16, lines=['way["waterway"="river"]["name"="River Thames"]']), dict(id="paris", name="Paris", lat=48.890, lon=2.340, span_km=20, lines=['way["waterway"="river"]["name"~"^(La )?Seine$"]']), dict(id="hobart", name="Hobart", lat=-42.870, lon=147.310, span_km=22), dict(id="lisbon", name="Lisbon", lat=38.725, lon=-9.170, span_km=26), dict(id="auckland", name="Auckland", lat=-36.905, lon=174.760, span_km=38), ] OFFLINE = False DEBUG = False def log(msg): print(msg, flush=True) # ---------------------------------------------------------------- downloads def http_get(url, data=None, timeout=180): req = urllib.request.Request(url, data=data, headers={"User-Agent": USER_AGENT}) with urllib.request.urlopen(req, timeout=timeout) as r: return r.read() def fetch_tile(z, x, y): path = os.path.join(CACHE_DIR, "terrarium", str(z), str(x), "%d.png" % y) if not os.path.exists(path): if OFFLINE: raise RuntimeError("tile %d/%d/%d is not cached and --offline is set" % (z, x, y)) os.makedirs(os.path.dirname(path), exist_ok=True) url = TILE_URL.format(z=z, x=x, y=y) for attempt in range(4): try: blob = http_get(url, timeout=60) break except (urllib.error.URLError, OSError) as e: if attempt == 3: raise time.sleep(2 * 3 ** attempt) with open(path + ".part", "wb") as f: f.write(blob) os.replace(path + ".part", path) time.sleep(0.05) rgb = np.asarray(Image.open(path).convert("RGB"), dtype=np.float64) return rgb[:, :, 0] * 256.0 + rgb[:, :, 1] + rgb[:, :, 2] / 256.0 - 32768.0 OVERPASS_PAD = 0.05 # degrees: queries are rounded outward to this grid, so nudging a box reuses the cached reply def overpass_query(place, bbox): s, w, n, e = bbox parts = [ 'way["natural"="water"]', 'relation["natural"="water"]', 'way["waterway"="riverbank"]', 'relation["waterway"="riverbank"]', 'way["waterway"="dock"]', 'relation["waterway"="dock"]', 'way["natural"="coastline"]', ] + list(place.get("lines", [])) filters = "".join(p + ";" for p in parts) return "[out:json][timeout:150][bbox:%.5f,%.5f,%.5f,%.5f];(%s);out geom;" % (s, w, n, e, filters), hashlib.sha1(filters.encode("utf-8")).hexdigest()[:12] def fetch_overpass(place, bbox): """ One query per place: water polygons, the coastline and the place's line features, with full geometry. Returns the element list, or None if Overpass could not be reached. Replies are cached; a cached reply for a box that contains the one asked for (same place, same filters) is reused, since "out geom" returns whole ways whatever the box. """ folder = os.path.join(CACHE_DIR, "overpass") os.makedirs(folder, exist_ok=True) index_path = os.path.join(folder, "index.json") index = {} if os.path.exists(index_path): with open(index_path, "r", encoding="utf-8") as f: index = json.load(f) def remember(name, box, filters_key): index[name] = dict(id=place["id"], bbox=list(box), filters=filters_key) with open(index_path, "w", encoding="utf-8") as f: json.dump(index, f, indent=1, sort_keys=True) query, filters_key = overpass_query(place, bbox) exact = "%s-%s.json" % (place["id"], hashlib.sha1(query.encode("utf-8")).hexdigest()[:12]) if exact not in index and os.path.exists(os.path.join(folder, exact)): remember(exact, bbox, filters_key) for name, meta in sorted(index.items()): b = meta["bbox"] if meta["id"] == place["id"] and meta["filters"] == filters_key and b[0] <= bbox[0] and b[1] <= bbox[1] and b[2] >= bbox[2] and b[3] >= bbox[3] \ and os.path.exists(os.path.join(folder, name)): with open(os.path.join(folder, name), "r", encoding="utf-8") as f: return json.load(f)["elements"] if OFFLINE: log(" overpass: not cached and --offline is set") return None g = OVERPASS_PAD padded = (math.floor(bbox[0] / g) * g, math.floor(bbox[1] / g) * g, math.ceil(bbox[2] / g) * g, math.ceil(bbox[3] / g) * g) query, filters_key = overpass_query(place, padded) name = "%s-%s.json" % (place["id"], hashlib.sha1(query.encode("utf-8")).hexdigest()[:12]) path = os.path.join(folder, name) body = urllib.parse.urlencode({"data": query}).encode("utf-8") delay = 10 for attempt in range(4): url = OVERPASS_URLS[0] if attempt < 3 else OVERPASS_URLS[1] try: blob = http_get(url, data=body, timeout=240) doc = json.loads(blob.decode("utf-8")) if "elements" not in doc: raise ValueError("no elements in the reply") if "remark" in doc and "error" in str(doc["remark"]).lower(): raise ValueError("overpass remark: %s" % doc["remark"]) with open(path + ".part", "wb") as f: f.write(blob) os.replace(path + ".part", path) remember(name, padded, filters_key) time.sleep(3) # be polite before the next place return doc["elements"] except (urllib.error.URLError, OSError, ValueError) as e: log(" overpass attempt %d failed: %s" % (attempt + 1, e)) if attempt < 3: time.sleep(delay) delay *= 3 return None # ---------------------------------------------------------------- projection def merc(lat, lon, z): """Global Web Mercator pixel coordinates at zoom z (256-px tiles).""" scale = 256.0 * 2 ** z x = (lon + 180.0) / 360.0 * scale s = math.sin(math.radians(lat)) y = (0.5 - math.log((1 + s) / (1 - s)) / (4 * math.pi)) * scale return x, y def unmerc(x, y, z): scale = 256.0 * 2 ** z lon = x / scale * 360.0 - 180.0 lat = math.degrees(math.atan(math.sinh(math.pi * (1 - 2 * y / scale)))) return lat, lon class Box: """The place's square in Mercator pixels at the zoom that gives at least SUB samples per cell.""" def __init__(self, place): lat, lon, span_m = place["lat"], place["lon"], place["span_km"] * 1000.0 m_per_px0 = 156543.03392 * math.cos(math.radians(lat)) z = 0 while span_m / (m_per_px0 / 2 ** z) < HI and z < 14: z += 1 self.z = z self.m_per_px = m_per_px0 / 2 ** z cx, cy = merc(lat, lon, z) half = span_m / self.m_per_px / 2 self.x0, self.y0, self.x1, self.y1 = cx - half, cy - half, cx + half, cy + half self.sample_m = span_m / HI self.cell_m = span_m / N north, west = unmerc(self.x0, self.y0, z) south, east = unmerc(self.x1, self.y1, z) self.bbox = (south, west, north, east) def to_hi(self, lat, lon): """Position in hi-res sample units (0..HI across the box).""" x, y = merc(lat, lon, self.z) return (x - self.x0) / (self.x1 - self.x0) * HI, (y - self.y0) / (self.y1 - self.y0) * HI def elevation_grid(box): """HI x HI elevations (bilinear from the tile mosaic).""" tx0, ty0 = int(math.floor(box.x0 / 256)), int(math.floor(box.y0 / 256)) tx1, ty1 = int(math.floor(box.x1 / 256)), int(math.floor(box.y1 / 256)) mosaic = np.zeros(((ty1 - ty0 + 1) * 256, (tx1 - tx0 + 1) * 256)) for ty in range(ty0, ty1 + 1): for tx in range(tx0, tx1 + 1): mosaic[(ty - ty0) * 256:(ty - ty0 + 1) * 256, (tx - tx0) * 256:(tx - tx0 + 1) * 256] = fetch_tile(box.z, tx, ty) step = (box.x1 - box.x0) / HI xs = box.x0 - tx0 * 256 + (np.arange(HI) + 0.5) * step - 0.5 ys = box.y0 - ty0 * 256 + (np.arange(HI) + 0.5) * step - 0.5 xs = np.clip(xs, 0, mosaic.shape[1] - 1.001) ys = np.clip(ys, 0, mosaic.shape[0] - 1.001) xi, yi = np.floor(xs).astype(int), np.floor(ys).astype(int) fx, fy = xs - xi, ys - yi a = mosaic[np.ix_(yi, xi)] b = mosaic[np.ix_(yi, xi + 1)] c = mosaic[np.ix_(yi + 1, xi)] d = mosaic[np.ix_(yi + 1, xi + 1)] top = a * (1 - fx)[None, :] + b * fx[None, :] bot = c * (1 - fx)[None, :] + d * fx[None, :] return top * (1 - fy)[:, None] + bot * fy[:, None] # ---------------------------------------------------------------- OSM geometry def join_rings(ways): """Join way geometries (lists of (x, y)) end to end into rings.""" rings, pool = [], [list(w) for w in ways if len(w) >= 2] while pool: ring = pool.pop() grown = True while grown and ring[0] != ring[-1]: grown = False for i, w in enumerate(pool): if w[0] == ring[-1]: ring += w[1:] elif w[-1] == ring[-1]: ring += w[-2::-1] elif w[-1] == ring[0]: ring = w[:-1] + ring elif w[0] == ring[0]: ring = w[:0:-1] + ring else: continue pool.pop(i) grown = True break if len(ring) >= 3: rings.append(ring) return rings def osm_layers(elements, place, box): """Returns (water mask HI x HI from polygons, coastline polylines, line-feature polylines), all in hi-res sample units.""" def geom(g): # keep full float precision for drawing; round only for joining return [box.to_hi(p["lat"], p["lon"]) for p in g if p is not None] def is_water(tags): return tags.get("natural") == "water" or tags.get("waterway") in ("riverbank", "dock") line_names = set() thin_names = set(place.get("thin", [])) thin = Image.new("L", (HI, HI), 0) thin_draw = ImageDraw.Draw(thin) mask = Image.new("L", (HI, HI), 0) draw = ImageDraw.Draw(mask) coast, lines = [], [] for el in elements: tags = el.get("tags", {}) if el["type"] == "way" and "geometry" in el: pts = geom(el["geometry"]) if tags.get("natural") == "coastline": coast.append(pts) elif is_water(tags): if len(pts) >= 3: draw.polygon(pts, fill=1) if tags.get("name") in thin_names: thin_draw.polygon(pts, fill=1) elif tags.get("waterway") in ("river", "canal", "stream"): lines.append(pts) line_names.add(tags.get("name", "?")) elif el["type"] == "relation" and is_water(tags): outers = [geom(m["geometry"]) for m in el.get("members", []) if m.get("type") == "way" and "geometry" in m and m.get("role") != "inner"] inners = [geom(m["geometry"]) for m in el.get("members", []) if m.get("type") == "way" and "geometry" in m and m.get("role") == "inner"] tmp = Image.new("L", (HI, HI), 0) td = ImageDraw.Draw(tmp) for ring in join_rings(outers): td.polygon(ring, fill=1) for ring in join_rings(inners): td.polygon(ring, fill=0) mask = Image.fromarray(np.maximum(np.asarray(mask), np.asarray(tmp))) draw = ImageDraw.Draw(mask) if tags.get("name") in thin_names: thin = Image.fromarray(np.maximum(np.asarray(thin), np.asarray(tmp))) thin_draw = ImageDraw.Draw(thin) return np.asarray(mask).astype(bool), np.asarray(thin).astype(bool), coast, lines, sorted(line_names) def coast_sea(coast, elev_sea): """ Sea from the OSM coastline. The coastline ways are drawn as barriers, which cuts the box into regions (4-connected, so an 8-connected line holds). Coastline ways run with the land on their left and the water on their right, so every segment votes: the region just to its right is sea, the region just to its left is land. A region is what most of its votes say (a stray vote where two coastlines nearly touch is outvoted); a region nobody votes on, or a tie, goes by its elevation (sea if most of it is at or below sea level). Barrier samples are land. Returns (sea mask, number of samples decided by elevation). """ barrier = Image.new("L", (HI, HI), 0) d = ImageDraw.Draw(barrier) for pts in coast: if len(pts) >= 2: d.line(pts, fill=1, width=1) barrier = np.asarray(barrier).astype(bool) region = np.zeros((HI, HI), dtype=np.int32) open_ = (~barrier).tolist() reg = region.tolist() count = 0 for y0 in range(HI): for x0 in range(HI): if not open_[y0][x0] or reg[y0][x0]: continue count += 1 reg[y0][x0] = count queue = deque([(x0, y0)]) while queue: x, y = queue.popleft() for ax, ay in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)): if 0 <= ax < HI and 0 <= ay < HI and open_[ay][ax] and not reg[ay][ax]: reg[ay][ax] = count queue.append((ax, ay)) region = np.array(reg, dtype=np.int32) votes = np.zeros((count + 1, 2), dtype=np.int64) # [region] = (sea votes, land votes) for pts in coast: for (ax, ay), (bx, by) in zip(pts, pts[1:]): dx, dy = bx - ax, by - ay length = math.hypot(dx, dy) if length < 1e-9: continue # image y grows downward: the right-hand normal of (dx, dy) is (-dy, dx) nx, ny = -dy / length, dx / length steps = max(1, int(length / 1.5)) for k in range(steps + 1): px, py = ax + dx * k / steps, ay + dy * k / steps for side, which in ((1.6, 0), (-1.6, 1)): sx, sy = int(math.floor(px + nx * side)), int(math.floor(py + ny * side)) if 0 <= sx < HI and 0 <= sy < HI and reg[sy][sx]: votes[reg[sy][sx], which] += 1 size = np.bincount(region.ravel(), minlength=count + 1) low = np.bincount(region.ravel(), weights=elev_sea.ravel().astype(np.float64), minlength=count + 1) by_elev = low * 2 >= size is_sea = np.where(votes[:, 0] != votes[:, 1], votes[:, 0] > votes[:, 1], by_elev) is_sea[0] = False undecided = int(size[1:][votes[1:, 0] == votes[1:, 1]].sum()) return is_sea[region], undecided # ---------------------------------------------------------------- grid helpers def components(mask, diagonal=False): """Label connected cells of a small boolean grid. Returns (labels, sizes) with label 0 = not in the mask.""" h, w = mask.shape labels = np.zeros((h, w), dtype=np.int32) sizes = [0] steps = [(1, 0), (-1, 0), (0, 1), (0, -1)] + ([(1, 1), (1, -1), (-1, 1), (-1, -1)] if diagonal else []) for y in range(h): for x in range(w): if not mask[y, x] or labels[y, x]: continue lab = len(sizes) labels[y, x] = lab stack, count = [(x, y)], 0 while stack: cx, cy = stack.pop() count += 1 for dx, dy in steps: ax, ay = cx + dx, cy + dy if 0 <= ax < w and 0 <= ay < h and mask[ay, ax] and not labels[ay, ax]: labels[ay, ax] = lab stack.append((ax, ay)) sizes.append(count) return labels, sizes def neighbors8(mask): """Number of set cells in each 3x3 window (the cell itself included); the outside counts as unset.""" p = np.pad(mask.astype(np.int32), 1) return sum(p[1 + dy:1 + dy + mask.shape[0], 1 + dx:1 + dx + mask.shape[1]] for dy in (-1, 0, 1) for dx in (-1, 0, 1)) def box_blur(a, size): """Mean over a size x size window (edges: the window is clipped), by summed-area table.""" r0, r1 = size // 2, size - size // 2 c = np.pad(a, ((1, 0), (1, 0))).cumsum(axis=0).cumsum(axis=1) h, w = a.shape y0 = np.clip(np.arange(h) - r0, 0, h); y1 = np.clip(np.arange(h) + r1, 0, h) x0 = np.clip(np.arange(w) - r0, 0, w); x1 = np.clip(np.arange(w) + r1, 0, w) total = c[np.ix_(y1, x1)] - c[np.ix_(y0, x1)] - c[np.ix_(y1, x0)] + c[np.ix_(y0, x0)] return total / ((y1 - y0)[:, None] * (x1 - x0)[None, :]) def block_mean(a): return a.reshape(N, SUB, N, SUB).mean(axis=(1, 3)) def draw_cell_line(grid, pts): """Mark every cell a polyline (hi-res sample units) passes through.""" for (ax, ay), (bx, by) in zip(pts, pts[1:]): steps = max(1, int(math.hypot(bx - ax, by - ay) / (SUB / 4.0))) for k in range(steps + 1): x, y = ax + (bx - ax) * k / steps, ay + (by - ay) * k / steps cx, cy = int(math.floor(x / SUB)), int(math.floor(y / SUB)) if 0 <= cx < N and 0 <= cy < N: grid[cy, cx] = True # ---------------------------------------------------------------- one place def make_place(place): box = Box(place) log("%s: %s, %.0f km box, zoom %d, cell %.0f m" % (place["id"], place["name"], place["span_km"], box.z, box.cell_m)) elev = elevation_grid(box) elements = fetch_overpass(place, box.bbox) source = "overpass" if elements is not None else "elevation-only" if elements is None: log(" OVERPASS FAILED for %s: falling back to elevation only (no lakes, no rivers above sea level)" % place["id"]) sea_mode = place.get("sea", "coast") elev_sea = elev <= place.get("sea_level", 0.0) osm_water = np.zeros((HI, HI), dtype=bool) thin_water = np.zeros((HI, HI), dtype=bool) coast, lines, line_names = [], [], [] if elements is not None: osm_water, thin_water, coast, lines, line_names = osm_layers(elements, place, box) if sea_mode == "none": sea = np.zeros((HI, HI), dtype=bool) elif sea_mode == "coast" and coast: sea, unknown = coast_sea(coast, elev_sea) log(" sea from the OSM coastline (%d ways; %.1f%% of the samples in regions decided by elevation)" % (len(coast), unknown * 100.0 / HI / HI)) else: if sea_mode == "coast": log(" no coastline in the box: sea from the elevation instead") sea_mode = "elev" sea = elev_sea water_hi = sea | osm_water # ---- cells: water frac = block_mean(water_hi.astype(np.float64)) water = frac >= place.get("water_frac", 0.5) river = np.zeros((N, N), dtype=bool) for pts in lines: draw_cell_line(river, pts) if lines: log(" line features: %s (%d cells)" % (", ".join(line_names), int(river.sum()))) water |= river if place.get("thin"): thin_cells = block_mean(thin_water.astype(np.float64)) >= place.get("thin_frac", 0.2) log(" thin water: %s (%d cells)" % (", ".join(place["thin"]), int((thin_cells & ~water).sum()))) water |= thin_cells # islands smaller than min_land go under; water bodies smaller than min_water dry up (line features count with what they touch) labels, sizes = components(~water) for lab in range(1, len(sizes)): if sizes[lab] < place.get("min_land", 2): water[labels == lab] = True labels, sizes = components(water, diagonal=True) for lab in range(1, len(sizes)): if sizes[lab] < place.get("min_water", 3): water[labels == lab] = False land = ~water # ---- cells: hills and rock, from the land samples land_hi = ~water_hi e = np.where(land_hi, np.maximum(elev, 0.0), 0.0) # slope of the terrain, not of the buildings: the elevation is a surface model in many cities, so it is smoothed over about # one cell first (water samples take the mean land height around them, so a shore is not a cliff) smooth_w = box_blur(land_hi.astype(np.float64), SUB) smooth_e = box_blur(e, SUB) / np.maximum(smooth_w, 1e-9) gy, gx = np.gradient(np.where(smooth_w > 0, smooth_e, 0.0), box.sample_m) slope_hi = np.where(land_hi & (smooth_w > 0.5), np.hypot(gx, gy), 0.0) weight = np.maximum(block_mean(land_hi.astype(np.float64)), 1e-9) cell_elev = block_mean(e) / weight cell_slope = block_mean(slope_hi) / weight # height above the local base: the lowest land cell within base_radius cells r = place.get("base_radius", 6) padded = np.pad(np.where(land, cell_elev, np.inf), r, constant_values=np.inf) base = np.full((N, N), np.inf) for dy in range(-r, r + 1): for dx in range(-r, r + 1): if dx * dx + dy * dy <= r * r: base = np.minimum(base, padded[r + dy:r + dy + N, r + dx:r + dx + N]) prominence = np.where(land, cell_elev - np.where(np.isfinite(base), base, cell_elev), 0.0) score = np.where(land, cell_slope / 0.10 + prominence / 60.0, -1.0) n_land = int(land.sum()) want = int(round(place.get("hill_frac", 0.14) * n_land)) floor = place.get("hill_floor", 0.5) order = np.sort(score[land])[::-1] cut = max(floor, order[want - 1] if 0 < want <= len(order) else floor) hill = land & (score >= cut) # coherent patches: fill holes and shave whiskers (two majority passes), then drop patches under min_hill cells for _ in range(2): cnt = neighbors8(hill) hill = land & ((cnt >= 5) | (hill & (cnt >= 4))) labels, sizes = components(hill) for lab in range(1, len(sizes)): if sizes[lab] < place.get("min_hill", 4): hill[labels == lab] = False # rock: the steepest hill cells only, never more than rock_max of the land rock = hill & (cell_slope >= place.get("rock_slope", 0.30)) max_rock = int(place.get("rock_max", 0.02) * n_land) if rock.sum() > max_rock: keep = np.sort(cell_slope[rock])[::-1][max_rock - 1] if max_rock > 0 else np.inf rock &= cell_slope >= keep # a rock cell cannot be built on or crossed: never let rock cut land off grid = np.full((N, N), GRASS, dtype=np.int8) grid[water] = WATER grid[hill] = HILL before = max(components(grid != WATER)[1]) for y, x in sorted(zip(*np.nonzero(rock)), key=lambda p: -cell_slope[p]): grid[y, x] = ROCK if max(components((grid == GRASS) | (grid == HILL))[1]) < before - int((grid == ROCK).sum()): grid[y, x] = HILL stats = check(place, grid) stats.update(source=source, sea=sea_mode, zoom=box.z, cell_m=round(box.cell_m), bbox=[round(v, 5) for v in box.bbox]) write_place(place, grid) preview(grid).save(os.path.join(PREVIEW_DIR, place["id"] + ".png")) if DEBUG: debug_images(place, elev, sea, osm_water, coast) return grid, stats def check(place, grid): total = N * N buildable = (grid == GRASS) | (grid == HILL) n_build, n_water, n_hill, n_rock = int(buildable.sum()), int((grid == WATER).sum()), int((grid == HILL).sum()), int((grid == ROCK).sum()) sizes = components(buildable)[1] largest = max(sizes) if len(sizes) > 1 else 0 stats = dict(id=place["id"], name=place["name"], land_pct=round(n_build * 100.0 / total, 1), water_pct=round(n_water * 100.0 / total, 1), hill_pct=round(n_hill * 100.0 / max(1, n_build), 1), rock_pct=round(n_rock * 100.0 / max(1, n_build + n_rock), 1), largest_region_pct=round(largest * 100.0 / max(1, n_build), 1), regions=len(sizes) - 1) problems = [] if stats["land_pct"] < 45: problems.append("buildable land under 45%") if stats["largest_region_pct"] < 60: problems.append("largest land region under 60% of the land") if stats["rock_pct"] > 3: problems.append("rock over 3%") stats["problems"] = problems log(" land %.1f%% water %.1f%% hill %.1f%% of land rock %.1f%% largest region %.1f%% (%d regions)%s" % ( stats["land_pct"], stats["water_pct"], stats["hill_pct"], stats["rock_pct"], stats["largest_region_pct"], stats["regions"], " !! " + "; ".join(problems) if problems else "")) return stats def write_place(place, grid): os.makedirs(OUT_DIR, exist_ok=True) rows = ["".join(GLYPH[int(v)] for v in row) for row in grid] # exactly three header keys (name, credit, map): generated, never edited by hand (box and parameters: PLACES above) text = "name: %s\ncredit: %s\nmap:\n%s\n" % (place["name"], CREDIT, "\n".join(rows)) with open(os.path.join(OUT_DIR, place["id"] + ".txt"), "w", encoding="utf-8", newline="\n") as f: f.write(text) def preview(grid, px=8): img = Image.new("RGB", (N * px, N * px)) arr = np.zeros((N, N, 3), dtype=np.uint8) for t, c in COLOR.items(): arr[grid == t] = c return Image.fromarray(arr).resize((N * px, N * px), Image.NEAREST) def font(size): try: return ImageFont.load_default(size=size) except TypeError: return ImageFont.load_default() def contact_sheet(ids): """All previews in the listed order, 5 per row, each under its name.""" cols, tile, pad, label = 5, 384, 12, 26 items = [(p, os.path.join(PREVIEW_DIR, p["id"] + ".png")) for p in PLACES if p["id"] in ids and os.path.exists(os.path.join(PREVIEW_DIR, p["id"] + ".png"))] rows = (len(items) + cols - 1) // cols sheet = Image.new("RGB", (pad + cols * (tile + pad), pad + rows * (tile + label + pad)), (250, 250, 248)) d = ImageDraw.Draw(sheet) f = font(18) for i, (p, path) in enumerate(items): x, y = pad + (i % cols) * (tile + pad), pad + (i // cols) * (tile + label + pad) sheet.paste(Image.open(path).resize((tile, tile), Image.NEAREST), (x, y + label)) d.text((x + 2, y + 2), "%s (%s)" % (p["name"], p["id"]), fill=(40, 40, 40), font=f) path = os.path.join(PREVIEW_DIR, "contact-sheet.png") sheet.save(path) return path def debug_images(place, elev, sea, osm_water, coast): out = os.path.join(CACHE_DIR, "debug") os.makedirs(out, exist_ok=True) shade = np.clip(elev, 0, None) shade = (255 * (shade / max(1.0, float(np.percentile(shade, 99.5)))) ** 0.5).clip(0, 255).astype(np.uint8) rgb = np.stack([shade, shade, shade], axis=2) rgb[sea] = (150, 190, 235) rgb[osm_water & ~sea] = (60, 110, 220) img = Image.fromarray(rgb) d = ImageDraw.Draw(img) for pts in coast: if len(pts) >= 2: d.line(pts, fill=(230, 60, 40), width=1) img.save(os.path.join(out, place["id"] + "-hires.png")) def main(argv): global OFFLINE, DEBUG args = [a for a in argv if not a.startswith("--")] OFFLINE = "--offline" in argv DEBUG = "--debug" in argv known = {p["id"] for p in PLACES} for a in args: if a not in known: sys.exit("unknown place '%s' (have: %s)" % (a, ", ".join(sorted(known)))) os.makedirs(PREVIEW_DIR, exist_ok=True) summary_path = os.path.join(PREVIEW_DIR, "summary.json") summary = {} if os.path.exists(summary_path): with open(summary_path, "r", encoding="utf-8") as f: summary = {s["id"]: s for s in json.load(f)} for place in PLACES: if args and place["id"] not in args: continue _, stats = make_place(place) summary[place["id"]] = stats ordered = [summary[p["id"]] for p in PLACES if p["id"] in summary] with open(summary_path, "w", encoding="utf-8", newline="\n") as f: json.dump(ordered, f, indent=1) f.write("\n") sheet = contact_sheet({s["id"] for s in ordered}) log("contact sheet: %s" % sheet) bad = [s["id"] for s in ordered if s["problems"]] fallback = [s["id"] for s in ordered if s["source"] != "overpass"] if fallback: log("ELEVATION-ONLY (Overpass failed): %s" % ", ".join(fallback)) if bad: log("PROBLEMS: %s" % ", ".join(bad)) return 1 if bad else 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))