Give satellite imagery, terrain data, buildings, streetview photos to your LLM
Read dashboard terrain from saved tiles
Belluxxb3a2ed4ed3ab
diff --git a/src/assets/app.js b/src/assets/app.jsindex 332f027..708de62 100644--- a/src/assets/app.js+++ b/src/assets/app.js@@ -438,10 +438,11 @@ async function selectCapture(identity, fit = true) {$("layer-satellite").disabled = !run.layers.satellite?.done;["buildings", "roads", "water"].forEach((key) => $("layer-" + key).disabled = !run.osm);$("layer-photos").disabled = !run.layers.streetview?.done;- $("terrain").disabled = !run.terrain;- $("terrain").title = !state.config.terrain ? "Terrain viewing requires Pillow with libtiff support." : !run.terrain ? "No saved terrain is available in this capture." : "Tilt the map and show terrain heights.";- const newTerrain = run.terrain && !map.getSource("terrain");- if (!updating || newTerrain) $("terrain").checked = run.terrain;+ const terrain = Boolean(run.layers.terrain?.done);+ $("terrain").disabled = !terrain;+ $("terrain").title = terrain ? "Tilt the map and show terrain heights." : "No saved terrain is available in this capture.";+ const newTerrain = terrain && !map.getSource("terrain");+ if (!updating || newTerrain) $("terrain").checked = terrain;if (run.layers.satellite && !map.getSource("satellite")) {// HTTP expiry refreshes incomplete regions; completed tiles stay cached.map.addSource("satellite", {type: "raster", tiles: [template("satellite")], tileSize: 256,@@ -463,9 +464,9 @@ async function selectCapture(identity, fit = true) {}setSelection(null);// Load the bounded DEM from above, then focus at its elevation before tilting.- if (fit && run.terrain) fitCapture(false);- if (!updating || newTerrain || fit) applyTerrain(fit && run.terrain);- if (fit && !run.terrain) fitCapture();+ if (fit && terrain) fitCapture(false);+ if (!updating || newTerrain || fit) applyTerrain(fit && terrain);+ if (fit && !terrain) fitCapture();if (run.osm && !map.getSource("osm")) loadOSM(run);if (run.layers.streetview && (!map.getSource("photos")|| state.photoCount < run.layers.streetview.done)) {diff --git a/src/dashboard.py b/src/dashboard.pyindex a68d269..c682a68 100644--- a/src/dashboard.py+++ b/src/dashboard.py@@ -13,8 +13,6 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServerfrom pathlib import Pathfrom urllib.parse import parse_qs, unquote, urlsplit-from PIL import features-from . import capturefrom .common import Client, contained, nowfrom .dashboard_osm import display_osm@@ -100,13 +98,11 @@ class Catalog:layers[mode] = dict(done=len(stage["results"]), total=capture.total(stage))if "grid" in stage:layers[mode]["zoom"] = stage["grid"]["zoom"]- terrain_path = contained(folder, "terrain.tif")return dict(id=identity, bounds=run["bounds"], started_at=run["started_at"],state=run["state"], error=run.get("error"), revision=revision,exports_saved=run.get("exports_saved", False), options=run["options"], layers=layers,osm=contained(folder, "map.osm").is_file(),- terrain=terrain_path.is_file() and features.check("libtiff"),)def list(self):@@ -343,7 +339,7 @@ class Handler(BaseHTTPRequestHandler):raise FileNotFoundError("Unknown action.")if path == "/api/config":return self.json(dict(token=self.server.token, root=str(self.server.catalog.root),- defaults=capture.DEFAULT_OPTIONS, terrain=features.check("libtiff")))+ defaults=capture.DEFAULT_OPTIONS))if path == "/api/captures":return self.json(self.server.catalog.list())if path == "/api/job":diff --git a/src/dashboard_tiles.py b/src/dashboard_tiles.pyindex 7678392..00c53f6 100644--- a/src/dashboard_tiles.py+++ b/src/dashboard_tiles.py@@ -2,9 +2,9 @@import mathimport struct-import sysimport threading-from io import BytesIO+import zlib+from itertools import accumulatefrom PIL import Image@@ -12,42 +12,37 @@ from . import terrainfrom .common import atomic_path, contained-def terrain_tile(path, grid, x, y):- """Decode just four compressed TIFF blocks, not the entire terrain mosaic."""- with path.open("rb") as stream:- source = terrain.TIFF(stream)- columns = grid["columns"] * 2- bx, by = (x - grid["x0"]) * 2, (y - grid["y0"]) * 2- offsets, sizes = source.values(324), source.values(325)- indices = [(by + dy) * columns + bx + dx for dy in range(2) for dx in range(2)]- if any(i < 0 or i >= len(offsets) or not offsets[i] or not sizes[i] for i in indices):- return Image.new("F", (512, 512))- blocks = [source.read(offsets[i], sizes[i]) for i in indices]- tags = {key: value for key, value in source.tags.items()- if key in (258, 259, 262, 274, 277, 284, 317, 322, 323, 339)}+def unpredict(data, width):+ """Undo the TIFF floating point predictor, giving big-endian Float32 rows."""+ rows = bytearray()+ for y in range(0, len(data), width * 4):+ # Each row holds byte differences across four planes, most significant first.+ planes = bytes(v & 255 for v in accumulate(data[y:y + width * 4]))+ row = bytearray(width * 4)+ for plane in range(4):+ row[plane::4] = planes[plane * width:(plane + 1) * width]+ rows += row+ return bytes(rows)- def tag(code, values):- tags[code] = 4, len(values), struct.pack(source.order + "I" * len(values), *values)- tag(256, [512])- tag(257, [512])- tag(324, [0] * 4)- tag(325, [len(block) for block in blocks])- cursor = len(terrain.header(tags, source.order))- locations = []- for block in blocks:- locations.append(cursor)- cursor += len(block)- tag(324, locations)- data = terrain.header(tags, source.order) + b"".join(blocks)+def terrain_tile(path):+ """Decode one saved 512-pixel terrain tile without libtiff."""+ with path.open("rb") as stream:+ source = terrain.TIFF(stream)+ size = source.value(322)+ per_row = 512 // size+ image = Image.new("F", (512, 512))+ for i, (offset, length) in enumerate(zip(source.values(324), source.values(325))):+ data = source.read(offset, length)+ if source.value(259) != 1:+ data = zlib.decompress(data)+ if source.value(317, 1) == 3:+ data, raw_mode = unpredict(data, size), "F;32BF"+ else:+ raw_mode = "F;32F" if source.order == "<" else "F;32BF"+ with Image.frombytes("F", (size, size), data, "raw", raw_mode) as block:+ image.paste(block, (i % per_row * size, i // per_row * size))nodata = float(b"".join(source.values(42113)).rstrip(b"\0"))- with Image.open(BytesIO(data)) as decoded:- # libtiff returns native floats, even when the source TIFF is big-endian.- raw_mode = "F;32F" if sys.byteorder == "little" else "F;32BF"- decoded.tile = [(codec, extent, offset, (raw_mode, *args[1:]))- if codec == "libtiff" else (codec, extent, offset, args)- for codec, extent, offset, args in decoded.tile]- image = decoded.convert("F")# RGB elevation has no NoData representation. Unknown heights use sea level.image.putdata([v if math.isfinite(v) and v != nodata else 0for (v,) in struct.iter_unpack("=f", image.tobytes())])@@ -90,56 +85,49 @@ class Tiles:stage = next((stage for stage in run["stages"] if stage["mode"] == kind), None)if stage is None or z < 0 or z > stage["grid"]["zoom"] or not (0 <= x < 2**z and 0 <= y < 2**z):raise FileNotFoundError("Tile is outside this capture.")- directory = self.cache / folder.name / kind- if kind == "terrain":- stat = contained(folder, "terrain.tif").stat()- directory /= f"{stat.st_mtime_ns}-{stat.st_size}"- grid = stage["grid"]- count = len(stage["results"]) if kind == "satellite" else grid["columns"] * grid["rows"]- version, complete = tile_state(grid, count, z, x, y)+ grid, results = stage["grid"], stage["results"]+ version, complete = tile_state(grid, len(results), z, x, y)# Native imagery is already browser-readable; avoid decoding or re-encoding it.if kind == "satellite" and version and z == grid["zoom"]:- original = contained(folder, stage["results"][version - 1]["filename"])- if original.is_file():- return original, complete- filename = directory / str(z) / str(x) / f"{y}.png"+ return contained(folder, results[version - 1]["filename"]), complete+ filename = self.cache / folder.name / kind / str(z) / str(x) / f"{y}.png"# Published cache files are atomic. Ready tiles never wait for an overview build.if self.versions.get(filename, -1) >= version:return filename, complete# Bound decoding to one tree per layer, without terrain blocking satellite imagery.with self.locks[kind]:if self.versions.get(filename, -1) < version:- with self._image(folder, directory, grid, stage["results"], count, kind, z, x, y) as image:+ with self._image(folder, kind, grid, results, z, x, y) as image:if kind == "terrain":with terrarium(image) as rgb, atomic_path(filename) as temporary:rgb.save(temporary, format="PNG")self.versions[filename] = versionreturn filename, complete- def _image(self, folder, directory, grid, results, count, kind, z, x, y):+ def _image(self, folder, kind, grid, results, z, x, y):+ """Satellite tiles are cached as display PNGs, terrain as Float32 TIFFs."""elevation = kind == "terrain"mode, size = ("F", 512) if elevation else ("RGBA", 256)- suffix = "tif" if elevation else "png"- filename = directory / str(z) / str(x) / f"{y}.{suffix}"- version, _ = tile_state(grid, count, z, x, y)+ filename = self.cache / folder.name / kind / str(z) / str(x) / f"{y}.{'tif' if elevation else 'png'}"+ version, _ = tile_state(grid, len(results), z, x, y)if self.versions.get(filename, -1) >= version:with Image.open(filename) as image:return image.copy()- if not elevation and version and z == grid["zoom"]:- path = contained(folder, results[version - 1]["filename"])- if path.is_file():- with Image.open(path) as patch:- return patch.convert("RGBA")- image = (terrain_tile(contained(folder, "terrain.tif"), grid, x, y)- if elevation and version and z == grid["zoom"] else Image.new(mode, (size, size)))- if version and z < grid["zoom"]:- for dy in range(2):- for dx in range(2):- with (- self._image(folder, directory, grid, results, count, kind, z + 1, x * 2 + dx, y * 2 + dy) as child,- child.resize((size // 2, size // 2), Image.Resampling.BOX) as smaller,- ):- image.paste(smaller, (dx * size // 2, dy * size // 2))+ native = version and z == grid["zoom"]+ if native and not elevation:+ with Image.open(contained(folder, results[version - 1]["filename"])) as patch:+ return patch.convert("RGBA")+ if native:+ image = terrain_tile(contained(folder, results[version - 1]["filename"]))+ else:+ image = Image.new(mode, (size, size))+ children = [(dx, dy) for dy in range(2) for dx in range(2)] if version else []+ for dx, dy in children:+ with (+ self._image(folder, kind, grid, results, z + 1, x * 2 + dx, y * 2 + dy) as child,+ child.resize((size // 2, size // 2), Image.Resampling.BOX) as smaller,+ ):+ image.paste(smaller, (dx * size // 2, dy * size // 2))with atomic_path(filename) as temporary:image.save(temporary, format="TIFF" if elevation else "PNG")self.versions[filename] = version