Skip to content

armgit / Aleph

Public Imported from GitHub
0

Give satellite imagery, terrain data, buildings, streetview photos to your LLM

Group tiles

Belluxx16e8dae9462c
diff --git a/docs/cli.md b/docs/cli.md
index 6e67d4f..b549917 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -89,7 +89,7 @@ Add `mesh` for Google Earth's textured 3D model of the area, saved as `mesh.glb`
alephgeo capture create --bbox 41.8895 12.4910 41.8910 12.4940 --sources mesh satellite -o captures
```
-`--mesh-level 1–22` sets the detail (default: `21`). Each level halves the texel size; `22` is the most detailed (about 4 cm per texel) and needs about four times as many downloads as `21`. Coordinates in `mesh.glb` are meters from the rectangle's center: x east, y up, z south.
+`--mesh-level 1–22` sets the detail (default: `21`). Each level halves the texel size; `22` is the most detailed (about 4 cm per texel) and needs about four times as many downloads as `21`. Coordinates in `mesh.glb` are meters from the rectangle's center: x east, y up, z south, with the lowest point at y = 0. The file is split into objects of up to 64 tiles, and each tile keeps its original texture.
Resume a capture or start a saved plan using its printed run folder:
diff --git a/src/capture.py b/src/capture.py
index 88c83e5..ea8366b 100644
--- a/src/capture.py
+++ b/src/capture.py
@@ -37,8 +37,8 @@ TERRAIN_NOTES = {
"quality": "Terrain resolution, dates, accuracy and vertical reference vary by source. Higher zoom does not guarantee more detail.",
}
MESH_NOTES = {
- "mesh": "Google Earth 3D photogrammetry as glTF binary with Google's original JPEG textures and baked-in lighting. Coordinates are meters from the rectangle's center at sea level: x east, y up, z south. Heights are approximate.",
- "selection": "Nodes are saved as downloaded in mesh/nodes/. Finer nodes replace their parent's octants; triangles are kept when their center is inside the rectangle. Levels stop where Google's data ends; level 22 is the most detailed.",
+ "mesh": "Google Earth 3D photogrammetry as glTF binary with Google's original JPEG textures and baked-in lighting. Coordinates are meters from the rectangle's center: x east, y up, z south. y = 0 is the lowest point; asset extras give its approximate height above sea level as base.",
+ "selection": "Nodes are saved as downloaded in mesh/nodes/. Finer nodes replace their parent's octants; triangles are kept when their center is inside the rectangle. Each glTF mesh holds up to 64 tiles from one octree block, with one material per original texture. Levels stop where Google's data ends; level 22 is the most detailed.",
}
README = b"""Aleph capture
@@ -54,7 +54,7 @@ satellite.png contains the same full-resolution pixels for easy viewing.
Transparent pixels mark missing imagery or unfinished downloads.
terrain.tif holds meters in EPSG:3857 (Int16 up to zoom 12, else Float32).
Keep terrain/tiles/ for resume and offline export.
-mesh.glb is Google Earth's textured 3D mesh in meters around the center.
+mesh.glb is Google Earth's textured 3D mesh in meters, resting on y = 0.
Keep mesh/nodes/ for resume and offline export.
Filenames in the manifest and GeoJSON are relative to this run folder.
See manifest.json for settings, sources, imagery dates, and coverage notes.
diff --git a/src/mesh.py b/src/mesh.py
index fd9b083..685eacd 100644
--- a/src/mesh.py
+++ b/src/mesh.py
@@ -27,6 +27,7 @@ JPEG = 1
WORKERS = 16
LOWEST, HIGHEST = -1000, 9000 # Heights searched for nodes, in meters from the sphere.
GLB_LIMIT = 2**32
+TILES_PER_MESH = 64 # Unreal's Nanite allows 64 materials per mesh; Godot allows 256 surfaces.
BITS = [bytes((byte >> i) & 1 for i in range(8)) for byte in range(256)]
@@ -317,9 +318,13 @@ def crop(area, axes, radius):
def export(path, folder, stage, area, progress):
- """Join saved nodes into one glTF binary in a local east/north/up frame around the center."""
+ """Join saved nodes into one glTF binary in a local east/north/up frame around the center.
+
+ Each glTF mesh holds up to TILES_PER_MESH textured tiles from one octree block, named after
+ its first tile: viewers import few objects, and Blender's import slows with many per mesh.
+ """
nodes, results = stage["nodes"], stage["results"]
- radius = stage["radius"]
+ radius, block = stage["radius"], max(1, stage["level"] - 4)
lat, lon = (area[0] + area[2]) / 2, (area[1] + area[3]) / 2
axes = frame(lat, lon)
inside = crop(area, axes, radius)
@@ -327,9 +332,7 @@ def export(path, folder, stage, area, progress):
missing = {node[0] for node, result in zip(nodes, results) if result.get("status") == "skipped"}
saved = [(node, result["filename"]) for node, result in zip(nodes, results) if "filename" in result]
gltf = dict(
- asset=dict(version="2.0", generator="Aleph", copyright="© Google",
- extras=dict(latitude=lat, longitude=lon, radius=radius, bounds=list(area),
- frame="meters east (x), up (y), and south (z) of the center at sea level")),
+ asset=dict(version="2.0", generator="Aleph", copyright="© Google"),
scene=0, scenes=[dict(nodes=[])], nodes=[], meshes=[], materials=[], textures=[], images=[],
accessors=[], bufferViews=[], samplers=[dict(magFilter=9729, minFilter=9987, wrapS=33071, wrapT=33071)],
extensionsUsed=["KHR_materials_unlit"],
@@ -348,11 +351,13 @@ def export(path, folder, stage, area, progress):
raise ValueError("The 3D mesh exceeds the 4 GiB glTF limit. Use a smaller area or lower mesh level.")
return len(gltf["bufferViews"]) - 1
+ # Nodes are sorted by path, so each block's nodes are consecutive.
+ key = None
for done, (node, filename) in enumerate(saved, 1):
mask = node[3] & ~sum(1 << k for k in range(8) if f"{node[0]}{k}" in missing)
data = (folder / filename).read_bytes()
for part, (positions, texcoords, indices, jpeg) in enumerate(decode(data, mask, axes, radius, inside)):
- i, k = len(gltf["meshes"]), len(gltf["accessors"])
+ i, k = len(gltf["materials"]), len(gltf["accessors"])
name = node[0] if not part else f"{node[0]}-{part}"
gltf["accessors"] += [
dict(bufferView=view(positions, 34962), componentType=5126, count=len(positions) // 3,
@@ -368,13 +373,36 @@ def export(path, folder, stage, area, progress):
gltf["materials"].append(dict(name=name, doubleSided=True, extensions={"KHR_materials_unlit": {}},
pbrMetallicRoughness=dict(baseColorTexture=dict(index=i),
metallicFactor=0, roughnessFactor=1)))
- gltf["meshes"].append(dict(name=name, primitives=[dict(
- attributes=dict(POSITION=k, TEXCOORD_0=k + 1), indices=k + 2, material=i)]))
- gltf["nodes"].append(dict(name=name, mesh=i))
- gltf["scenes"][0]["nodes"].append(i)
+ if node[0][:block] != key or len(gltf["meshes"][-1]["primitives"]) == TILES_PER_MESH:
+ key = node[0][:block]
+ gltf["meshes"].append(dict(name=name, primitives=[]))
+ gltf["meshes"][-1]["primitives"].append(dict(
+ attributes=dict(POSITION=k, TEXCOORD_0=k + 1), indices=k + 2, material=i))
progress("Building mesh.glb", done, len(saved))
if not gltf["meshes"]:
return
+ gltf["nodes"] = [dict(name=mesh["name"], mesh=i) for i, mesh in enumerate(gltf["meshes"])]
+ gltf["scenes"][0]["nodes"] = list(range(len(gltf["meshes"])))
+
+ # Rest the lowest point on y = 0, so viewers orbit and zoom around the ground, not sea level.
+ positions = [accessor for accessor in gltf["accessors"] if accessor["type"] == "VEC3"]
+ base = min(accessor["min"][1] for accessor in positions)
+ for accessor in positions:
+ span = gltf["bufferViews"][accessor["bufferView"]]
+ spool.seek(span["byteOffset"])
+ values = array("f", spool.read(span["byteLength"]))
+ if sys.byteorder != "little":
+ values.byteswap()
+ values[1::3] = array("f", [y - base for y in values[1::3]])
+ accessor["min"][1], accessor["max"][1] = min(values[1::3]), max(values[1::3])
+ if sys.byteorder != "little":
+ values.byteswap()
+ spool.seek(span["byteOffset"])
+ spool.write(bytes(values))
+ spool.seek(0, 2)
+ gltf["asset"]["extras"] = dict(
+ latitude=lat, longitude=lon, base=base, radius=radius, bounds=list(area),
+ frame="meters east (x), up (y), and south (z) of the center; y = 0 is the lowest point, base meters above sea level")
size = spool.tell()
gltf["buffers"] = [dict(byteLength=size)]
header = json.dumps(gltf, ensure_ascii=False, separators=(",", ":")).encode()
diff --git a/tests/test_mesh.py b/tests/test_mesh.py
index 6163049..2266866 100644
--- a/tests/test_mesh.py
+++ b/tests/test_mesh.py
@@ -7,7 +7,7 @@ import unittest
from array import array
from pathlib import Path
from tempfile import TemporaryDirectory
-from unittest.mock import Mock
+from unittest.mock import Mock, patch
from src import capture, mesh
from src.common import MissingImagery, write_bytes
@@ -81,25 +81,36 @@ class MeshTests(unittest.TestCase):
self.folder = Path(temporary.name)
def test_export_drops_refined_octants_skirts_upper_layers_and_outside_triangles(self):
- vertices = [(0, 0, 1), (2, 0, 0), (0, 2, 0), (2, 2, 0), (4, 0, 0), (4, 2, 0), (0, 4, 0), (60, 60, 0), (-60, -60, 0)]
+ # Five meters up; the export rests the lowest point on y = 0.
+ vertices = [(0, 0, 6), (2, 0, 5), (0, 2, 5), (2, 2, 5), (4, 0, 5), (4, 2, 5), (0, 4, 5), (60, 60, 5), (-60, -60, 5)]
# Triangles: 0 kept, 1 skirt, 2–3 degenerate, 4 octant refined by "01", 5 octant of the
# unavailable "02" (kept, odd so flipped), 6 centered outside, 7 in the fourth layer.
strip = [0, 1, 2, 3, 3, 4, 5, 6, 7, 8]
runs = [6, 1, 1, 0, 0, 0, 0, 0, 1] + [0] * 15 + [1]
write_bytes(self.folder / "0.bin", node(vertices, strip, runs, skirts=bytes([0b10]), jpeg=b"jpeg-0"))
- write_bytes(self.folder / "01.bin", node())
- stage = dict(radius=RADIUS, nodes=[["0", 1, None, 0b110], ["01", 1, None, 0], ["02", 1, None, 0]],
- results=[dict(filename="0.bin"), dict(filename="01.bin"), dict(status="skipped")])
- mesh.export(self.folder / "mesh.glb", self.folder, stage, AREA, Mock())
+ write_bytes(self.folder / "01.bin", node([(6, 6, 5), (8, 6, 5), (6, 8, 5)], [0, 1, 2], [3], jpeg=b"jpeg-01"))
+ write_bytes(self.folder / "03.bin", node([(6, -6, 5), (8, -6, 5), (6, -8, 5)], [0, 1, 2], [3], jpeg=b"jpeg-03"))
+ stage = dict(radius=RADIUS, level=3,
+ nodes=[["0", 1, None, 0b110], ["01", 1, None, 0], ["02", 1, None, 0], ["03", 1, None, 0]],
+ results=[dict(filename="0.bin"), dict(filename="01.bin"), dict(status="skipped"),
+ dict(filename="03.bin")])
+ with patch.object(mesh, "TILES_PER_MESH", 2):
+ mesh.export(self.folder / "mesh.glb", self.folder, stage, AREA, Mock())
gltf, view = read_glb(self.folder / "mesh.glb")
- self.assertEqual([m["name"] for m in gltf["meshes"]], ["0"])
+ self.assertEqual(gltf["asset"]["extras"]["base"], 5)
+ # One block, split at the tile limit; each primitive has its own texture.
+ self.assertEqual([(m["name"], [p["material"] for p in m["primitives"]]) for m in gltf["meshes"]],
+ [("0", [0, 1]), ("03", [2])])
position, uv, indices = (gltf["accessors"][i]["bufferView"] for i in range(3))
self.assertEqual(list(view(indices, "H")), [0, 1, 2, 3, 4, 5])
# Vertices 0, 1, 2, 4, 6, 5 as x east, y up, z south.
self.assertEqual(list(view(position, "f")), [0, 1, 0, 2, 0, 0, 0, 0, -2, 4, 0, 0, 0, 0, -4, 4, 0, -2])
self.assertEqual(list(view(uv, "f"))[6:8], [40.5 / 256, 20.5 / 256])
self.assertEqual(view(gltf["images"][0]["bufferView"], "B").tobytes(), b"jpeg-0")
+ self.assertEqual(list(view(gltf["accessors"][3]["bufferView"], "f")), [6, 0, -6, 8, 0, -6, 6, 0, -8])
+ self.assertEqual([view(gltf["images"][gltf["textures"][i]["source"]]["bufferView"], "B").tobytes()
+ for i in (1, 2)], [b"jpeg-01", b"jpeg-03"])
def test_parallel_download_keeps_node_order_across_interrupt_and_resume(self):
nodes = [[f"0{i:02d}", 1, None, 0] for i in range(20)]