39 lines
1.8 KiB
Python
39 lines
1.8 KiB
Python
"""MapEngine: facade for map retrieval operations.
|
|
|
|
Minimal implementation: constructs a MapTileManager and exposes
|
|
`get_image_for_area` and `get_image_for_point` helper methods.
|
|
"""
|
|
from typing import Optional, Tuple
|
|
from map_manager.tile_manager import MapTileManager
|
|
from map_manager.services import get_map_service_instance
|
|
|
|
class MapEngine:
|
|
def __init__(self, service_name: str = 'osm', cache_dir: Optional[str] = None, enable_online: bool = True):
|
|
service = get_map_service_instance(service_name)
|
|
if service is None:
|
|
raise ValueError(f"Unknown map service: {service_name}")
|
|
self.tile_manager = MapTileManager(service, cache_root_directory=cache_dir, enable_online_tile_fetching=enable_online)
|
|
|
|
def get_image_for_area(self, bbox: Tuple[float, float, float, float], zoom: int) -> Optional[object]:
|
|
"""Return a stitched PIL image covering `bbox` at `zoom` level."""
|
|
from map_manager.utils import get_tile_ranges_for_bbox
|
|
tile_ranges = get_tile_ranges_for_bbox(bbox, zoom)
|
|
if not tile_ranges:
|
|
return None
|
|
return self.tile_manager.stitch_map_image(zoom, tile_ranges[0], tile_ranges[1])
|
|
|
|
def get_image_for_point(self, lat: float, lon: float, zoom: int, tiles_radius: int = 1) -> Optional[object]:
|
|
"""Return a stitched image centered on (lat,lon) using a small tile window.
|
|
|
|
tiles_radius=1 returns a 3x3 tile image.
|
|
"""
|
|
try:
|
|
import mercantile
|
|
except Exception:
|
|
return None
|
|
center_tile = mercantile.tile(lon, lat, zoom)
|
|
x, y = center_tile.x, center_tile.y
|
|
range_x = (x - tiles_radius, x + tiles_radius)
|
|
range_y = (y - tiles_radius, y + tiles_radius)
|
|
return self.tile_manager.stitch_map_image(zoom, range_x, range_y)
|