Skip to content

armgit / Aleph

Public Imported from GitHub
0

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

Streamed satellite merge (less memory)

Belluxx6407ee87915f
diff --git a/src/capture.py b/src/capture.py
index a327831..9f48cae 100644
--- a/src/capture.py
+++ b/src/capture.py
@@ -2,8 +2,10 @@
import json
import math
+import struct
import tempfile
import time
+import zlib
from datetime import datetime, timezone
from io import BytesIO
from pathlib import Path
@@ -339,6 +341,42 @@ def capture_osm(stage, area, folder, client, progress):
raise failure
+def merge_satellite(stage, folder):
+ """Write RGBA PNG scanlines with at most one tile row decoded at a time."""
+ mosaic = stage["grid"]
+ width, height = mosaic["width"], mosaic["height"]
+ bottom = mosaic["top"] + height
+ with atomic_path(folder / "satellite.png") as temporary, temporary.open("wb") as output:
+ def chunk(kind, data):
+ output.write(struct.pack(">I", len(data)))
+ output.write(kind)
+ output.write(data)
+ output.write(struct.pack(">I", zlib.crc32(data, zlib.crc32(kind))))
+
+ output.write(b"\x89PNG\r\n\x1a\n")
+ chunk(b"IHDR", struct.pack(">2I5B", width, height, 8, 6, 0, 0, 0))
+ compressor = zlib.compressobj()
+ with Image.new("RGBA", (width, min(256, height))) as strip:
+ for row in range(mosaic["rows"]):
+ tile_top = (mosaic["y0"] + row) * 256
+ top = max(mosaic["top"], tile_top)
+ strip_height = min(bottom, tile_top + 256) - top
+ strip.paste((0, 0, 0, 0), (0, 0, width, strip.height))
+ # Saved patches are a row-major prefix, including transparent gaps.
+ first = row * mosaic["columns"]
+ for photo in stage["results"][first:first + mosaic["columns"]]:
+ with open_image((folder / photo["filename"]).read_bytes(), (256, 256)) as patch:
+ strip.paste(patch, (photo["x"] * 256 - mosaic["left"], tile_top - top))
+ for y in range(strip_height):
+ with strip.crop((0, y, width, y + 1)) as scanline:
+ # Filter 0 keeps encoding simple and requires no previous row.
+ data = compressor.compress(b"\0" + scanline.tobytes())
+ if data:
+ chunk(b"IDAT", data)
+ chunk(b"IDAT", compressor.flush())
+ chunk(b"IEND", b"")
+
+
def export(run, folder, progress, *, rebuild=True):
"""Also works offline, on partial runs, or after a failed final export."""
# Commit capture progress before merging, which can fail or be interrupted.
@@ -353,16 +391,7 @@ def export(run, folder, progress, *, rebuild=True):
collection(feature("Polygon", [tile_ring(p)], p) for p in stage["results"]),
)
progress("Merging satellite image")
- mosaic = stage["grid"]
- with Image.new("RGBA", (mosaic["width"], mosaic["height"])) as image:
- for photo in stage["results"]:
- with open_image((folder / photo["filename"]).read_bytes(), (256, 256)) as patch:
- image.paste(
- patch,
- (photo["x"] * 256 - mosaic["left"], photo["y"] * 256 - mosaic["top"]),
- )
- with atomic_path(folder / "satellite.png") as temporary:
- image.save(temporary, format="PNG")
+ merge_satellite(stage, folder)
elif stage["mode"] == "osm":
terrain_results = [item for item in stage["results"] if "x" in item]
count = stage["grid"]["rows"] * stage["grid"]["columns"]
diff --git a/tests/test_capture.py b/tests/test_capture.py
index 1ddc29f..395c598 100644
--- a/tests/test_capture.py
+++ b/tests/test_capture.py
@@ -121,9 +121,15 @@ class CaptureRecoveryTests(unittest.TestCase):
dict(include=["satellite"], satellite_zoom=2), self.progress)
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0)]
self.client.get.side_effect = [image_bytes((256, 256), color) for color in colors]
- with patch.object(Image.Image, "save", side_effect=OSError("export disk full")):
+ previous = image_bytes((1, 1), "black")
+ (self.folder / "satellite.png").write_bytes(previous)
+ compressor = Mock()
+ compressor.compress.side_effect = OSError("export disk full")
+ with patch("src.capture.zlib.compressobj", return_value=compressor):
with self.assertRaisesRegex(OSError, "export disk full"):
capture.download(run, self.folder, self.client, self.progress)
+ self.assertEqual((self.folder / "satellite.png").read_bytes(), previous)
+ self.assertFalse(list(self.folder.glob(".satellite.png-*")))
saved = capture.load(self.folder)
self.assertEqual(saved["state"], "failed")
self.assertFalse(saved["exports_saved"])
@@ -139,6 +145,38 @@ class CaptureRecoveryTests(unittest.TestCase):
self.assertEqual([mosaic.getpixel(point) for point in ((0, 0), (255, 0), (0, 255), (255, 255))],
[(*color, 255) for color in colors])
+ def test_satellite_strips_match_mosaic_for_empty_partial_and_complete_captures(self):
+ north, west = geo.coordinate(384, 384, 3)
+ south, east = geo.coordinate(896, 984, 3)
+ run = capture.plan(self.client, [south, west, north, east],
+ dict(include=["satellite"], satellite_zoom=3), self.progress)
+ stage = run["stages"][0]
+ patches = []
+ for i, tile in enumerate(geo.tiles(stage["grid"])):
+ filename = f"patch-{i}.png"
+ # Vary both coordinates and alpha to expose cropping and paste errors.
+ pixels = bytes(v for y in range(256) for x in range(256)
+ for v in (x, y, i * 20, (x + y) % 256))
+ with Image.frombytes("RGBA", (256, 256), pixels) as image:
+ image.save(self.folder / filename)
+ patches.append(dict(tile, filename=filename))
+ for count in (0, 4, 9):
+ with self.subTest(saved_tiles=count), Image.new("RGBA", (512, 600)) as expected:
+ stage["results"] = patches[:count]
+ for photo in stage["results"]:
+ with Image.open(self.folder / photo["filename"]) as image:
+ expected.paste(image, (photo["x"] * 256 - 384, photo["y"] * 256 - 384))
+ with patch.object(Image, "new", wraps=Image.new) as allocate:
+ capture.export(run, self.folder, self.progress)
+ self.assertTrue(allocate.called)
+ self.assertTrue(all(call.args[1][1] <= 256 for call in allocate.call_args_list))
+ with Image.open(self.folder / "satellite.png") as actual:
+ actual.verify() # Includes PNG chunk CRCs.
+ with Image.open(self.folder / "satellite.png") as actual:
+ self.assertEqual(actual.size, expected.size)
+ self.assertEqual(actual.mode, "RGBA")
+ self.assertEqual(actual.tobytes(), expected.tobytes())
+
def test_satellite_404_leaves_transparent_tile_without_retrying_on_resume(self):
north, west = geo.coordinate(384, 384, 2)
south, east = geo.coordinate(640, 640, 2)