diff --git a/sijapi/__init__.py b/sijapi/__init__.py index 78d6a77..fa9b3cf 100644 --- a/sijapi/__init__.py +++ b/sijapi/__init__.py @@ -28,6 +28,7 @@ Db = Database.init('sys') # load configuration from config/sys.yaml Dir = DirConfig.init('dirs') # load configuration from config/dirs.yaml # Load module configurations +Gis = Config.init('gis', 'secrets', Dir) Img = Config.init('img', 'secrets', Dir) Llm = Config.init('llm', 'secrets', Dir) News = Config.init('news', 'secrets', Dir) diff --git a/sijapi/config/gis.yaml-example b/sijapi/config/gis.yaml-example index 6e24d18..e1fe69b 100644 --- a/sijapi/config/gis.yaml-example +++ b/sijapi/config/gis.yaml-example @@ -4,6 +4,9 @@ custom_locations: longitude: -123.049396 radius: 2 +map: + max_zoom: 11 + layers: - url: "https://gis.blm.gov/arcgis/rest/services/Cadastral/BLM_Natl_PLSS_CadNSDI/MapServer/1/query" table_name: "public.plss_townships" diff --git a/sijapi/routers/gis.py b/sijapi/routers/gis.py index 44443ce..c52e523 100644 --- a/sijapi/routers/gis.py +++ b/sijapi/routers/gis.py @@ -19,7 +19,7 @@ from playwright.async_api import async_playwright from zoneinfo import ZoneInfo from dateutil.parser import parse as dateutil_parse from typing import Optional, List, Union -from sijapi import Sys, Db, TZ, GEO +from sijapi import Sys, Db, TZ, GEO, Gis from sijapi.classes import Location from sijapi.utilities import haversine, assemble_journal_path from sijapi.serialization import json_dumps @@ -124,7 +124,7 @@ async def get_last_location() -> Optional[Location]: -async def generate_and_save_heatmap( +wasync def generate_and_save_heatmap( start_date: Union[str, int, datetime], end_date: Optional[Union[str, int, datetime]] = None, output_path: Optional[Path] = None @@ -139,6 +139,7 @@ Generate a heatmap for the given date range and save it as a PNG file. """ try: from staticmap import StaticMap, CircleMarker + import math start_date = await dt(start_date) if end_date: @@ -150,10 +151,23 @@ Generate a heatmap for the given date range and save it as a PNG file. if not locations: raise ValueError("No locations found for the given date range") - # Create map with correct URL template + # Calculate bounds + lats = [loc.latitude for loc in locations] + lons = [loc.longitude for loc in locations] + lat_diff = max(lats) - min(lats) + lon_diff = max(lons) - min(lons) + + # Calculate zoom level - lower number = more zoomed out + zoom = min( + Gis.map.max_zoom, + int(math.log2(360 / max(lat_diff, lon_diff))) - 1 + ) + + # Create map with correct URL template and zoom m = StaticMap( 640, 360, - url_template='https://cartodb-basemaps-a.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png' + url_template='https://cartodb-basemaps-a.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png', + zoom=zoom ) # Add markers with heat effect @@ -177,7 +191,6 @@ Generate a heatmap for the given date range and save it as a PNG file. - async def generate_map(start_date: datetime, end_date: datetime, max_points: int): locations = await fetch_locations(start_date, end_date) if not locations: