"""Anki flashcard generator for Polish islands. Generates Anki-compatible flashcard decks with maps showing islands highlighted on a Poland map. """ from __future__ import annotations import argparse import hashlib from io import BytesIO import multiprocessing as mp from pathlib import Path import random import sys import tempfile from typing import TYPE_CHECKING import genanki import geopandas as gpd import matplotlib as mpl mpl.use("Agg") # Non-interactive backend for multiprocessing import matplotlib.pyplot as plt sys.path.insert(0, str(Path(__file__).parent.parent)) from geo_data import get_poland_boundary, get_polish_islands if TYPE_CHECKING: from collections.abc import Sequence from matplotlib.figure import Figure ISLAND_COLOR = "#E67E22" # Orange for islands NEIGHBOR_COLOR = "#EAECEE" # Lighter gray for extended view # Padding for zoom (in degrees) ZOOM_PADDING_DEG = 0.2 def _island_extends_beyond( island_gdf: gpd.GeoDataFrame, poland_boundary: gpd.GeoDataFrame, ) -> bool: """Check if island extends beyond Poland's boundaries.""" poland_bounds = poland_boundary.total_bounds # [minx, miny, maxx, maxy] island_bounds = island_gdf.total_bounds # Check if any part of island is outside Poland extends_west = island_bounds[0] < poland_bounds[0] extends_south = island_bounds[1] < poland_bounds[1] extends_east = island_bounds[2] > poland_bounds[2] extends_north = island_bounds[3] > poland_bounds[3] return extends_west or extends_south or extends_east or extends_north def create_island_map( island_gdf: gpd.GeoDataFrame, poland_boundary: gpd.GeoDataFrame, *, zoom: bool, ) -> Figure: """Create a map showing Poland with one island highlighted. Args: island_gdf: GeoDataFrame with the island to highlight. poland_boundary: GeoDataFrame with Poland's boundary. zoom: If True, zoom to island area for better visibility. """ fig, ax = plt.subplots(figsize=(10, 12)) ax.set_aspect("equal") ax.axis("off") fig.patch.set_alpha(0) ax.patch.set_alpha(0) extends_beyond = _island_extends_beyond(island_gdf, poland_boundary) if extends_beyond: # Draw extended background if island goes beyond Poland island_bounds = island_gdf.total_bounds padding = 0.5 ax.fill( [ island_bounds[0] - padding, island_bounds[2] + padding, island_bounds[2] + padding, island_bounds[0] - padding, ], [ island_bounds[1] - padding, island_bounds[1] - padding, island_bounds[3] + padding, island_bounds[3] + padding, ], color=NEIGHBOR_COLOR, zorder=0, ) # Plot Poland as a plain gray shape poland_boundary.plot(ax=ax, color="#D5D8DC", alpha=0.6) poland_boundary.boundary.plot(ax=ax, color="#2C3E50", linewidth=1) # Plot the island with thinner lines island_gdf.plot(ax=ax, color=ISLAND_COLOR, alpha=0.9) island_gdf.boundary.plot(ax=ax, color="#1A1A1A", linewidth=1.5) # Set bounds based on zoom mode and whether island extends beyond if zoom: # Zoom to island area with padding island_bounds = island_gdf.total_bounds ax.set_xlim( island_bounds[0] - ZOOM_PADDING_DEG, island_bounds[2] + ZOOM_PADDING_DEG, ) ax.set_ylim( island_bounds[1] - ZOOM_PADDING_DEG, island_bounds[3] + ZOOM_PADDING_DEG, ) elif extends_beyond: # Include the full island in view island_bounds = island_gdf.total_bounds poland_bounds = poland_boundary.total_bounds ax.set_xlim( min(poland_bounds[0], island_bounds[0] - 0.1), max(poland_bounds[2], island_bounds[2] + 0.1), ) ax.set_ylim( min(poland_bounds[1], island_bounds[1] - 0.1), max(poland_bounds[3], island_bounds[3] + 0.1), ) else: # Normal Poland bounds bounds = poland_boundary.total_bounds ax.set_xlim(bounds[0], bounds[2]) ax.set_ylim(bounds[1], bounds[3]) return fig def generate_island_image_bytes( island_gdf: gpd.GeoDataFrame, poland_boundary: gpd.GeoDataFrame, *, zoom: bool, ) -> bytes: """Generate an island map image as bytes.""" fig = create_island_map(island_gdf, poland_boundary, zoom=zoom) buf = BytesIO() fig.savefig(buf, format="png", bbox_inches="tight", dpi=150) plt.close(fig) buf.seek(0) return buf.read() # Global variables for multiprocessing (set via initializer) _mp_poland_boundary: gpd.GeoDataFrame | None = None _mp_zoom_mode: str = "no-zoom" def _init_worker(poland_geojson: str, zoom_mode: str) -> None: """Initialize worker process with shared data.""" global _mp_poland_boundary, _mp_zoom_mode # noqa: PLW0603 _mp_poland_boundary = gpd.read_file(poland_geojson) _mp_zoom_mode = zoom_mode def _render_single_island(args: tuple[str, str]) -> tuple[str, bytes]: """Render a single island image (worker function). Args: args: Tuple of (island_name, island_geojson_str). Returns: Tuple of (island_name, image_bytes). """ island_name, island_geojson = args island_gdf = gpd.read_file(island_geojson) assert _mp_poland_boundary is not None # noqa: S101 image_data = generate_island_image_bytes( island_gdf, _mp_poland_boundary, zoom=(_mp_zoom_mode == "zoom") ) return island_name, image_data def generate_anki_package( islands: gpd.GeoDataFrame, poland_boundary: gpd.GeoDataFrame, deck_name: str = "Polish Islands", *, zoom: bool = True, ) -> genanki.Package: """Generate Anki package for Polish islands.""" model_id_hash = hashlib.md5(f"polish_islands_{deck_name}".encode()) # noqa: S324 model_id = int(model_id_hash.hexdigest()[:8], 16) card_css = """ .card { font-family: Arial, sans-serif; font-size: 24px; text-align: center; color: #333; background-color: #fff; } .card.night_mode { color: #eee; background-color: #2f2f2f; } .map-container { display: flex; justify-content: center; align-items: center; min-height: 80vh; } .map-container img { max-width: 100%; max-height: 80vh; object-fit: contain; } .answer-text { font-size: 32px; font-weight: bold; margin-top: 20px; color: #2C3E50; } .card.night_mode .answer-text { color: #ECF0F1; } .info-text { font-size: 18px; color: #7F8C8D; margin-top: 10px; } .card.night_mode .info-text { color: #BDC3C7; } """ my_model = genanki.Model( model_id, "Polish Island Model", fields=[ {"name": "IslandMap"}, {"name": "IslandName"}, {"name": "Area"}, ], templates=[ { "name": "Card 1", "qfmt": '