Skip to content

armgit / Aleph

Public Imported from GitHub
0

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

Initial polygon selection support

Belluxx0c0945623bb4
diff --git a/README.md b/README.md
index ca3d76e..d3ea854 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@
Aleph does two things:
-- Lets you draw a rectangle on the map and downloads **everything** inside it (buildings, elevation, HD satellite imagery, Street View photos, textured 3D models).
+- Lets you draw a rectangle or polygon on the map and downloads **everything** inside it (buildings, elevation, HD satellite imagery, Street View photos, textured 3D models).
- Provides any of the above information for **specific points** on Earth, on the fly, via the CLI.
This was born as a way to let LLMs quickly gather information from the physical world, but you can just use it to have piece of the Earth on your laptop and do whatever you want with it.
@@ -49,7 +49,7 @@ alephgeo --help
alephgeo dashboard -o captures
```
-The dashboard opens in your browser at `http://127.0.0.1:8100`. Draw a rectangle on the map, choose the sources and their settings, check the preview and time estimate, then start the capture. Open any capture to show or hide each layer, or to explore its 3D mesh or terrain relief in 3D. Captures are saved in `captures`.
+The dashboard opens in your browser at `http://127.0.0.1:8100`. Draw a rectangle or polygon on the map, choose the sources and their settings, check the preview and time estimate, then start the capture. Open any capture to show or hide each layer, or to explore its 3D mesh or terrain relief in 3D. Captures are saved in `captures`.
The basemap comes from [OpenFreeMap](https://openfreemap.org) and needs an internet connection.
diff --git a/aleph.py b/aleph.py
index 214c568..1685313 100644
--- a/aleph.py
+++ b/aleph.py
@@ -10,7 +10,7 @@ from pathlib import Path
from src import capture, places, quick
from src.cli import parser
from src.common import CachedClient, Client, Progress, RequestError, now
-from src.geo import bounds
+from src.geo import bounds, corners, vertices
def style(text, code="1"):
@@ -31,7 +31,7 @@ def describe(run):
elif mode == "satellite":
label = "Satellite"
g = stage["grid"]
- scale = capture.resolution(run["bounds"], g["zoom"])
+ scale = capture.resolution(run["polygon"], g["zoom"])
message = f"{estimate['satellite_tiles']:,} tiles, {g['width']:,} × {g['height']:,} px at about {scale:.2f} m/px"
elif mode == "osm":
label = "OSM map"
@@ -202,10 +202,16 @@ def main(argv=None):
action = args.action
if action == "create":
+ if args.polygon:
+ if len(args.polygon) % 2:
+ raise ValueError("--polygon takes LAT LON pairs.")
+ polygon = vertices(zip(args.polygon[::2], args.polygon[1::2]))
+ else:
+ polygon = corners(bounds(args.bbox))
client = Client(args.delay, cache_dir=args.cache_dir, refresh=args.refresh)
options = {key: getattr(args, key) for key in capture.DEFAULT_OPTIONS}
with Progress() as progress:
- run = capture.plan(client, bounds(args.bbox), options, progress)
+ run = capture.plan(client, polygon, options, progress)
else:
folder = args.folder.expanduser().resolve()
run = capture.load(folder)
diff --git a/docs/cli.md b/docs/cli.md
index a9f4e0f..a83910c 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -81,6 +81,14 @@ alephgeo capture create --bbox 41.8895 12.4910 41.8910 12.4940 -o captures
The command shows an estimate and asks before downloading. Add `--yes` to skip the prompt, or `--plan` to save a plan for later.
+To capture any other shape, use `--polygon` with its corners in order as `LAT LON` pairs:
+
+```sh
+alephgeo capture create --polygon 41.8895 12.4910 41.8910 12.4915 41.8905 12.4940 41.8893 12.4932 -o captures
+```
+
+Street View, satellite imagery, OSM data, and the 3D mesh stay inside the polygon; satellite pixels outside it are transparent. Terrain covers its bounding box. Edges must not cross.
+
Use `--sources` to select `streetview`, `satellite`, `osm`, or `terrain`, or a combination. These four are the default.
Add `mesh` for Google Earth's textured 3D model of the area, saved as `mesh.glb`:
@@ -89,7 +97,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, 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.
+`--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 center of the area's bounding box: 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 0c3b58a..8ab60f3 100644
--- a/src/capture.py
+++ b/src/capture.py
@@ -12,10 +12,11 @@ from PIL import Image
from . import mesh, satellite, streetview, terrain
from .common import WORKERS, MissingImagery, contained, fetch, now, number, open_image, positive, write_bytes, write_json
-from .geo import MERCATOR_RADIUS, RADIUS, bounds, collection, coordinate, distance, feature, grid, pixel, tile_ring, tiles
+from .geo import (MERCATOR_RADIUS, RADIUS, collection, coordinate, corners, extent, feature, grid, lines, measure,
+ pixel, tile_ring, tiles, vertices)
SOURCES = ("streetview", "satellite", "osm", "terrain", "mesh")
-FORMAT_VERSION = 5
+FORMAT_VERSION = 6
DEFAULT_OPTIONS = dict(
include=["streetview", "satellite", "osm", "terrain"], step=30, fov=75, depth="roads", streetview_format="jpg",
full_sphere=False, sphere_zoom=3,
@@ -35,15 +36,15 @@ OSM_EXPORT_SECONDS = 10
OSM_NOTES = {
"map": "OSM XML from Geofabrik with original tags and topology; coordinates are WGS84. Contributor names, IDs and changeset IDs are omitted by Geofabrik.",
- "selection": "Selected ways are complete. Selected multipolygons include all available outer boundaries and holes, but members missing from the regional file remain unresolved. Other relations may also be incomplete. Objects may extend outside the rectangle; crossings without inside nodes and enclosing polygons may be absent.",
+ "selection": "Selected ways are complete. Selected multipolygons include all available outer boundaries and holes, but members missing from the regional file remain unresolved. Other relations may also be incomplete. Objects may extend outside the area; crossings without inside nodes and enclosing polygons may be absent.",
}
TERRAIN_NOTES = {
- "terrain": "Heights in meters (Int16 up to zoom 12, Float32 from zoom 13), EPSG:3857, without resampling. Full edge tiles extend beyond the rectangle. Tiles are checkpointed individually; terrain.tif is built after all terrain tiles are saved.",
+ "terrain": "Heights in meters (Int16 up to zoom 12, Float32 from zoom 13), EPSG:3857, without resampling. Every tile meeting the area's bounding box is saved, so terrain extends beyond the area. Tiles are checkpointed individually; terrain.tif is built after all terrain tiles are saved.",
"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: 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.",
+ "mesh": "Google Earth 3D photogrammetry as glTF binary with Google's original JPEG textures and baked-in lighting. Coordinates are meters from the center of the area's bounding box: 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 area. 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,9 +55,9 @@ Open satellite.tif for satellite imagery and map.osm for native map data.
Photo headings are clockwise from north; left/right follow OSM node order.
GeoJSON files record paths, photo locations, or satellite patch footprints.
satellite.tif is a lossless RGBA Cloud Optimized GeoTIFF in EPSG:3857.
-It is north-up, cropped to enclosing pixels, with internal overviews.
+It is north-up, cropped to the pixels enclosing the area, with internal overviews.
satellite.png contains the same full-resolution pixels for easy viewing.
-Transparent pixels mark missing imagery or unfinished downloads.
+Transparent pixels mark missing imagery, unfinished downloads, or pixels outside the area.
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, resting on y = 0.
@@ -105,23 +106,29 @@ def create_folder(run, parent):
return folder
-def plan(client, area, options, progress):
+def tile_grid(mode, polygon, options):
+ """Satellite tiles meeting the polygon; terrain.tif needs every tile of the bounding box."""
+ return grid(polygon if mode == "satellite" else corners(extent(polygon)), options[f"{mode}_zoom"])
+
+
+def plan(client, polygon, options, progress):
+ """Plan a capture of a polygon of (lat, lon) points; a rectangle is its four corners."""
options = settings(options)
stages = []
if "streetview" in options["include"]:
- stages.append(streetview.plan(client, area, options, progress, allow_empty=len(options["include"]) > 1))
+ stages.append(streetview.plan(client, polygon, options, progress, allow_empty=len(options["include"]) > 1))
if "satellite" in options["include"]:
- stages.append(dict(mode="satellite", grid=grid(area, options["satellite_zoom"]), results=[]))
+ stages.append(dict(mode="satellite", grid=tile_grid("satellite", polygon, options), results=[]))
if "osm" in options["include"]:
stages.append(dict(mode="osm", results=[], notes=OSM_NOTES))
if "terrain" in options["include"]:
- stages.append(dict(mode="terrain", grid=grid(area, options["terrain_zoom"]), results=[], notes=TERRAIN_NOTES))
+ stages.append(dict(mode="terrain", grid=tile_grid("terrain", polygon, options), results=[], notes=TERRAIN_NOTES))
if "mesh" in options["include"]:
- stage = mesh.plan(client, area, options["mesh_level"], options["mesh_workers"], progress,
+ stage = mesh.plan(client, polygon, options["mesh_level"], options["mesh_workers"], progress,
allow_empty=len(options["include"]) > 1)
stages.append(dict(stage, notes=MESH_NOTES))
- return dict(format="aleph-python", version=FORMAT_VERSION, bounds=area, options=options,
- started_at=now(), state="planned", stages=stages)
+ return dict(format="aleph-python", version=FORMAT_VERSION, polygon=[list(point) for point in polygon],
+ options=options, started_at=now(), state="planned", stages=stages)
def total(stage):
@@ -131,7 +138,7 @@ def total(stage):
return 1
if stage["mode"] == "mesh":
return len(stage["nodes"])
- return stage["grid"]["rows"] * stage["grid"]["columns"]
+ return stage["grid"]["count"]
def estimate(run):
@@ -173,37 +180,36 @@ def workers(options, kind):
return 1 if options["delay"] or kind not in REQUEST_WORKERS else options[REQUEST_WORKERS[kind]]
-def planning(area, options):
+def planning(polygon, options):
"""Rough seconds that planning Street View and 3D mesh spends on requests, by mode.
- Street View lists panoramas in every zoom-17 tile. 3D mesh lists octree nodes four levels at a
- time, one level after another, from the nodes meeting the area; nodes at level n are about
- 2πR / 2^n wide, and measured counts run about twice that footprint as nodes stack up over terrain
- and buildings. Excludes reading roads and the first download of a regional OSM file.
+ Street View lists panoramas in every zoom-17 tile meeting the polygon. 3D mesh lists octree nodes
+ four levels at a time, one level after another, from the nodes meeting the polygon; nodes at
+ level n are about s = 2πR / 2^n wide. A shape of area A and perimeter P meets about
+ A / s² + P / 2s + 1 of them, and measured counts run about twice that as nodes stack up over
+ terrain and buildings. Excludes reading roads and the first download of a regional OSM file.
"""
result = {}
if "streetview" in options["include"]:
- g = grid(area, 17)
- count = g["rows"] * g["columns"]
+ count = grid(polygon, 17)["count"]
result["streetview"] = count * (REQUEST_SECONDS["coverage_tile"] / workers(options, "coverage_tile")
+ options["delay"])
if "mesh" in options["include"]:
- south, west, north, east = area
- middle = (south + north) / 2
- width, height = distance((middle, west), (middle, east)), distance((south, west), (north, west))
+ surface, perimeter = measure(polygon)
seconds = 0
for level in range(0, options["mesh_level"], 4):
size = 2 * math.pi * RADIUS / 2**level
- count = 1 if level == 0 else 2 * (width / size + 1) * (height / size + 1)
+ count = 1 if level == 0 else 2 * (surface / size**2 + perimeter / (2 * size) + 1)
seconds += math.ceil(count / workers(options, "mesh_bulk")) * REQUEST_SECONDS["mesh_bulk"]
seconds += count * options["delay"]
result["mesh"] = seconds
return {mode: math.ceil(seconds) for mode, seconds in result.items()}
-def resolution(area, zoom, tile_size=256):
+def resolution(polygon, zoom, tile_size=256):
"""Approximate ground meters per pixel at the middle of the area."""
- latitude = math.radians((area[0] + area[2]) / 2)
+ south, _, north, _ = extent(polygon)
+ latitude = math.radians((south + north) / 2)
return 2 * math.pi * MERCATOR_RADIUS * math.cos(latitude) / (tile_size * 2**zoom)
@@ -282,7 +288,7 @@ def capture_satellite(stage, run, folder, client, progress):
def capture_osm(stage, run, folder, client, progress):
- metadata = client.maps.export(run["bounds"], folder / "map.osm", progress)
+ metadata = client.maps.export(run["polygon"], folder / "map.osm", progress)
stage["results"].append(dict(filename="map.osm", captured_at=now(), **metadata))
yield
@@ -335,13 +341,13 @@ def export(run, folder, progress, *, rebuild=True):
elif stage["mode"] == "satellite":
write_json(folder / "satellite/patches.geojson",
collection(feature("Polygon", [tile_ring(p)], p) for p in stage["results"]))
- satellite.merge(stage, folder, progress)
+ satellite.merge(stage, folder, progress, run["polygon"])
elif stage["mode"] == "terrain":
if len(stage["results"]) == total(stage) and (rebuild or not (folder / "terrain.tif").is_file()):
terrain.merge(folder / "terrain.tif", folder, stage["results"], stage["grid"], progress)
elif stage["mode"] == "mesh":
if len(stage["results"]) == total(stage) and (rebuild or not (folder / "mesh.glb").is_file()):
- mesh.export(folder / "mesh.glb", folder, stage, run["bounds"], progress)
+ mesh.export(folder / "mesh.glb", folder, stage, run["polygon"], progress)
run["exports_saved"] = True
write_json(folder / "manifest.json", run)
@@ -351,7 +357,7 @@ def save_preview(run, folder):
if set(run["options"]["include"]) <= {"osm", "terrain"}:
return
write_bytes(folder / "README.txt", README)
- area = run["bounds"]
+ area = extent(run["polygon"])
left, top = pixel((area[2], area[1]), 17)
right, bottom = pixel((area[0], area[3]), 17)
scale = min(850 / (right - left), 510 / (bottom - top))
@@ -363,6 +369,7 @@ def save_preview(run, folder):
def corner(x, y, zoom):
return xy(coordinate(x * 256, y * 256, zoom))
+ outline = "M" + "L".join(xy(p) for p in run["polygon"]) + "Z"
for stage in run["stages"]:
if stage["mode"] == "streetview":
write_json(folder / "streetview/paths.geojson", collection(
@@ -380,14 +387,12 @@ def save_preview(run, folder):
caption = "Planned stops. Dotted red lines show spacing gaps. North is up."
elif stage["mode"] == "satellite":
g = stage["grid"]
- x0, y0, x1, y1, z = g["x0"], g["y0"], g["x0"] + g["columns"], g["y0"] + g["rows"], g["zoom"]
- lines = [f"M{corner(x, y0, z)}L{corner(x, y1, z)}" for x in range(x0, x1 + 1)]
- lines += [f"M{corner(x0, y, z)}L{corner(x1, y, z)}" for y in range(y0, y1 + 1)]
- drawing = [f'<path d="{"".join(lines)}" stroke="#888" stroke-width="1"/>']
- caption = f"Satellite zoom {g['zoom']}, {g['columns']} × {g['rows']} patches. North is up."
+ edges = "".join(f"M{corner(*a, g['zoom'])}L{corner(*b, g['zoom'])}" for a, b in lines(g))
+ drawing = [f'<path d="{edges}" stroke="#888" stroke-width="1"/>']
+ caption = f"Satellite zoom {g['zoom']}, {g['count']} patches. North is up."
else:
continue
- drawing.append(f'<rect x="25" y="45" width="{(right - left) * scale}" height="{(bottom - top) * scale}" stroke="#a32632"/>')
+ drawing.append(f'<path d="{outline}" stroke="#a32632"/>')
svg = (
f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 580" role="img">'
f'<title>{caption}</title><rect width="900" height="580" fill="#faf8f4"/>'
@@ -444,13 +449,13 @@ def load(folder, *, check_files=True):
run = json.loads(contained(folder, "manifest.json").read_text(encoding="utf-8"))
if run.get("format") != "aleph-python" or run.get("version") != FORMAT_VERSION:
raise ValueError("Unsupported capture format. Start a new capture with this version of Aleph.")
- bounds(run["bounds"], minimum=0)
+ vertices(run["polygon"], minimum=0)
options = settings(run["options"])
if [s["mode"] for s in run["stages"]] != options["include"]:
raise ValueError("Invalid saved stages.")
for stage in run["stages"]:
if stage["mode"] in ("satellite", "terrain"):
- if stage["grid"] != grid(run["bounds"], options[stage["mode"] + "_zoom"]):
+ if stage["grid"] != tile_grid(stage["mode"], run["polygon"], options):
raise ValueError("Invalid saved tile grid.")
if stage["mode"] == "mesh" and stage["level"] != options["mesh_level"]:
raise ValueError("Invalid saved mesh level.")
diff --git a/src/cli.py b/src/cli.py
index c3a822a..c7a42f2 100644
--- a/src/cli.py
+++ b/src/cli.py
@@ -142,7 +142,9 @@ def _capture_command(commands):
captures = commands.add_parser("capture", help="Create, resume, or export an area capture")
actions = captures.add_subparsers(dest="action", required=True)
area = actions.add_parser("create", help="Plan and download an area with multiple sources")
- area.add_argument("--bbox", required=True, nargs=4, type=float, metavar=("S", "W", "N", "E"))
+ shape = area.add_mutually_exclusive_group(required=True)
+ shape.add_argument("--bbox", nargs=4, type=float, metavar=("S", "W", "N", "E"))
+ shape.add_argument("--polygon", nargs="+", type=float, metavar="LAT LON", help="polygon corners in order, at least three")
area.add_argument("--sources", dest="include", nargs="+", choices=capture.SOURCES, help="sources to download (default: streetview satellite osm terrain)")
# Planning and confirmation.
diff --git a/src/dashboard.py b/src/dashboard.py
index e87f659..1a0bae2 100644
--- a/src/dashboard.py
+++ b/src/dashboard.py
@@ -11,7 +11,7 @@ from urllib.parse import parse_qs, unquote, urlsplit
from . import capture, layers, places
from .common import CachedClient, Client, RequestError, contained, now
-from .geo import bounds, collection, coordinate, feature, grid
+from .geo import collection, coordinate, extent, feature, lines, vertices
WEB = Path(__file__).resolve().parent / "web"
PHOTO_KEYS = ("sequence", "filename", "heading", "side", "imagery_date", "pano_id", "path_name", "streetview_url")
@@ -61,24 +61,19 @@ class Job:
print(f"alephgeo dashboard: {self.error}", file=sys.stderr)
-def imagery(area, stage, tile_size):
+def imagery(polygon, stage, tile_size):
g = stage["grid"]
- return dict(tiles=g["rows"] * g["columns"], zoom=g["zoom"], width=g["width"], height=g["height"],
- meters_per_pixel=capture.resolution(area, g["zoom"], tile_size))
+ return dict(tiles=g["count"], zoom=g["zoom"], width=g["width"], height=g["height"],
+ meters_per_pixel=capture.resolution(polygon, g["zoom"], tile_size))
def grid_lines(g, kind):
"""Tile boundaries, or just the outline of large grids."""
- x0, y0, x1, y1 = g["x0"], g["y0"], g["x0"] + g["columns"], g["y0"] + g["rows"]
-
def point(x, y):
lat, lon = coordinate(x * 256, y * 256, g["zoom"])
return [lon, lat]
- xs = range(x0, x1 + 1) if g["columns"] <= 64 else (x0, x1)
- ys = range(y0, y1 + 1) if g["rows"] <= 64 else (y0, y1)
- lines = [[point(x, y0), point(x, y1)] for x in xs] + [[point(x0, y), point(x1, y)] for y in ys]
- return feature("MultiLineString", lines, dict(kind=kind))
+ return feature("MultiLineString", [[point(*a), point(*b)] for a, b in lines(g)], dict(kind=kind))
def plan_layers(run):
@@ -97,7 +92,7 @@ def plan_layers(run):
def preview(run):
- area, stages = run["bounds"], {}
+ stages = {}
for stage in run["stages"]:
mode = stage["mode"]
if mode == "streetview":
@@ -105,14 +100,14 @@ def preview(run):
panoramas=stage["coverage"]["available"], gaps=len(stage["coverage"]["gaps"]),
meters=stage["length"])
elif mode in ("satellite", "terrain"):
- stages[mode] = imagery(area, stage, 512 if mode == "terrain" else 256)
+ stages[mode] = imagery(run["polygon"], stage, 512 if mode == "terrain" else 256)
elif mode == "mesh":
stages[mode] = dict(nodes=len(stage["nodes"]), level=stage["level"])
else:
stages[mode] = {}
stages[mode]["seconds"] = capture.estimate(dict(stages=[stage], options=run["options"]))["seconds"]
- return dict(bounds=area, options=run["options"], seconds=capture.estimate(run)["seconds"], stages=stages,
- layers=plan_layers(run))
+ return dict(bounds=extent(run["polygon"]), options=run["options"], seconds=capture.estimate(run)["seconds"],
+ stages=stages, layers=plan_layers(run))
def summary(identity, run, live):
@@ -126,16 +121,19 @@ def summary(identity, run, live):
if "grid" in stage:
info.update(zoom=stage["grid"]["zoom"], minzoom=layers.lowest(stage))
state = "interrupted" if run["state"] == "running" and not live else run["state"]
- return dict(id=identity, bounds=run["bounds"], started_at=run["started_at"], finished_at=run.get("finished_at"),
+ # Polygons go to the browser in GeoJSON order, [lon, lat].
+ return dict(id=identity, bounds=extent(run["polygon"]), polygon=[point[::-1] for point in run["polygon"]],
+ started_at=run["started_at"],
+ finished_at=run.get("finished_at"),
state=state, error=run.get("error"), options=run["options"], stages=stages)
def area_and_options(body):
try:
- area = bounds([float(value) for value in body["bounds"]])
+ polygon = vertices(point[::-1] for point in body["polygon"])
except (KeyError, TypeError, ValueError) as error:
- raise ValueError(f"Draw a valid rectangle first. {error}".strip()) from error
- return area, capture.settings(body.get("options"))
+ raise ValueError(f"Draw a valid area first. {error}".strip()) from error
+ return polygon, capture.settings(body.get("options"))
class Dashboard:
@@ -161,10 +159,10 @@ class Dashboard:
return Client(delay, cancel=job.check, cache_dir=self.cache_dir, refresh=self.refresh)
def plan(self, body):
- area, options = area_and_options(body)
+ polygon, options = area_and_options(body)
def work(job):
- job.run = capture.plan(self.client(job, options["delay"]), area, options, job.progress)
+ job.run = capture.plan(self.client(job, options["delay"]), polygon, options, job.progress)
job.preview = preview(job.run)
return self.start("plan", work)
@@ -341,14 +339,14 @@ class Dashboard:
case "GET", ["api", "captures", identity, "files", *path] if path:
return self.file(identity, path)
case "POST", ["api", "estimate"]:
- area, options = area_and_options(body)
+ polygon, options = area_and_options(body)
result = {}
for mode in ("satellite", "terrain"):
if mode in options["include"]:
- stage = dict(mode=mode, grid=grid(area, options[f"{mode}_zoom"]), results=[])
- result[mode] = dict(imagery(area, stage, 512 if mode == "terrain" else 256),
+ stage = dict(mode=mode, grid=capture.tile_grid(mode, polygon, options), results=[])
+ result[mode] = dict(imagery(polygon, stage, 512 if mode == "terrain" else 256),
seconds=capture.estimate(dict(stages=[stage], options=options))["seconds"])
- result["planning"] = capture.planning(area, options)
+ result["planning"] = capture.planning(polygon, options)
return result
case "POST", ["api", "plan"]:
return self.plan(body)
diff --git a/src/geo.py b/src/geo.py
index 0ca68f6..f0ca8cb 100644
--- a/src/geo.py
+++ b/src/geo.py
@@ -5,11 +5,15 @@ from bisect import bisect_right
from collections import defaultdict
from itertools import pairwise
+import numpy as np
+
from .common import number, positive
RADIUS = 6_371_008.8
MERCATOR_RADIUS = 6_378_137
MAX_LATITUDE = 85.0511287798066
+MAX_VERTICES = 1000
+DETAILED_GRID = 64 # Rows and columns up to which lines() draws every tile.
def clamp(value, low, high):
@@ -73,14 +77,114 @@ def bounds(values, *, minimum=1):
if not (-90 <= south <= north <= 90 and -180 <= west <= east <= 180):
raise ValueError("Use latitude −90…90 and longitude −180…180.")
if east - west > 180:
- raise ValueError("Split areas crossing the date line into two rectangles.")
+ raise ValueError("Split areas crossing the date line in two.")
middle = (south + north) / 2
size = min(distance((middle, west), (middle, east)), distance((south, west), (north, west)))
if size <= 0 or size < minimum:
- raise ValueError(f"The rectangle must be nonempty and at least {minimum} meter(s) wide and high.")
+ raise ValueError(f"The area must be nonempty and at least {minimum} meter(s) wide and high.")
return south, west, north, east
+def corners(area):
+ """A rectangle as a polygon, counterclockwise from its southwest corner."""
+ south, west, north, east = area
+ return [(south, west), (south, east), (north, east), (north, west)]
+
+
+def crosses(a, b, c, d):
+ """Whether segments a–b and c–d cross at a point inside both."""
+ def side(p, q, r):
+ return (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0])
+
+ return side(a, b, c) * side(a, b, d) < 0 and side(c, d, a) * side(c, d, b) < 0
+
+
+def vertices(values, *, minimum=1):
+ """A simple polygon of (lat, lon) points, without repeating the first one at the end."""
+ polygon = []
+ for value in values:
+ vertex = point(value)
+ if not polygon or vertex != polygon[-1]:
+ polygon.append(vertex)
+ if len(polygon) > 1 and polygon[0] == polygon[-1]:
+ polygon.pop()
+ if not 3 <= len(polygon) <= MAX_VERTICES:
+ raise ValueError(f"A polygon needs 3 to {MAX_VERTICES} distinct points.")
+ bounds(extent(polygon), minimum=minimum)
+ ring = [*polygon, polygon[0]]
+ sides = list(pairwise(ring))
+ for i, (a, b) in enumerate(sides):
+ # Neighbors share a point, including the last and first sides.
+ for c, d in sides[i + 2:len(sides) - (i == 0)]:
+ if crosses(a, b, c, d):
+ raise ValueError("The polygon's edges must not cross.")
+ if not signed_area(ring):
+ raise ValueError("The polygon must enclose an area.")
+ return polygon
+
+
+def within(polygon, lats, lons):
+ """ring_contains for arrays of latitudes and longitudes."""
+ inside = np.zeros(np.shape(lats), bool)
+ for (alat, alon), (blat, blon) in pairwise([*polygon, polygon[0]]):
+ if alat != blat:
+ inside ^= ((alat > lats) != (blat > lats)) & (lons < alon + (lats - alat) * (blon - alon) / (blat - alat))
+ return inside
+
+
+def pieces(a, b, polygon):
+ """Parts of segment a–b inside the polygon, in order from a."""
+ ring = [*polygon, polygon[0]]
+ dlat, dlon = b[0] - a[0], b[1] - a[1]
+ cuts = [0, 1]
+ for c, d in pairwise(ring):
+ elat, elon = d[0] - c[0], d[1] - c[1]
+ if denominator := dlat * elon - dlon * elat:
+ t = ((c[0] - a[0]) * elon - (c[1] - a[1]) * elat) / denominator
+ u = ((c[0] - a[0]) * dlon - (c[1] - a[1]) * dlat) / denominator
+ if 0 < t < 1 and 0 <= u <= 1:
+ cuts.append(t)
+
+ def at(t):
+ return a if t == 0 else b if t == 1 else (a[0] + t * dlat, a[1] + t * dlon)
+
+ parts = []
+ for start, end in pairwise(sorted(cuts)):
+ if start < end and ring_contains(ring, at((start + end) / 2)):
+ if parts and parts[-1][1] == start:
+ parts[-1][1] = end
+ else:
+ parts.append([start, end])
+ return [(at(start), at(end)) for start, end in parts if distance(at(start), at(end)) > 0.01]
+
+
+def offset(point, origin):
+ """Meters east and north of a nearby origin, on a plane."""
+ scale = math.radians(RADIUS)
+ return (((point[1] - origin[1] + 180) % 360 - 180) * scale * math.cos(math.radians(origin[0])),
+ (point[0] - origin[0]) * scale)
+
+
+def near(polygon, point, meters):
+ """Whether a point lies inside the polygon or within a short distance of its edges."""
+ ring = [*polygon, polygon[0]]
+ if ring_contains(ring, point):
+ return True
+ for (ax, ay), (bx, by) in pairwise([offset(p, point) for p in ring]):
+ dx, dy = bx - ax, by - ay
+ t = clamp(-(ax * dx + ay * dy) / (dx * dx + dy * dy), 0, 1) if dx or dy else 0
+ if math.hypot(ax + t * dx, ay + t * dy) <= meters:
+ return True
+ return False
+
+
+def measure(polygon):
+ """Approximate ground area in square meters and perimeter in meters."""
+ origin = sum(p[0] for p in polygon) / len(polygon), polygon[0][1]
+ ring = [offset(p, origin) for p in [*polygon, polygon[0]]]
+ return abs(signed_area(ring)), sum(math.dist(a, b) for a, b in pairwise(ring))
+
+
def around(center, size):
"""A square in ground meters, returned in south/west/north/east order."""
lat, lon = point(center)
@@ -94,11 +198,6 @@ def around(center, size):
return bounds((lat - dy, lon - dx, lat + dy, lon + dx), minimum=0)
-def inside(point, area):
- south, west, north, east = area
- return south - 1e-9 <= point[0] <= north + 1e-9 and west - 1e-9 <= point[1] <= east + 1e-9
-
-
def clip(a, b, area):
"""Liang–Barsky clipping also retains segments with both ends outside."""
south, west, north, east = area
@@ -198,8 +297,12 @@ def coordinate(x, y, zoom):
return math.degrees(math.atan(math.sinh(math.pi * (1 - 2 * y / size)))), x / size * 360 - 180
-def grid(area, zoom):
- south, west, north, east = area
+def grid(polygon, zoom):
+ """Tiles meeting the polygon and the pixels enclosing it; a rectangle meets every tile of its grid.
+
+ Spans list those tiles in download order: [row, first column, column after the last, tiles in earlier spans].
+ """
+ south, west, north, east = extent(polygon)
if south < -MAX_LATITUDE or north > MAX_LATITUDE:
raise ValueError("Imagery areas must stay within latitudes −85.0511…85.0511.")
@@ -211,17 +314,73 @@ def grid(area, zoom):
left, top = (math.floor(snap(v)) for v in pixel((north, west), zoom))
right, bottom = (math.ceil(snap(v)) for v in pixel((south, east), zoom))
+ x0, y0 = left // 256, top // 256
+ columns, rows = math.ceil(right / 256) - x0, math.ceil(bottom / 256) - y0
+ points = [tuple(snap(v) / 256 - origin for v, origin in zip(pixel(p, zoom), (x0, y0))) for p in polygon]
+ spans, count = [], 0
+ for row in range(rows):
+ for start, end in meeting(points, row, columns):
+ spans.append([row, start, end, count])
+ count += end - start
return dict(zoom=zoom, left=left, top=top, width=right - left, height=bottom - top,
- x0=left // 256, y0=top // 256,
- columns=math.ceil(right / 256) - left // 256, rows=math.ceil(bottom / 256) - top // 256)
+ x0=x0, y0=y0, columns=columns, rows=rows, spans=spans, count=count)
+
+
+def meeting(points, row, columns):
+ """Column ranges of a row's tiles that meet a polygon in tile units, ignoring touches along tile edges."""
+ ranges, crossings = [], []
+ for (ax, ay), (bx, by) in pairwise([*points, points[0]]):
+ # Part of the edge within the row.
+ if ay == by:
+ if row < ay < row + 1:
+ ranges.append((min(ax, bx), max(ax, bx)))
+ continue
+ low, high = sorted(((row - ay) / (by - ay), (row + 1 - ay) / (by - ay)))
+ low, high = max(low, 0), min(high, 1)
+ if low < high:
+ ranges.append(tuple(sorted((ax + low * (bx - ax), ax + high * (bx - ax)))))
+ # Tiles with no edges inside lie between edges crossing the row's middle.
+ if (ay > row + 0.5) != (by > row + 0.5):
+ crossings.append(ax + (row + 0.5 - ay) * (bx - ax) / (by - ay))
+ crossings.sort()
+ ranges += zip(crossings[::2], crossings[1::2])
+ runs = []
+ for start, end in sorted((max(math.floor(low), 0), min(math.ceil(high), columns)) for low, high in ranges):
+ if start >= end:
+ continue
+ if runs and start <= runs[-1][1]:
+ runs[-1] = runs[-1][0], max(end, runs[-1][1])
+ else:
+ runs.append((start, end))
+ return runs
def tiles(grid):
- for row in range(grid["rows"]):
- for column in range(grid["columns"]):
+ for row, start, end, _ in grid["spans"]:
+ for column in range(start, end):
yield dict(x=grid["x0"] + column, y=grid["y0"] + row, zoom=grid["zoom"], row=row, column=column)
+def lines(grid):
+ """Edges of a grid's tiles as ((x, y), (x, y)) in tile units, or just their outline for large grids."""
+ detailed = grid["rows"] <= DETAILED_GRID and grid["columns"] <= DETAILED_GRID
+ x0, y0 = grid["x0"], grid["y0"]
+ rows = [[] for _ in range(grid["rows"] + 2)] # Empty rows above and below.
+ segments = []
+ for row, start, end, _ in grid["spans"]:
+ rows[row + 1].append((start, end))
+ for column in range(start, end + 1) if detailed else (start, end):
+ segments.append(((x0 + column, y0 + row), (x0 + column, y0 + row + 1)))
+ for row, (above, below) in enumerate(pairwise(rows)):
+ # Every tile's top and bottom, or only where coverage changes between rows.
+ ends = sorted({x for run in above + below for x in run})
+ for low, high in pairwise(ends):
+ depth = sum(start <= low < end for start, end in above + below)
+ if depth == 1 or detailed and depth:
+ segments.append(((x0 + low, y0 + row), (x0 + high, y0 + row)))
+ return segments
+
+
def tile_ring(tile):
north, west = coordinate(tile["x"] * 256, tile["y"] * 256, tile["zoom"])
south, east = coordinate((tile["x"] + 1) * 256, (tile["y"] + 1) * 256, tile["zoom"])
diff --git a/src/layers.py b/src/layers.py
index 7a9c709..52a7b97 100644
--- a/src/layers.py
+++ b/src/layers.py
@@ -3,6 +3,7 @@
import math
import threading
import zlib
+from bisect import bisect_left
from collections import OrderedDict, defaultdict
from io import BytesIO
from xml.etree import ElementTree as ET
@@ -46,20 +47,16 @@ PATHS = {"footway", "path", "cycleway", "steps", "pedestrian", "track", "bridlew
def saved(grid, count, z, x, y):
- """One past the last saved grid tile inside tile z/x/y, or 0; downloads are row-major."""
+ """One past the last saved grid tile inside tile z/x/y, or 0; downloads follow the grid's spans."""
factor = 2 ** (grid["zoom"] - z)
- left = max(x * factor - grid["x0"], 0)
- right = min((x + 1) * factor - grid["x0"], grid["columns"])
- top = max(y * factor - grid["y0"], 0)
- bottom = min((y + 1) * factor - grid["y0"], grid["rows"])
- if left >= right or top >= bottom:
- return 0
- columns = grid["columns"]
- row = min(bottom - 1, (count - 1) // columns)
- end = min(right, count - row * columns)
- if end <= left:
- row, end = row - 1, right
- return row * columns + end if row >= top else 0
+ left, top = x * factor - grid["x0"], y * factor - grid["y0"]
+ spans = grid["spans"]
+ # The block's rows, latest first; spans start with their row.
+ for row, start, end, before in reversed(spans[bisect_left(spans, [top]):bisect_left(spans, [top + factor])]):
+ stop = min(end, left + factor, start + count - before)
+ if max(start, left) < stop:
+ return before + stop - start
+ return 0
def lowest(stage):
diff --git a/src/mesh.py b/src/mesh.py
index dbbd265..8aee040 100644
--- a/src/mesh.py
+++ b/src/mesh.py
@@ -16,12 +16,14 @@ import tempfile
import numpy as np
from .common import atomic_path, fetch
+from .geo import extent, near, within
from .pbf import fields, varints
BASE = "https://kh.google.com/rt/earth/"
LEAF, NODATA, USE_IMAGERY_EPOCH = 4, 8, 16
JPEG = 1
LOWEST, HIGHEST = -1000, 9000 # Heights searched for nodes, in meters from the sphere.
+NEAR = 100_000 # Largest node half-diagonal, in meters, checked against the polygon; larger ones always count.
GLB_LIMIT = 2**32
TILES_PER_MESH = 64 # Unreal's Nanite allows 64 materials per mesh; Godot allows 256 surfaces.
@@ -77,6 +79,18 @@ def overlaps(a, b):
return all(abs(dot(d, x)) <= sum(h * abs(dot(r, x)) for h, r in zip(h1 + h2, r1 + r2)) for x in axes)
+def degrees(x, y, z):
+ """Latitude and longitude of planet coordinates, as numbers or arrays; frame() goes the other way."""
+ return np.degrees(np.arctan2(z, np.hypot(x, y))), np.degrees(np.arctan2(y, x))
+
+
+def reaches(polygon, box):
+ """Whether a node's box may hold ground in the polygon, judging by its center and half-diagonal."""
+ center, half, _ = box
+ size = math.hypot(*half)
+ return size > NEAR or near(polygon, degrees(*center), size * 1.05 + 1)
+
+
def node_box(packed, head, texel):
if len(packed) != 15:
raise ValueError("Unexpected 3D mesh node bounds.")
@@ -118,12 +132,12 @@ def bulk(data, head, epoch):
yield path, packed, node.get(2, epoch), node.get(5, epoch), node.get(7, imagery), box
-def plan(client, area, level, workers, progress, *, allow_empty=False):
- """Nodes with data meeting the rectangle down to level, and the octants their children refine."""
+def plan(client, polygon, level, workers, progress, *, allow_empty=False):
+ """Nodes with data meeting the polygon down to level, and the octants their children refine."""
planet = dict(fields(client.get(BASE + "PlanetoidMetadata")))
root = dict(fields(planet[1]))
radius = struct.unpack("<f", planet[2])[0]
- target = search_box(area, radius)
+ target = search_box(extent(polygon), radius)
found, existing = [], set()
pending, done = [("", root.get(5, root[2]))], 0
progress("Finding 3D mesh nodes", done)
@@ -142,7 +156,7 @@ def plan(client, area, level, workers, progress, *, allow_empty=False):
existing.add(path)
# Parents come first; skip subtrees outside the area.
if ((len(path) > len(head) + 1 and path[:-1] not in hit) or len(path) > level
- or box is None or not overlaps(target, box)):
+ or box is None or not overlaps(target, box) or not reaches(polygon, box)):
continue
hit.add(path)
if not flags & NODATA:
@@ -266,24 +280,19 @@ def decode(data, mask, axes, radius, inside):
yield positions, texcoords, indices, texture[1]
-def crop(area, axes, radius):
- """A test for local east/north/up points inside the rectangle's latitudes and longitudes."""
- south, west, north, east = (math.radians(value) for value in area)
+def crop(polygon, axes, radius):
+ """A test for local east/north/up points inside the polygon."""
(ex, ey, ez), (nx, ny, nz), (ux, uy, uz) = axes
- low, high = math.sin(south), math.sin(north)
- west_x, west_y, east_x, east_y = -math.sin(west), math.cos(west), -math.sin(east), math.cos(east)
def inside(e, n, u):
r = radius + u
x, y, z = ex * e + nx * n + ux * r, ey * e + ny * n + uy * r, ez * e + nz * n + uz * r
- length = np.sqrt(x * x + y * y + z * z)
- return ((low * length <= z) & (z <= high * length)
- & (x * west_x + y * west_y >= 0) & (x * east_x + y * east_y <= 0))
+ return within(polygon, *degrees(x, y, z))
return inside
-def export(path, folder, stage, area, progress):
+def export(path, folder, stage, polygon, progress):
"""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
@@ -291,9 +300,10 @@ def export(path, folder, stage, area, progress):
"""
nodes, results = stage["nodes"], stage["results"]
radius, block = stage["radius"], max(1, stage["level"] - 4)
+ area = extent(polygon)
lat, lon = (area[0] + area[2]) / 2, (area[1] + area[3]) / 2
axes = frame(lat, lon)
- inside = crop(area, axes, radius)
+ inside = crop(polygon, axes, radius)
# Keep parent octants whose child was unavailable, so gaps stay covered.
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]
diff --git a/src/osm.py b/src/osm.py
index e1f02e0..a862f79 100644
--- a/src/osm.py
+++ b/src/osm.py
@@ -83,10 +83,11 @@ class Source:
self.regions[url] = path, dict(source_url=url, region=props["name"], osm_data_at=stamp or None)
return self.regions[url]
- def export(self, area, output, progress):
+ def export(self, polygon, output, progress):
+ area = extent(polygon)
source, metadata = self.region(area, progress)
progress("Extracting OSM map")
- PBF(source, self.client.check_cancel).export(area, output)
+ PBF(source, self.client.check_cancel).export(polygon, output)
return metadata
def data(self, area, *, poi=False, progress=lambda *args: None):
diff --git a/src/pbf.py b/src/pbf.py
index ef8e68b..f05842b 100644
--- a/src/pbf.py
+++ b/src/pbf.py
@@ -23,6 +23,7 @@ from xml.sax.saxutils import quoteattr
import numpy as np
from .common import atomic_path
+from .geo import extent, within
MAX_BLOB = 32 * 1024 * 1024
MEMBERS = ("node", "way", "relation")
@@ -196,7 +197,7 @@ def dense(message):
def scan_nodes(task):
- path, entry, area, retain = task
+ path, entry, area, polygon, retain = task
values, groups = block(path, entry)
gran = values.get(17, 100)
lat_offset, lon_offset = signed64(values.get(19, 0)), signed64(values.get(20, 0))
@@ -229,6 +230,9 @@ def scan_nodes(task):
last = int(ids[-1])
if area is not None:
inside = (lats >= south) & (lats <= north) & (lons >= west) & (lons <= east)
+ if polygon is not None and inside.any():
+ inside[inside] = within(polygon, (lats[inside] * gran + lat_offset) / 1e9,
+ (lons[inside] * gran + lon_offset) / 1e9)
hits.append((ids[inside], lats[inside], lons[inside]))
selected, lats, lons = (np.concatenate(parts) for parts in zip(*hits)) if hits else [np.zeros(0, np.int64)] * 3
coordinates = (array("q", lats.tobytes()), array("q", lons.tobytes())) if retain else None
@@ -639,11 +643,11 @@ class PBF:
return
yield from self.objects(4, wanted)
- def select(self, area, xml=False):
+ def select(self, area, xml=False, polygon=None):
self.way_xml = {}
self.selected_coordinates.clear()
spatial, nodes, ways, relations = set(), set(), set(), set()
- tasks = ((self.path, entry, area, xml) for entry in self.entries)
+ tasks = ((self.path, entry, area, polygon, xml) for entry in self.entries)
last = None
for entry, (kinds, first, end, selected, coordinates) in zip(self.entries, self.parallel(scan_nodes, tasks)):
entry[2:] = kinds, first, end
@@ -724,7 +728,7 @@ class PBF:
return
unknown = [entry for entry in self.entries if not entry[2] or (entry[2] & 6 and entry[3] is None)]
for entry, (kinds, first, last, _, _) in zip(
- unknown, self.parallel(scan_nodes, ((self.path, e, None, False) for e in unknown))):
+ unknown, self.parallel(scan_nodes, ((self.path, e, None, None, False) for e in unknown))):
entry[2:] = kinds, first, last
tasks = []
for entry in self.entries:
@@ -797,8 +801,9 @@ class PBF:
item["center"] = (box[0] + box[2]) / 2, (box[1] + box[3]) / 2
return items
- def export(self, area, output):
- selected = self.select(area, xml=True)
+ def export(self, polygon, output):
+ area = extent(polygon)
+ selected = self.select(area, xml=True, polygon=polygon)
south, west, north, east = area
with atomic_path(output) as temporary, temporary.open("wb") as stream:
stream.write((f'<?xml version="1.0" encoding="UTF-8"?>\n<osm version="0.6" generator="Aleph">\n'
diff --git a/src/quick.py b/src/quick.py
index 27fe804..28e5015 100644
--- a/src/quick.py
+++ b/src/quick.py
@@ -5,11 +5,11 @@ from pathlib import Path
from . import capture, places, routes, streetview
from .common import WORKERS, MissingImagery, RequestError, now, number, positive, write_json
-from .geo import around, bearing, bounds, coordinate, distance, extent, point
+from .geo import around, bearing, bounds, coordinate, corners, distance, extent, point
def coverage(client, area, progress, workers):
try:
- return streetview.coverage(client, area, progress, workers)
+ return streetview.coverage(client, corners(area), progress, workers)
except (OSError, ValueError) as error:
raise RequestError("provider_unavailable", f"Street View coverage request failed: {error}") from error
@@ -28,7 +28,7 @@ def street_samples(client, sections, stops, step, progress, workers):
area = bounds((lower[0], lower[1], upper[2], upper[3]))
views = coverage(client, area, progress, workers)
ways, _ = client.maps.data(area, progress=progress)
- context = streetview.roads(ways, area, "all")
+ context = streetview.roads(ways, corners(area), "all")
samples, gaps, requested = [], [], 0
counts = routes.allocate_stops(sections, stops) if stops is not None else [None] * len(sections)
for section, count in zip(sections, counts):
@@ -159,7 +159,7 @@ def satellite(client, output, progress, *, at=None, place=None, match=None, best
center, selected_place = location(client, at=at, place=place, match=match,
best_match=best_match, endpoint=endpoint)
area = around(center, size)
- run = capture.plan(client, area, dict(include=["satellite"], satellite_zoom=zoom,
+ run = capture.plan(client, corners(area), dict(include=["satellite"], satellite_zoom=zoom,
satellite_format=satellite_format, satellite_workers=workers), progress)
folder = capture.create_folder(run, output)
try:
diff --git a/src/satellite.py b/src/satellite.py
index 4f99efa..58726a3 100644
--- a/src/satellite.py
+++ b/src/satellite.py
@@ -12,10 +12,10 @@ import tempfile
import zlib
from array import array
-from PIL import Image
+from PIL import Image, ImageChops, ImageDraw
from .common import atomic_path, contained
-from .geo import MERCATOR_RADIUS
+from .geo import MERCATOR_RADIUS, pixel, tiles
BLOCK = 256
TIFF_LIMIT = 2**32
@@ -163,8 +163,16 @@ def write_png(path, level, spool, progress):
chunk(b"IEND", b"")
-def merge(stage, folder, progress):
- """Write satellite.tif and satellite.png, retaining transparent gaps."""
+def clear(image, outline):
+ """Make pixels outside a polygon in image pixels transparent, keeping those its edges pass through."""
+ with Image.new("L", image.size) as inside:
+ ImageDraw.Draw(inside).polygon(outline, fill=255, outline=255)
+ with image.getchannel("A") as alpha, ImageChops.multiply(alpha, inside) as kept:
+ image.putalpha(kept)
+
+
+def merge(stage, folder, progress, polygon):
+ """Write satellite.tif and satellite.png, retaining transparent gaps and clearing pixels outside the polygon."""
grid = stage["grid"]
levels = [Level(grid["width"], grid["height"])]
while max(levels[-1].width, levels[-1].height) > BLOCK:
@@ -174,14 +182,17 @@ def merge(stage, folder, progress):
done = 0
progress("Building satellite.tif", done, count)
+ saved = {}
+ for planned, tile in zip(tiles(grid), stage["results"]):
+ if (tile["x"], tile["y"], tile["zoom"]) != (planned["x"], planned["y"], grid["zoom"]):
+ raise ValueError("Invalid saved satellite tile order.")
+ saved[tile["x"], tile["y"]] = tile["filename"]
+ outline = [(x - grid["left"], y - grid["top"]) for x, y in (pixel(point, grid["zoom"]) for point in polygon)]
+
def source(x, y):
- index = (y - grid["y0"]) * grid["columns"] + x - grid["x0"]
- if index >= len(stage["results"]):
+ if (x, y) not in saved:
return Image.new("RGBA", (BLOCK, BLOCK))
- tile = stage["results"][index]
- if (tile["x"], tile["y"], tile["zoom"]) != (x, y, grid["zoom"]):
- raise ValueError("Invalid saved satellite tile order.")
- with Image.open(contained(folder, tile["filename"])) as image:
+ with Image.open(contained(folder, saved[x, y])) as image:
if image.format not in ("JPEG", "PNG") or image.size != (BLOCK, BLOCK):
raise ValueError("Expected a 256 × 256 satellite JPEG or PNG.")
return image.convert("RGBA")
@@ -198,6 +209,7 @@ def merge(stage, folder, progress):
if i == 0:
left, top = grid["left"] + x, grid["top"] + y
with region((left, top, left + width, top + height), source) as image:
+ clear(image, [(u - x, v - y) for u, v in outline])
level.write(spool, image)
else:
previous = levels[i - 1]
diff --git a/src/streetview.py b/src/streetview.py
index 0d741e1..7f1bf48 100644
--- a/src/streetview.py
+++ b/src/streetview.py
@@ -11,7 +11,7 @@ from pathlib import Path
from PIL import Image
from .common import MissingImagery, fetch, number, open_image, save_image, url, write_bytes
-from .geo import Line, RoadIndex, clip, distance, grid, inside, tiles
+from .geo import Line, RoadIndex, distance, extent, grid, pieces, ring_contains, tiles
EXCLUDED = re.compile(r"^(construction|proposed|planned|abandoned|razed|demolished)$")
MAIN = r"(motorway|trunk|primary|secondary|tertiary)(_link)?"
@@ -61,10 +61,10 @@ def parse_coverage(data):
raise ValueError("Unreadable Google panorama coverage; the endpoint may have changed.") from error
-def coverage(client, area, progress, workers):
- """Every Google panorama listed in the zoom-17 tiles that cover the area."""
- coverage_grid = grid(area, 17)
- count = coverage_grid["rows"] * coverage_grid["columns"]
+def coverage(client, polygon, progress, workers):
+ """Every Google panorama listed in the zoom-17 tiles that meet the polygon."""
+ coverage_grid = grid(polygon, 17)
+ count = coverage_grid["count"]
found = {}
progress("Finding panoramas", 0, count)
addresses = (url("https://www.google.com/maps/photometa/ac/v1",
@@ -180,8 +180,8 @@ def save_sphere(client, metadata, zoom, target, workers):
tile_count=columns * rows, source_url=metadata_url(metadata["pano_id"]))
-def roads(ways, area, depth):
- """Clip mapped roads to the area, splitting them wherever they leave it or repeat an edge."""
+def roads(ways, polygon, depth):
+ """Clip mapped roads to the polygon, splitting them wherever they leave it or repeat an edge."""
parts, seen_edges = [], set()
for way in sorted(ways, key=lambda way: way["id"]):
tags = way["tags"]
@@ -195,18 +195,19 @@ def roads(ways, area, depth):
for i, (a, b) in enumerate(pairwise(points)):
if distance(a, b) <= 0.01:
continue
- segment = clip(a, b, area)
edge = layer, tuple(sorted((nodes[i], nodes[i + 1])))
- if segment is None or edge in seen_edges:
+ segments = [] if edge in seen_edges else pieces(a, b, polygon)
+ if not segments:
runs.append([])
continue
seen_edges.add(edge)
- if runs[-1] and distance(runs[-1][-1], segment[0]) > 0.01:
- runs.append([])
- if not runs[-1]:
- runs[-1].append(segment[0])
- runs[-1].append(segment[1])
- if not inside(b, area):
+ for start, end in segments:
+ if runs[-1] and distance(runs[-1][-1], start) > 0.01:
+ runs.append([])
+ if not runs[-1]:
+ runs[-1].append(start)
+ runs[-1].append(end)
+ if segments[-1][1] != b:
runs.append([])
for part, run in enumerate((run for run in runs if len(run) > 1), 1):
parts.append(dict(id=f"way-{way['id']}-{part}", osm_id=way["id"], name=tags.get("name", ""),
@@ -282,14 +283,15 @@ def select_stops(candidates, spacing):
index = following
-def plan(client, area, options, progress, *, allow_empty=False):
+def plan(client, polygon, options, progress, *, allow_empty=False):
progress("Finding roads")
- ways, metadata = client.maps.data(area, progress=progress)
- parts = roads(ways, area, options["depth"])
+ ways, metadata = client.maps.data(extent(polygon), progress=progress)
+ parts = roads(ways, polygon, options["depth"])
if not parts and not allow_empty:
raise ValueError("No mapped roads at this path depth. Choose another area or depth.")
- found = [view for view in coverage(client, area, progress, options["streetview_workers"])
- if inside((view["lat"], view["lon"]), area)]
+ ring = [*polygon, polygon[0]]
+ found = [view for view in coverage(client, polygon, progress, options["streetview_workers"])
+ if ring_contains(ring, (view["lat"], view["lon"]))]
progress("Indexing roads")
lines = [Line(part["points"]) for part in parts]
index = RoadIndex(lines)
diff --git a/src/web/app.js b/src/web/app.js
index cd4884e..347fe6d 100644
--- a/src/web/app.js
+++ b/src/web/app.js
@@ -105,13 +105,46 @@ function size([s, w, n, e]) {
return [distance([middle, w], [middle, e]), distance([s, w], [n, w])];
}
-const dimensions = area => size(area).map(meters).join(" × ");
-const ring = ([s, w, n, e]) => [[w, s], [e, s], [e, n], [w, n], [w, s]];
-const polygon = (area, properties = {}) => ({type: "Feature", properties, geometry: {type: "Polygon", coordinates: [ring(area)]}});
+// Areas are polygons of [lng, lat] points, without repeating the first, as the server sends and expects them.
+const corners = ([s, w, n, e]) => [[w, s], [e, s], [e, n], [w, n]];
+const extent = points => {
+ const lngs = points.map(p => p[0]), lats = points.map(p => p[1]);
+ return [Math.min(...lats), Math.min(...lngs), Math.max(...lats), Math.max(...lngs)];
+};
+const rectangle = points => points.length === 4 && new Set(points.map(p => p[0])).size === 2
+ && new Set(points.map(p => p[1])).size === 2;
+const polygon = (points, properties = {}) => ({type: "Feature", properties,
+ geometry: {type: "Polygon", coordinates: [[...points, points[0]]]}});
+const line = coordinates => ({type: "Feature", properties: {}, geometry: {type: "LineString", coordinates}});
const point = (coordinates, properties = {}) => ({type: "Feature", properties, geometry: {type: "Point", coordinates}});
const collection = features => ({type: "FeatureCollection", features});
const lngLatBounds = ([s, w, n, e]) => [[w, s], [e, n]];
+// Ground area in square meters, flattened around the middle latitude.
+function surface(points) {
+ const r = Math.PI / 180, [s, , n] = extent(points);
+ const twice = points.reduce((sum, [x1, y1], i) => {
+ const [x2, y2] = points[(i + 1) % points.length];
+ return sum + x1 * y2 - x2 * y1;
+ }, 0);
+ return Math.abs(twice) / 2 * (EARTH * r) ** 2 * Math.cos((s + n) / 2 * r);
+}
+
+function dimensions(points) {
+ if (rectangle(points)) return size(extent(points)).map(meters).join(" × ");
+ const area = surface(points);
+ return `${area >= 1e5 ? `${(area / 1e6).toFixed(area >= 1e7 ? 0 : area >= 1e6 ? 1 : 2)} km²`
+ : `${Math.round(area).toLocaleString("en-US")} m²`} polygon`;
+}
+
+// Whether two edges cross, which the server refuses.
+function tangled(points) {
+ const side = (a, b, c) => Math.sign((b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]));
+ const edges = points.map((p, i) => [p, points[(i + 1) % points.length]]);
+ return edges.some(([a, b], i) => edges.slice(i + 2, edges.length - (i === 0)).some(([c, d]) =>
+ side(a, b, c) * side(a, b, d) < 0 && side(c, d, a) * side(c, d, b) < 0));
+}
+
function when(iso) {
const date = new Date(iso);
return `${date.toLocaleDateString(undefined, {day: "numeric", month: "short", year: "numeric"})}, ${
@@ -133,8 +166,11 @@ const state = {
captures: [],
job: null,
view: null,
- area: null,
+ area: null, // Polygon of [lng, lat] points.
+ tool: store.get("tool", "rectangle") === "polygon" ? "polygon" : "rectangle",
drawing: false,
+ sketch: null, // Points of a polygon being drawn.
+ cursor: null,
drag: null,
preview: null,
planError: null,
@@ -299,7 +335,7 @@ function visibleLayers() {
const add = (...ids) => ids.forEach(id => on.add(id));
if (state.view === "library") add("outlines", "outlines-fill");
if (state.view === "new" || state.view === "preview") add("area-fill", "area-line");
- if (state.view === "new" && state.area && !state.drawing) add("corners");
+ if (state.view === "new" && (state.sketch?.length || state.area && !state.drawing)) add("corners");
if (state.view === "preview" && state.preview) add("plan-grid", "plan-terrain", "plan-paths", "plan-gaps", "plan-stops");
if (state.view === "capture") {
for (const [group, ids] of Object.entries(GROUPS)) if (state.toggles[group]) add(...ids);
@@ -527,30 +563,39 @@ async function syncSolid() {
// Area drawing
+const HINTS = {rectangle: "Drag to draw the capture area, or press Esc to pan instead",
+ polygon: "Click to add points, then click the first one or double-click to finish"};
+const clampLngLat = ([lng, lat]) => [Math.max(-180, Math.min(180, lng)), Math.max(-85.05, Math.min(85.05, lat))];
+
function setArea(area) {
state.area = area;
- setData("area", area ? collection([polygon(area)]) : EMPTY);
- setData("corners", area ? collection(ring(area).slice(0, 4).map(c => point(c))) : EMPTY);
+ showArea();
renderArea();
}
-function areaFrom(a, b) {
- const clampLat = value => Math.max(-85.05, Math.min(85.05, value));
- const clampLon = value => Math.max(-180, Math.min(180, value));
- return [clampLat(Math.min(a.lat, b.lat)), clampLon(Math.min(a.lng, b.lng)),
- clampLat(Math.max(a.lat, b.lat)), clampLon(Math.max(a.lng, b.lng))];
+// The area, or the polygon being drawn with a line to the pointer.
+function showArea() {
+ const {sketch, area} = state;
+ if (sketch?.length) {
+ setData("area", collection([line(state.cursor ? [...sketch, state.cursor] : sketch)]));
+ setData("corners", collection(sketch.map(c => point(c))));
+ } else {
+ setData("area", area ? collection([polygon(area)]) : EMPTY);
+ setData("corners", area ? collection(area.map(c => point(c))) : EMPTY);
+ }
+}
+
+function rectangleFrom(a, b) {
+ const [lng1, lat1] = clampLngLat(a), [lng2, lat2] = clampLngLat(b);
+ return corners([Math.min(lat1, lat2), Math.min(lng1, lng2), Math.max(lat1, lat2), Math.max(lng1, lng2)]);
}
-function cornerAt(pixel) {
- if (!state.area) return null;
- const corners = ring(state.area).slice(0, 4);
- const index = corners.findIndex(corner => {
- const p = map.project(corner);
+// Index of the point under the pointer, or -1.
+function pointAt(pixel, points = state.area ?? []) {
+ return points.findIndex(c => {
+ const p = map.project(c);
return Math.abs(p.x - pixel.x) <= 9 && Math.abs(p.y - pixel.y) <= 9;
});
- if (index < 0) return null;
- const [lng, lat] = corners[(index + 2) % 4];
- return {index, anchor: {lng, lat}};
}
function hint(text) {
@@ -560,39 +605,85 @@ function hint(text) {
function setDrawing(on) {
state.drawing = on;
- hint(on ? "Drag to draw the capture area, or press Esc to pan instead" : "");
+ state.sketch = on && state.tool === "polygon" ? [] : null;
+ state.cursor = null;
+ hint(on ? HINTS[state.tool] : "");
map.getCanvas().style.cursor = on ? "crosshair" : "";
+ showArea();
renderArea();
syncMap();
}
+function setTool(tool) {
+ state.tool = tool;
+ store.set("tool", tool);
+ setDrawing(true);
+}
+
+// Polygon points come from clicks, so the map still pans while drawing one.
+function addPoint(event) {
+ const sketch = state.sketch;
+ if (state.view !== "new" || !sketch) return;
+ if (sketch.length >= 3 && pointAt(event.point, sketch) === 0) return finishSketch();
+ if (pointAt(event.point, sketch.slice(-1)) === 0) return; // The second click of a double-click.
+ sketch.push(clampLngLat(event.lngLat.toArray()));
+ showArea();
+ syncMap();
+}
+
+function removePoint() {
+ state.sketch.pop();
+ showArea();
+ syncMap();
+}
+
+function finishSketch() {
+ const sketch = state.sketch;
+ if (!sketch || sketch.length < 3) return;
+ state.sketch = null;
+ setArea(sketch);
+ setDrawing(false);
+ estimate();
+}
+
function startDrag(event) {
- if (state.view !== "new" || event.originalEvent.button !== 0) return;
- const corner = state.drawing ? null : cornerAt(event.point);
- if (!state.drawing && !corner) return;
+ if (state.view !== "new" || event.originalEvent.button !== 0 || state.sketch) return;
+ const index = state.drawing ? -1 : pointAt(event.point);
+ if (!state.drawing && index < 0) return;
event.preventDefault(); // Keeps the map from panning.
- state.drag = {anchor: corner ? corner.anchor : event.lngLat, previous: state.area};
+ // Rectangles are drawn and resized from the opposite corner; polygon points move on their own.
+ const area = state.area;
+ const anchor = index < 0 ? event.lngLat.toArray() : rectangle(area) ? area[(index + 2) % 4] : null;
+ state.drag = {anchor, index, previous: area};
}
function moveDrag(event) {
- if (state.drag) return setArea(areaFrom(state.drag.anchor, event.lngLat));
- if (state.view === "new" && !state.drawing) {
- const corner = cornerAt(event.point);
- map.getCanvas().style.cursor = corner ? (corner.index % 2 ? "nwse-resize" : "nesw-resize") : "";
+ const drag = state.drag, at = event.lngLat.toArray();
+ if (drag?.anchor) return setArea(rectangleFrom(drag.anchor, at));
+ if (drag) return setArea(drag.previous.with(drag.index, clampLngLat(at)));
+ if (state.view !== "new") return;
+ if (state.sketch) {
+ state.cursor = at;
+ const closing = state.sketch.length >= 3 && pointAt(event.point, state.sketch) === 0;
+ map.getCanvas().style.cursor = closing ? "pointer" : "crosshair";
+ showArea();
+ } else if (!state.drawing) {
+ const index = pointAt(event.point);
+ map.getCanvas().style.cursor = index < 0 ? "" : !rectangle(state.area) ? "move"
+ : index % 2 ? "nwse-resize" : "nesw-resize";
}
}
function endDrag() {
- if (!state.drag) return;
- const {previous} = state.drag;
+ const drag = state.drag;
+ if (!drag) return;
state.drag = null;
- const [width, height] = state.area ? size(state.area) : [0, 0];
- if (width < 5 || height < 5) {
- setArea(previous); // A click, not a drag.
- return;
+ if (drag.anchor) {
+ const [width, height] = state.area ? size(extent(state.area)) : [0, 0];
+ if (width < 5 || height < 5) return setArea(drag.previous); // A click, not a drag.
+ setDrawing(false);
}
- setDrawing(false);
- estimate();
+ if (state.area !== drag.previous) estimate();
}
// Settings
@@ -603,6 +694,7 @@ function fillForm(options) {
if (!name) continue;
if (name === "include") input.checked = options.include.includes(input.value);
else if (name === "camera") input.checked = (input.value === "sphere") === options.full_sphere;
+ else if (name === "shape") input.checked = input.value === state.tool;
else if (input.type === "radio") input.checked = String(options[name]) === input.value;
else if (name in CHOICES) input.value = CHOICES[name].findLastIndex(value => value <= options[name]);
else if (name in options) input.value = options[name];
@@ -643,9 +735,10 @@ function placeTag(range) {
}
function renderPreviewButton() {
- const button = $("#preview-button"), sources = readForm().include.length;
- button.disabled = !state.area || !sources;
- button.textContent = !sources ? "Select one or more sources first" : !state.area ? "Draw an area first" : "Preview";
+ const button = $("#preview-button"), sources = readForm().include.length, crossed = state.area && tangled(state.area);
+ button.disabled = !state.area || !sources || crossed;
+ button.textContent = !sources ? "Select one or more sources first" : !state.area ? "Draw an area first"
+ : crossed ? "Move points so edges do not cross" : "Preview";
}
let estimateTimer = null;
@@ -655,9 +748,9 @@ let estimateCount = 0;
async function fetchEstimate() {
const {include, satellite_zoom, terrain_zoom, mesh_level, streetview_workers, satellite_workers, terrain_workers,
mesh_workers, delay} = readForm();
- if (!state.area || include.every(mode => mode === "osm")) return {};
+ if (!state.area || tangled(state.area) || include.every(mode => mode === "osm")) return {};
try {
- return await api("/api/estimate", {bounds: state.area, options: {include, satellite_zoom, terrain_zoom, mesh_level,
+ return await api("/api/estimate", {polygon: state.area, options: {include, satellite_zoom, terrain_zoom, mesh_level,
streetview_workers, satellite_workers, terrain_workers, mesh_workers, delay}});
} catch {
return {}; // Invalid values are reported by Preview.
@@ -690,8 +783,9 @@ function estimate() {
function renderArea() {
const area = state.area;
$("#redraw").innerHTML = state.drawing ? "Cancel" : `${icon("pencil")}${area ? "Redraw" : "Draw area"}`;
+ const drawing = state.tool === "polygon" ? "Click on the map to add points." : "Drag on the map to draw it.";
$("#area").innerHTML = area ? `<strong>${esc(dimensions(area))}</strong>`
- : `<span class="muted">${state.drawing ? "Drag on the map to draw it." : "No area yet."}</span>`;
+ : `<span class="muted">${state.drawing ? drawing : "No area yet."}</span>`;
renderPreviewButton();
}
@@ -724,7 +818,7 @@ async function loadCaptures() {
$("#captures").innerHTML = `<li class="empty error">${esc(error.message)}</li>`;
return;
}
- setData("outlines", collection(state.captures.filter(c => c.bounds).map(c => polygon(c.bounds, {id: c.id}))));
+ setData("outlines", collection(state.captures.filter(c => c.polygon).map(c => polygon(c.polygon, {id: c.id}))));
renderLibrary();
}
@@ -736,7 +830,7 @@ function renderLibrary() {
return;
}
list.innerHTML = state.captures.map(c => {
- if (!c.bounds) {
+ if (!c.polygon) {
return `<li><div class="capture"><span class="when">${esc(c.id)}</span><span class="state failed">${STATES[c.state]}</span>
<span class="detail">${esc(c.error)}</span></div></li>`;
}
@@ -745,7 +839,7 @@ function renderLibrary() {
const status = live ? "running" : c.state;
return `<li><button type="button" class="capture" data-id="${esc(c.id)}">
<span class="when">${esc(when(c.started_at))}</span><span class="state ${status}">${STATES[status]}</span>
- <span class="detail">${esc(c.options.include.map(s => NAMES[s]).join(", "))} (${esc(dimensions(c.bounds))})</span>${bar}
+ <span class="detail">${esc(c.options.include.map(s => NAMES[s]).join(", "))} (${esc(dimensions(c.polygon))})</span>${bar}
</button></li>`;
}).join("");
}
@@ -807,7 +901,7 @@ async function previewPlan(event) {
Object.assign(state, {preview: null, planError: null});
setData("plan", EMPTY);
try {
- state.job = await api("/api/plan", {bounds: state.area, options: readForm()});
+ state.job = await api("/api/plan", {polygon: state.area, options: readForm()});
state.watched = state.job.id;
} catch (failure) {
error.textContent = failure.message;
@@ -893,7 +987,7 @@ async function openCapture(id) {
const first = state.capture?.id !== id;
state.capture = detail;
if (first) {
- setData("bounds", collection([polygon(detail.bounds)]));
+ setData("bounds", collection([polygon(detail.polygon)]));
setData("plan", absolute(capturePath(id, "/plan")));
map.fitBounds(lngLatBounds(detail.bounds), {padding: 60, maxZoom: 18, duration: 600});
}
@@ -955,7 +1049,7 @@ function renderCapture() {
const live = running("capture", c.id);
const status = live ? "running" : c.state;
$("#capture-title").textContent = when(c.started_at);
- $("#capture-meta").textContent = `${STATES[status]}: ${dimensions(c.bounds)}`;
+ $("#capture-meta").textContent = `${STATES[status]}: ${dimensions(c.polygon)}`;
$("#progress").hidden = !live;
if (live) {
const downloadPhase = DOWNLOADS.has(job.phase) && !job.stopping;
@@ -1051,8 +1145,7 @@ function showPhoto(index) {
$("#viewer").hidden = false;
const at = photo.geometry.coordinates;
const features = [point(at)];
- if (p.heading != null) features.unshift({type: "Feature", properties: {},
- geometry: {type: "LineString", coordinates: [at, destination(at, p.heading, 18)]}});
+ if (p.heading != null) features.unshift(line([at, destination(at, p.heading, 18)]));
setData("selected", collection(features));
if (!map.getBounds().contains(at)) map.easeTo({center: at});
}
@@ -1172,6 +1265,7 @@ function bind() {
estimate();
});
$("#settings").addEventListener("submit", previewPlan);
+ $("#settings").addEventListener("change", event => { if (event.target.name === "shape") setTool(event.target.value); });
$("#redraw").addEventListener("click", () => setDrawing(!state.drawing));
$("#preview-back").addEventListener("click", async () => {
@@ -1275,6 +1369,10 @@ function bind() {
if (state.drawing && !state.drag) setDrawing(false);
else if (!results.hidden) results.hidden = true;
else if (!$("#viewer").hidden) closeViewer();
+ } else if (state.sketch && event.key === "Enter") {
+ finishSketch();
+ } else if (state.sketch?.length && event.key === "Backspace") {
+ removePoint();
} else if (!$("#viewer").hidden && (event.key === "ArrowLeft" || event.key === "ArrowRight")) {
showPhoto(state.selected + (event.key === "ArrowLeft" ? -1 : 1));
}
@@ -1289,6 +1387,12 @@ function bindMap() {
map.on("mousedown", startOrbit);
map.on("mousemove", moveDrag);
map.on("mouseup", endDrag);
+ map.on("click", addPoint);
+ map.on("dblclick", event => {
+ if (!state.sketch) return;
+ event.preventDefault(); // Finishes the polygon instead of zooming.
+ finishSketch();
+ });
map.on("click", "photos", event => {
const sequence = event.features[0].properties.sequence;
showPhoto(state.photos.findIndex(photo => photo.properties.sequence === sequence));
@@ -1352,12 +1456,8 @@ async function init() {
if (state.job?.state === "running") poll();
if (!store.get("view", null) && state.view === "library") {
await loadCaptures();
- const bounds = state.captures.filter(c => c.bounds).map(c => c.bounds);
- if (bounds.length) {
- map.fitBounds(lngLatBounds([Math.min(...bounds.map(b => b[0])), Math.min(...bounds.map(b => b[1])),
- Math.max(...bounds.map(b => b[2])), Math.max(...bounds.map(b => b[3]))]),
- {padding: 80, maxZoom: 16, duration: 0});
- }
+ const points = state.captures.filter(c => c.polygon).flatMap(c => c.polygon);
+ if (points.length) map.fitBounds(lngLatBounds(extent(points)), {padding: 80, maxZoom: 16, duration: 0});
}
}
diff --git a/src/web/index.html b/src/web/index.html
index 43aefe6..2f3991f 100644
--- a/src/web/index.html
+++ b/src/web/index.html
@@ -66,6 +66,12 @@
<section class="block">
<h2 class="label">Area</h2>
+ <div class="field shape"><span>Shape</span>
+ <span class="segmented">
+ <label><input type="radio" name="shape" value="rectangle"><span>Rectangle</span></label>
+ <label><input type="radio" name="shape" value="polygon"><span>Polygon</span></label>
+ </span>
+ </div>
<div class="area">
<p id="area"></p>
<button id="redraw" type="button" class="small">Redraw</button>
diff --git a/src/web/style.css b/src/web/style.css
index ef592d8..769ddb5 100644
--- a/src/web/style.css
+++ b/src/web/style.css
@@ -290,6 +290,7 @@ input[type=range]::-moz-range-thumb { width: 24px; height: 18px; border: 0; bord
/* Area and sources */
+.shape { margin-bottom: 10px; }
.area { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
#area strong { font-weight: 600; }
diff --git a/tests/test_capture.py b/tests/test_capture.py
index 7ec91db..4789809 100644
--- a/tests/test_capture.py
+++ b/tests/test_capture.py
@@ -12,7 +12,7 @@ from PIL import Image
from src import capture, quick, streetview
from src.common import MissingImagery
-from src.geo import coordinate
+from src.geo import coordinate, corners
def image_bytes(size=(256, 256)):
@@ -54,7 +54,7 @@ class CaptureTests(unittest.TestCase):
def satellite_run(self, columns=1):
north, west = coordinate(1024, 1024, 4)
south, east = coordinate(1024 + columns * 256, 1280, 4)
- return capture.plan(None, (south, west, north, east),
+ return capture.plan(None, corners((south, west, north, east)),
{"include": ["satellite"], "satellite_zoom": 4}, self.progress)
def test_resume_keeps_saved_tiles_and_transparent_missing_imagery(self):
@@ -88,6 +88,25 @@ class CaptureTests(unittest.TestCase):
self.assertEqual(image.getpixel((300, 100)), (0, 0, 0, 0))
self.assertEqual(image.getpixel((600, 100))[3], 255)
+ def test_polygon_downloads_only_tiles_it_meets_and_clears_pixels_outside(self):
+ north, west = coordinate(1024, 1024, 4)
+ south, east = coordinate(2048, 2048, 4)
+ # The lower left half of a 4 × 4 tile grid, cut along its diagonal.
+ run = capture.plan(None, [(south, west), (north, west), (south, east)],
+ {"include": ["satellite"], "satellite_zoom": 4}, self.progress)
+ client = Mock()
+ client.get.return_value = image_bytes()
+ capture.download(run, self.folder, client, self.progress)
+ queries = [parse_qs(call.args[0]) for call in client.get.call_args_list]
+ requested = [(int(query["x"][0]), int(query["y"][0])) for query in queries]
+ self.assertEqual(requested, [(4 + column, 4 + row) for row in range(4) for column in range(row + 1)])
+ self.assertEqual(capture.load(self.folder)["state"], "complete")
+ with Image.open(self.folder / "satellite.png") as image:
+ self.assertEqual(image.size, (1024, 1024))
+ # Inside, outside in a skipped tile, and on both sides of the diagonal in a saved tile.
+ self.assertEqual([image.getpixel(p)[3] for p in [(10, 1000), (1000, 10), (560, 700), (700, 560)]],
+ [255, 0, 255, 0])
+
def test_failed_export_can_resume_without_redownloading(self):
run = self.satellite_run()
client = Mock()
@@ -111,7 +130,7 @@ class CaptureTests(unittest.TestCase):
def street_run(self):
return dict(
format="aleph-python", version=capture.FORMAT_VERSION,
- bounds=[44.99, 8.99, 45.01, 9.01],
+ polygon=[list(point) for point in corners((44.99, 8.99, 45.01, 9.01))],
options=capture.settings({"include": ["streetview"]}),
state="planned", stages=[dict(
mode="streetview", full_sphere=False, results=[],
@@ -230,6 +249,17 @@ class CaptureTests(unittest.TestCase):
self.assertEqual(image.size, (6, 3))
+class PolygonTests(unittest.TestCase):
+ def test_roads_split_where_they_leave_a_concave_polygon(self):
+ # A U open to the north: a road along latitude 2 crosses both arms and the gap between them.
+ polygon = [(0, 0), (0, 3), (3, 3), (3, 2), (1, 2), (1, 1), (3, 1), (3, 0)]
+ ways = [dict(id=1, tags=dict(highway="residential"), nodes=[1, 2, 3], points=[(2, -1), (2, 0.5), (2, 4)])]
+ parts = streetview.roads(ways, polygon, "roads")
+ self.assertEqual([[tuple(round(v, 9) for v in point) for point in part["points"]] for part in parts],
+ [[(2, 0), (2, 0.5), (2, 1)], [(2, 2), (2, 3)]])
+ self.assertEqual([part["id"] for part in parts], ["way-1-1", "way-1-2"])
+
+
class GoogleResponseTests(unittest.TestCase):
def test_sphere_requires_valid_dimensions_and_orientation(self):
with self.assertRaisesRegex(ValueError, "Unreadable Google panorama metadata"):
diff --git a/tests/test_imagery.py b/tests/test_imagery.py
index 8b6895e..61c39d8 100644
--- a/tests/test_imagery.py
+++ b/tests/test_imagery.py
@@ -25,19 +25,30 @@ class SatelliteTests(unittest.TestCase):
with self.subTest(zoom=zoom, x=x, y=y):
north, west = geo.coordinate(x * 256, y * 256, zoom)
south, east = geo.coordinate((x + 1) * 256, (y + 1) * 256, zoom)
- self.assertEqual(geo.grid((south, west, north, east), zoom), dict(
+ self.assertEqual(geo.grid(geo.corners((south, west, north, east)), zoom), dict(
zoom=zoom, left=x * 256, top=y * 256, width=256, height=256,
- x0=x, y0=y, columns=1, rows=1))
+ x0=x, y0=y, columns=1, rows=1, spans=[[0, 0, 1, 0]], count=1))
north, west = geo.coordinate(1024 - 0.01, 1280 - 0.01, 21)
south, east = geo.coordinate(1280 + 0.01, 1536 + 0.01, 21)
- self.assertEqual(geo.grid((south, west, north, east), 21), dict(
+ self.assertEqual(geo.grid(geo.corners((south, west, north, east)), 21), dict(
zoom=21, left=1023, top=1279, width=258, height=258,
- x0=3, y0=4, columns=3, rows=3))
+ x0=3, y0=4, columns=3, rows=3, spans=[[0, 0, 3, 0], [1, 0, 3, 3], [2, 0, 3, 6]], count=9))
+
+ def test_saved_tiles_follow_spans_with_gaps(self):
+ # Row 0 holds columns 0–2, row 1 none, and row 2 columns 0 and 2: tiles 0 to 4 in download order.
+ grid = dict(zoom=2, x0=0, y0=0, columns=3, rows=3, spans=[[0, 0, 3, 0], [2, 0, 1, 3], [2, 2, 3, 4]], count=5)
+ for count, z, x, y, expected in [(5, 2, 2, 2, 5), (4, 2, 2, 2, 0), (5, 2, 1, 2, 0), (5, 2, 1, 1, 0),
+ (5, 1, 0, 0, 2), (5, 1, 0, 1, 4), (3, 1, 0, 1, 0), (5, 1, 1, 1, 5),
+ (4, 0, 0, 0, 4), (2, 0, 0, 0, 2), (0, 0, 0, 0, 0)]:
+ with self.subTest(count=count, tile=(z, x, y)):
+ self.assertEqual(layers.saved(grid, count, z, x, y), expected)
def test_partial_mosaic_crops_pixels_and_writes_georeferenced_overview(self):
grid = dict(zoom=4, left=1027, top=1287, width=301, height=259,
- x0=4, y0=5, columns=2, rows=2)
+ x0=4, y0=5, columns=2, rows=2, spans=[[0, 0, 2, 0], [1, 0, 2, 2]], count=4)
+ (north, west), (south, east) = geo.coordinate(1027, 1287, 4), geo.coordinate(1328, 1546, 4)
+ area = geo.corners((south, west, north, east))
with TemporaryDirectory() as directory, Image.new("RGBA", (512, 512)) as canvas:
folder = Path(directory)
results = []
@@ -53,7 +64,7 @@ class SatelliteTests(unittest.TestCase):
# Force BigTIFF using a small file; never allocate gigabytes.
for limit, magic in ((2**32, b"II*\0"), (1, b"II+\0")):
with self.subTest(format=magic), patch("src.satellite.TIFF_LIMIT", limit):
- satellite.merge(stage, folder, quiet)
+ satellite.merge(stage, folder, quiet, area)
self.assertEqual((folder / "satellite.tif").read_bytes()[:4], magic)
with Image.open(folder / "satellite.png") as png:
self.assertEqual(png.size, expected.size)
diff --git a/tests/test_mesh.py b/tests/test_mesh.py
index 2266866..73d9618 100644
--- a/tests/test_mesh.py
+++ b/tests/test_mesh.py
@@ -11,6 +11,7 @@ from unittest.mock import Mock, patch
from src import capture, mesh
from src.common import MissingImagery, write_bytes
+from src.geo import corners
RADIUS = 6371010.0
AREA = (-1e-4, -1e-4, 1e-4, 1e-4) # About 22 m square around latitude 0, longitude 0.
@@ -95,7 +96,7 @@ class MeshTests(unittest.TestCase):
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())
+ mesh.export(self.folder / "mesh.glb", self.folder, stage, corners(AREA), Mock())
gltf, view = read_glb(self.folder / "mesh.glb")
self.assertEqual(gltf["asset"]["extras"]["base"], 5)
@@ -114,7 +115,8 @@ class MeshTests(unittest.TestCase):
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)]
- run = dict(format="aleph-python", version=capture.FORMAT_VERSION, bounds=list(AREA),
+ run = dict(format="aleph-python", version=capture.FORMAT_VERSION,
+ polygon=[list(point) for point in corners(AREA)],
options=capture.settings({"include": ["mesh"]}), started_at="", state="planned",
stages=[dict(mode="mesh", level=21, radius=RADIUS, nodes=nodes, results=[])])
diff --git a/tests/test_pbf.py b/tests/test_pbf.py
index 5387c64..67d8663 100644
--- a/tests/test_pbf.py
+++ b/tests/test_pbf.py
@@ -8,6 +8,7 @@ from pathlib import Path
from tempfile import TemporaryDirectory
from src import pbf
+from src.geo import corners
def uint(value):
@@ -106,7 +107,7 @@ class PBFTests(unittest.TestCase):
with TemporaryDirectory() as directory:
source, target = Path(directory) / "region.pbf", Path(directory) / "map.osm"
source.write_bytes(snapshot)
- pbf.PBF(source).export((-0.1, -0.1, 0.1, 0.1), target)
+ pbf.PBF(source).export(corners((-0.1, -0.1, 0.1, 0.1)), target)
root = ET.parse(target).getroot()
self.assertEqual([int(n.get("id")) for n in root.findall("node")], list(range(1, 8)))
self.assertEqual([w.get("id") for w in root.findall("way")], ["10", "20", "30"])