Skip to content

armgit / Aleph

Public Imported from GitHub
0

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

Share Street View capture helpers

Belluxx64b940298b23
diff --git a/src/capture.py b/src/capture.py
index 01d00af..ef61c00 100644
--- a/src/capture.py
+++ b/src/capture.py
@@ -12,7 +12,7 @@ from PIL import Image
from . import satellite, streetview, terrain
from .common import MissingImagery, contained, now, number, open_image, positive, write_bytes, write_json
-from .geo import bounds, collection, coordinate, distance, feature, grid, pixel, tile_ring, tiles
+from .geo import bounds, collection, coordinate, feature, grid, pixel, tile_ring, tiles
SOURCES = ("streetview", "satellite", "osm")
FORMAT_VERSION = 4
@@ -171,25 +171,19 @@ def capture_streetview(stage, run, folder, client, progress):
try:
# Both sides of a stop share one panorama.
if metadata is None or metadata["pano_id"] != sample["pano_id"]:
- fetched = streetview.fetch_metadata(client, sample["pano_id"], full_sphere=sphere)
- if (fetched["pano_id"] != sample["pano_id"]
- or distance((sample["lat"], sample["lon"]), (fetched["lat"], fetched["lon"])) > 0.1):
- raise ValueError("The planned panorama identity or position changed. Plan a new run.")
- metadata = fetched
+ metadata = streetview.fetch_metadata(client, sample, full_sphere=sphere)
photo.update(metadata)
if sphere:
del photo["heading"] # A sphere has no single perspective camera heading.
photo["filename"] = f"streetview/photos/{index + 1:06d}_sphere.{options['streetview_format']}"
- photo.update(streetview.save_sphere(client, metadata, options["sphere_zoom"], folder / photo["filename"]),
- source_url=streetview.metadata_url(sample["pano_id"]))
+ photo.update(streetview.save_sphere(client, metadata, options["sphere_zoom"], folder / photo["filename"]))
else:
side = "left" if index % 2 == 0 else "right"
- photo.update(side=side, heading=(sample["heading"] + (-90 if side == "left" else 90)) % 360,
+ photo.update(side=side, heading=(sample["heading"] + streetview.VIEW_OFFSETS[side]) % 360,
pitch=0, fov=options["fov"])
- photo["source_url"] = streetview.image_url(photo["pano_id"], photo["heading"], photo["fov"])
- data = client.get(photo["source_url"], missing_ok=True)
photo["filename"] = "streetview/photos/" + streetview.photo_name(photo, options["streetview_format"])
- photo.update(streetview.save_photo(data, folder / photo["filename"]))
+ photo.update(streetview.save_photo(client, photo["pano_id"], photo["heading"], photo["fov"], 0,
+ folder / photo["filename"]))
photo.update(captured_at=now(), status="saved", streetview_url=streetview.maps_url(photo))
except MissingImagery as error:
photo.update(status="skipped", reason=str(error))
diff --git a/src/quick.py b/src/quick.py
index fb82763..e9595a6 100644
--- a/src/quick.py
+++ b/src/quick.py
@@ -7,9 +7,6 @@ from . import capture, places, routes, streetview
from .common import MissingImagery, RequestError, now, number, positive, write_json
from .geo import around, bearing, bounds, coordinate, distance, extent, point
-VIEW_OFFSETS = {"forward": 0, "backward": 180, "left": -90, "right": 90}
-
-
def coverage(client, area, progress):
try:
return streetview.coverage(client, area, progress)
@@ -98,30 +95,24 @@ def street_photos(client, output, progress, *, at=None, place=None, pano_id=None
progress("Street View", 0, len(samples))
for index, sample in enumerate(samples, 1):
try:
- metadata = streetview.fetch_metadata(client, sample["pano_id"], full_sphere=full_sphere)
- if metadata["pano_id"] != sample["pano_id"]:
- raise ValueError("The provider returned a different panorama.")
+ metadata = streetview.fetch_metadata(client, sample, full_sphere=full_sphere)
actual = (metadata["lat"], metadata["lon"])
- if "lat" in sample and distance(actual, (sample["lat"], sample["lon"])) > 1:
- raise MissingImagery("Panorama position changed; retry with --refresh.")
if full_sphere:
path = folder / f"{len(result['photos']) + 1:03d}_sphere.{streetview_format}"
photo = {**sample, **metadata}
if "heading" in photo:
photo["road_heading"] = photo.pop("heading")
- photo.update(streetview.save_sphere(client, metadata, sphere_zoom, path), path=str(path),
- source_url=streetview.metadata_url(sample["pano_id"]))
+ photo.update(streetview.save_sphere(client, metadata, sphere_zoom, path), path=str(path))
result["photos"].append(photo)
else:
for direction in directions if street else (None,):
- angle = (sample["heading"] + VIEW_OFFSETS[direction]) % 360 if street else heading
+ angle = (sample["heading"] + streetview.VIEW_OFFSETS[direction]) % 360 if street else heading
if look_at is not None:
angle = bearing(actual, look_at)
- address = streetview.image_url(sample["pano_id"], angle, fov, pitch)
- data = client.get(address, missing_ok=True)
path = folder / f"{len(result['photos']) + 1:03d}.{streetview_format}"
- photo = {**sample, **metadata, **streetview.save_photo(data, path)}
- photo.update(heading=angle, pitch=pitch, fov=fov, path=str(path), source_url=address)
+ photo = {**sample, **metadata,
+ **streetview.save_photo(client, sample["pano_id"], angle, fov, pitch, path)}
+ photo.update(heading=angle, pitch=pitch, fov=fov, path=str(path))
if street:
photo["view"] = direction
if "requested_location" in sample:
diff --git a/src/routes.py b/src/routes.py
index 17c4adb..1e392e6 100644
--- a/src/routes.py
+++ b/src/routes.py
@@ -124,20 +124,16 @@ def match_views(views, route, context, step, targets):
continue
meters, separation = line.project(location)
if separation <= 30:
- candidates.append(dict(view, path_meters=meters, road_distance_m=separation,
+ candidates.append(dict(view, path_meters=meters, road_distance=separation,
heading=line.heading(meters, step)))
- # Coverage can include different panorama IDs at the same physical position.
- unique = []
- for candidate in sorted(candidates, key=lambda item: (item["path_meters"], item["road_distance_m"], item["pano_id"])):
- if not unique or candidate["path_meters"] - unique[-1]["path_meters"] >= 0.1:
- unique.append(candidate)
+ unique = streetview.distinct(candidates)
selected, gaps, used = [], [], set()
for stop, target in enumerate(targets, 1):
eligible = [view for view in unique if view["pano_id"] not in used
and abs(view["path_meters"] - target) <= step / 2 + 0.01]
if eligible:
chosen = min(eligible, key=lambda view: (abs(view["path_meters"] - target),
- view["road_distance_m"], view["pano_id"]))
+ view["road_distance"], view["pano_id"]))
used.add(chosen["pano_id"])
selected.append(dict(chosen, stop=stop, target_meters=target,
requested_location=list(line.at(target))))
diff --git a/src/streetview.py b/src/streetview.py
index d4ca378..d359ae7 100644
--- a/src/streetview.py
+++ b/src/streetview.py
@@ -22,6 +22,7 @@ DEPTHS = {
}
PANO_ID = re.compile(r"[\w-]{10,200}", re.ASCII)
PHOTO_SIZE = (1024, 576)
+VIEW_OFFSETS = {"forward": 0, "backward": 180, "left": -90, "right": 90}
FLOAT32_MAX = float.fromhex("0x1.fffffep+127")
@@ -113,8 +114,13 @@ def metadata_url(pano_id):
pb=f"!1m1!1smaps_sv.tactile!2m2!1sen!2sUS!3m3!1m2!1e2!2s{pano_id}!4m6!1e1!1e2!1e3!1e4!2m1!1e1")
-def fetch_metadata(client, pano_id, *, full_sphere=False):
- return parse_metadata(client.get(metadata_url(pano_id)), full_sphere=full_sphere)
+def fetch_metadata(client, sample, *, full_sphere=False):
+ """Refuse a panorama whose identity or position changed since it was found."""
+ metadata = parse_metadata(client.get(metadata_url(sample["pano_id"])), full_sphere=full_sphere)
+ if metadata["pano_id"] != sample["pano_id"] or (
+ "lat" in sample and distance((sample["lat"], sample["lon"]), (metadata["lat"], metadata["lon"])) > 1):
+ raise ValueError("The panorama identity or position changed. Plan again or rerun with --refresh.")
+ return metadata
def image_url(pano_id, heading, fov, pitch=0):
@@ -132,10 +138,11 @@ def maps_url(photo):
return url("https://www.google.com/maps/@", **params)
-def save_photo(data, path):
- with open_image(data, PHOTO_SIZE) as image, image.convert("RGB") as rgb:
+def save_photo(client, pano_id, heading, fov, pitch, path):
+ address = image_url(pano_id, heading, fov, pitch)
+ with open_image(client.get(address, missing_ok=True), PHOTO_SIZE) as image, image.convert("RGB") as rgb:
save_image(rgb, path, quality=80)
- return dict(width=PHOTO_SIZE[0], height=PHOTO_SIZE[1])
+ return dict(width=PHOTO_SIZE[0], height=PHOTO_SIZE[1], source_url=address)
def save_sphere(client, metadata, zoom, target):
@@ -163,7 +170,7 @@ def save_sphere(client, metadata, zoom, target):
save_image(sphere, target, quality=90)
shutil.rmtree(cache)
return dict(projection="equirectangular", width=width, height=height, sphere_zoom=zoom,
- tile_count=columns * rows)
+ tile_count=columns * rows, source_url=metadata_url(metadata["pano_id"]))
def roads(ways, area, depth):
@@ -247,11 +254,17 @@ def match_road(parts, lines, index, point, spacing):
return dict(nearest, match_method=method)
+def distinct(candidates):
+ """Coverage can include different panorama IDs at the same physical position."""
+ unique = []
+ for candidate in sorted(candidates, key=lambda c: (c["path_meters"], c["road_distance"], c["pano_id"])):
+ if not unique or candidate["path_meters"] - unique[-1]["path_meters"] >= 0.1:
+ unique.append(candidate)
+ return unique
+
+
def select_stops(candidates, spacing):
- positions = []
- for candidate in sorted(candidates, key=lambda c: (c["path_meters"], c["pano_id"])):
- if not positions or candidate["path_meters"] - positions[-1]["path_meters"] >= 0.1:
- positions.append(candidate)
+ positions = distinct(candidates)
index = 0
while index < len(positions):
yield positions[index]