python-map-manager/map_manager/visualizer.py
2025-12-02 09:09:22 +01:00

48 lines
1.6 KiB
Python

"""Lightweight MapVisualizer inside package: minimal display helper using OpenCV if available."""
from typing import Optional, Callable
try:
import cv2
CV2_AVAILABLE = True
except Exception:
cv2 = None
CV2_AVAILABLE = False
class MapVisualizer:
def __init__(self, engine):
self.engine = engine
self.callback_on_click: Optional[Callable[[float, float], None]] = None
def set_click_callback(self, callback: Callable[[float, float], None]):
self.callback_on_click = callback
def show_pil_image(self, pil_image):
if pil_image is None:
return
if CV2_AVAILABLE:
import numpy as np
img = pil_image.convert('RGB')
arr = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
cv2.imshow('Map', arr)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
# Fallback: save temp file and open with default image viewer
import tempfile, os
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
pil_image.save(tmp.name)
tmp.close()
if os.name == 'nt':
os.startfile(tmp.name) # type: ignore
else:
try:
import webbrowser
webbrowser.open('file://' + tmp.name)
except Exception:
pass
def show_point(self, lat: float, lon: float, zoom: int = 12, tiles_radius: int = 1):
img = self.engine.get_image_for_point(lat, lon, zoom, tiles_radius=tiles_radius)
self.show_pil_image(img)