137 lines
5.3 KiB
Python
137 lines
5.3 KiB
Python
"""Offline map packs: estimate, download, and serve XYZ tiles locally.
|
|
|
|
Tiles are stored under data/offline_maps/{pack_id}/{layer}/{z}/{x}/{y}.png.
|
|
Downloads run in a background thread and update pack status in SQLite.
|
|
A per-pack tile cap (settings.tiles.max_pack_tiles) keeps usage within
|
|
tile-provider policies; see PLAN.md Phase 2 gate notes.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import math
|
|
import shutil
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
|
|
from app.config import settings
|
|
from app.models.map_pack import MapPack
|
|
from app.storage.route_store import Store
|
|
|
|
log = logging.getLogger("trekker.offline_maps")
|
|
|
|
LAYER_URLS = {
|
|
"base": lambda: settings.tiles.base_url,
|
|
"terrain": lambda: settings.tiles.terrain_url,
|
|
}
|
|
|
|
|
|
def _tile_range(bbox: list[float], zoom: int) -> tuple[int, int, int, int]:
|
|
"""(x_min, x_max, y_min, y_max) tile indices covering bbox at zoom."""
|
|
min_lon, min_lat, max_lon, max_lat = bbox
|
|
n = 2**zoom
|
|
|
|
def x_of(lon: float) -> int:
|
|
return min(n - 1, max(0, int((lon + 180.0) / 360.0 * n)))
|
|
|
|
def y_of(lat: float) -> int:
|
|
lat = max(-85.05112878, min(85.05112878, lat))
|
|
lat_r = math.radians(lat)
|
|
return min(n - 1, max(0, int((1.0 - math.asinh(math.tan(lat_r)) / math.pi) / 2.0 * n)))
|
|
|
|
return x_of(min_lon), x_of(max_lon), y_of(max_lat), y_of(min_lat)
|
|
|
|
|
|
def iter_tiles(bbox: list[float], min_zoom: int, max_zoom: int):
|
|
for z in range(min_zoom, max_zoom + 1):
|
|
x0, x1, y0, y1 = _tile_range(bbox, z)
|
|
for x in range(x0, x1 + 1):
|
|
for y in range(y0, y1 + 1):
|
|
yield z, x, y
|
|
|
|
|
|
def estimate_tile_count(bbox: list[float], min_zoom: int, max_zoom: int) -> int:
|
|
total = 0
|
|
for z in range(min_zoom, max_zoom + 1):
|
|
x0, x1, y0, y1 = _tile_range(bbox, z)
|
|
total += (x1 - x0 + 1) * (y1 - y0 + 1)
|
|
return total
|
|
|
|
|
|
class OfflineMapService:
|
|
def __init__(self, store: Store, base_dir: str | None = None):
|
|
self.store = store
|
|
self.base_dir = Path(base_dir or settings.offline_maps_dir)
|
|
|
|
def pack_dir(self, pack_id: str) -> Path:
|
|
return self.base_dir / pack_id
|
|
|
|
def create_pack(self, name: str, bbox: list[float], min_zoom: int,
|
|
max_zoom: int, layers: list[str]) -> MapPack:
|
|
layers = [l for l in layers if l in LAYER_URLS] or ["base"]
|
|
count = estimate_tile_count(bbox, min_zoom, max_zoom) * len(layers)
|
|
if count > settings.tiles.max_pack_tiles:
|
|
raise ValueError(
|
|
f"pack would need {count} tiles, over the cap of "
|
|
f"{settings.tiles.max_pack_tiles}; shrink the area or zoom range"
|
|
)
|
|
pack = MapPack(name=name, bbox=bbox, min_zoom=min_zoom, max_zoom=max_zoom,
|
|
layers=layers, status="queued", tile_count=count)
|
|
pack.path = str(self.pack_dir(pack.id))
|
|
self.store.save_map_pack(pack)
|
|
threading.Thread(target=self._download, args=(pack.id,), daemon=True).start()
|
|
return pack
|
|
|
|
def _download(self, pack_id: str) -> None:
|
|
pack = self.store.get_map_pack(pack_id)
|
|
if pack is None:
|
|
return
|
|
pack.status = "downloading"
|
|
self.store.save_map_pack(pack)
|
|
size = 0
|
|
done = 0
|
|
try:
|
|
with httpx.Client(timeout=30, headers={"User-Agent": settings.tiles.user_agent}) as client:
|
|
for layer in pack.layers:
|
|
url_tpl = LAYER_URLS[layer]()
|
|
for z, x, y in iter_tiles(pack.bbox, pack.min_zoom, pack.max_zoom):
|
|
dest = self.pack_dir(pack.id) / layer / str(z) / str(x) / f"{y}.png"
|
|
if not dest.is_file():
|
|
resp = client.get(url_tpl.format(z=z, x=x, y=y))
|
|
resp.raise_for_status()
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
dest.write_bytes(resp.content)
|
|
time.sleep(0.05) # be polite to public tile servers
|
|
size += dest.stat().st_size
|
|
done += 1
|
|
if done % 50 == 0:
|
|
pack.tiles_done, pack.size_bytes = done, size
|
|
self.store.save_map_pack(pack)
|
|
pack.tiles_done, pack.size_bytes, pack.status = done, size, "ready"
|
|
except Exception as e:
|
|
log.exception("pack %s download failed", pack_id)
|
|
pack.status, pack.error = "failed", str(e)
|
|
self.store.save_map_pack(pack)
|
|
|
|
def tile_path(self, pack_id: str, layer: str, z: int, x: int, y: int) -> Path | None:
|
|
p = self.pack_dir(pack_id) / layer / str(z) / str(x) / f"{y}.png"
|
|
return p if p.is_file() else None
|
|
|
|
def find_tile(self, layer: str, z: int, x: int, y: int) -> Path | None:
|
|
"""Search all ready packs for a tile (offline fallback serving)."""
|
|
for pack in self.store.list_map_packs():
|
|
if pack.status != "ready":
|
|
continue
|
|
p = self.tile_path(pack.id, layer, z, x, y)
|
|
if p:
|
|
return p
|
|
return None
|
|
|
|
def delete_pack(self, pack_id: str) -> bool:
|
|
ok = self.store.delete_map_pack(pack_id)
|
|
shutil.rmtree(self.pack_dir(pack_id), ignore_errors=True)
|
|
return ok
|