#!/usr/bin/env python3 """Anki flashcard generator for Warsaw metro stations. Generates Anki-compatible flashcard decks with maps showing individual Warsaw metro stations highlighted on a city map. """ from __future__ import annotations import argparse import hashlib from io import BytesIO from pathlib import Path import random import sys from typing import TYPE_CHECKING sys.path.insert(0, str(Path(__file__).parent.parent)) import genanki from geo_data import get_warsaw_metro_stations import geopandas as gpd import matplotlib.pyplot as plt if TYPE_CHECKING: from collections.abc import Sequence from matplotlib.figure import Figure # Station marker color STATION_COLOR = "#E74C3C" def load_warsaw_boundary() -> gpd.GeoDataFrame: """Load Warsaw boundary from districts GeoJSON. Returns: GeoDataFrame with Warsaw boundary. """ districts_path = ( Path(__file__).parent.parent / "warsaw_districts" / "warszawa-dzielnice.geojson" ) if districts_path.exists(): warsaw_gdf = gpd.read_file(districts_path) warsaw_boundary = warsaw_gdf[warsaw_gdf["name"] == "Warszawa"] if len(warsaw_boundary) == 0: warsaw_boundary = gpd.GeoDataFrame( geometry=[warsaw_gdf.union_all()], crs=warsaw_gdf.crs ) return warsaw_boundary msg = "Warsaw boundary data not found" raise FileNotFoundError(msg) def create_station_map( station_gdf: gpd.GeoDataFrame, warsaw_boundary: gpd.GeoDataFrame, ) -> Figure: """Create a map showing Warsaw with one metro station highlighted. Args: station_gdf: GeoDataFrame with the station point. warsaw_boundary: GeoDataFrame with Warsaw boundary. Returns: A matplotlib Figure object. """ fig, ax = plt.subplots(figsize=(10, 10)) ax.set_aspect("equal") ax.axis("off") fig.patch.set_alpha(0) ax.patch.set_alpha(0) # Plot Warsaw as a plain gray shape warsaw_boundary.plot(ax=ax, color="#D5D8DC", alpha=0.6) warsaw_boundary.boundary.plot(ax=ax, color="#2C3E50", linewidth=2) # Plot the station as a large dot station_gdf.plot( ax=ax, color=STATION_COLOR, markersize=300, marker="o", alpha=0.9, edgecolor="#1A1A1A", linewidth=2, ) # Set bounds to Warsaw bounds = warsaw_boundary.total_bounds ax.set_xlim(bounds[0], bounds[2]) ax.set_ylim(bounds[1], bounds[3]) return fig def generate_station_image_bytes( station_gdf: gpd.GeoDataFrame, warsaw_boundary: gpd.GeoDataFrame, ) -> bytes: """Generate a station map image as bytes.""" fig = create_station_map(station_gdf, warsaw_boundary) buf = BytesIO() fig.savefig(buf, format="png", bbox_inches="tight", dpi=150) plt.close(fig) buf.seek(0) return buf.read() def generate_anki_package( stations: gpd.GeoDataFrame, warsaw_boundary: gpd.GeoDataFrame, deck_name: str = "Warsaw Metro Stations", ) -> genanki.Package: """Generate Anki package for Warsaw metro stations.""" model_id_hash = hashlib.md5(f"warsaw_metro_{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; } .line-info { font-size: 24px; margin-top: 10px; color: #666; } .card.night_mode .answer-text { color: #ECF0F1; } .card.night_mode .line-info { color: #AAA; } """ my_model = genanki.Model( model_id, "Warsaw Metro Model", fields=[ {"name": "StationMap"}, {"name": "StationName"}, {"name": "Line"}, ], templates=[ { "name": "Card 1", "qfmt": '