Auto-update: Fri Nov 15 13:09:24 PST 2024

This commit is contained in:
sanj 2024-11-15 13:09:24 -08:00
parent f44d2e7122
commit 4c51798a78

View file

@ -9,6 +9,8 @@ import random
from pathlib import Path from pathlib import Path
import traceback import traceback
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import contextily as ctx
import numpy as np
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Union, List from typing import Union, List
import folium import folium
@ -122,6 +124,7 @@ async def get_last_location() -> Optional[Location]:
return None return None
async def generate_and_save_heatmap( async def generate_and_save_heatmap(
start_date: Union[str, int, datetime], start_date: Union[str, int, datetime],
end_date: Optional[Union[str, int, datetime]] = None, end_date: Optional[Union[str, int, datetime]] = None,
@ -136,11 +139,7 @@ Generate a heatmap for the given date range and save it as a PNG file.
:return: The path where the PNG file was saved :return: The path where the PNG file was saved
""" """
try: try:
import matplotlib.pyplot as plt
import contextily as ctx
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
start_date = await dt(start_date) start_date = await dt(start_date)
if end_date: if end_date:
end_date = await dt(end_date) end_date = await dt(end_date)
@ -160,39 +159,33 @@ Generate a heatmap for the given date range and save it as a PNG file.
buffer = max(lat_range, lon_range) * 0.05 buffer = max(lat_range, lon_range) * 0.05
# Enforce minimum zoom # Enforce minimum zoom
MIN_RANGE = 0.05 # roughly 3-4 miles MIN_RANGE = 0.05
lat_range = max(lat_range, MIN_RANGE) lat_range = max(lat_range, MIN_RANGE)
lon_range = max(lon_range, MIN_RANGE) lon_range = max(lon_range, MIN_RANGE)
bounds = [
min(lons) - buffer,
max(lons) + buffer,
min(lats) - buffer,
max(lats) + buffer
]
# Create figure with fixed size # Create figure with fixed size
fig, ax = plt.subplots(figsize=(6.4, 3.6), dpi=100) # 640x360 pixels fig, ax = plt.subplots(figsize=(6.4, 3.6), dpi=100)
# Add dark basemap # Set map extent
ctx.add_basemap( ax.set_xlim(min(lons) - buffer, max(lons) + buffer)
ax, ax.set_ylim(min(lats) - buffer, max(lats) + buffer)
crs='EPSG:4326',
source=ctx.providers.CartoDB.DarkMatter,
zoom='auto',
bbox=bounds
)
# Create heatmap overlay # Create heatmap overlay
heatmap = ax.hexbin( heatmap = ax.hexbin(
lons, lats, lons, lats,
extent=bounds,
gridsize=25, gridsize=25,
cmap='hot', cmap='hot',
alpha=0.6, alpha=0.6,
zorder=2 zorder=2
) )
# Add dark basemap
ctx.add_basemap(
ax,
source=ctx.providers.CartoDB.DarkMatter,
zoom='auto'
)
# Remove axes and margins # Remove axes and margins
ax.set_axis_off() ax.set_axis_off()
plt.subplots_adjust(left=0, right=1, top=1, bottom=0) plt.subplots_adjust(left=0, right=1, top=1, bottom=0)