#!/usr/bin/env python3 """ Generate an interactive map of NDE heritage institutions. Uses Folium (Leaflet.js) to create an HTML map with markers. Usage: python scripts/generate_nde_map.py Output: data/nde/exports/nde_map.html """ import json import yaml from pathlib import Path from typing import Dict, List import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) # Paths PROJECT_ROOT = Path(__file__).parent.parent ENTRIES_DIR = PROJECT_ROOT / "data" / "nde" / "enriched" / "entries" EXPORTS_DIR = PROJECT_ROOT / "data" / "nde" / "exports" # Institution type colors TYPE_COLORS = { "M": "#e74c3c", # Museum - red "A": "#3498db", # Archive - blue "L": "#2ecc71", # Library - green "S": "#9b59b6", # Society - purple "O": "#f39c12", # Official - orange "R": "#1abc9c", # Research - teal "D": "#34495e", # Digital - dark gray "F": "#95a5a6", # Features - gray "N": "#e91e63", # NGO - pink "B": "#4caf50", # Botanical - green "E": "#ff9800", # Education - amber "I": "#673ab7", # Intangible - deep purple "C": "#795548", # Corporation - brown "H": "#607d8b", # Holy sites - blue gray "T": "#ff5722", # Taste/smell - deep orange "G": "#00bcd4", # Gallery - cyan } TYPE_NAMES = { "M": "Museum", "A": "Archive", "L": "Library", "S": "Society", "O": "Official", "R": "Research", "D": "Digital", "F": "Features", "N": "NGO", "B": "Botanical", "E": "Education", "I": "Intangible", "C": "Corporation", "H": "Holy sites", "T": "Taste/smell", "G": "Gallery", } def load_entries_with_coordinates() -> List[Dict]: """Load entries that have coordinates.""" entries = [] for yaml_file in sorted(ENTRIES_DIR.glob("*.yaml")): try: with open(yaml_file, "r", encoding="utf-8") as f: entry = yaml.safe_load(f) if entry and entry.get("wikidata_enrichment"): coords = entry["wikidata_enrichment"].get("wikidata_coordinates", {}) if coords.get("latitude") and coords.get("longitude"): entries.append(entry) except Exception as e: pass return entries def generate_html_map(entries: List[Dict]) -> str: """Generate an HTML file with a Leaflet map.""" # Prepare markers data markers = [] for entry in entries: original = entry.get("original_entry", {}) wikidata = entry.get("wikidata_enrichment", {}) coords = wikidata.get("wikidata_coordinates", {}) inst_types = original.get("type", ["M"]) primary_type = inst_types[0] if inst_types else "M" name = original.get("organisatie", "Unknown") city = original.get("plaatsnaam_bezoekadres", "") website = original.get("webadres_organisatie", "") wikidata_id = original.get("wikidata_id", "") description = wikidata.get("wikidata_description_nl") or wikidata.get("wikidata_description_en", "") markers.append({ "lat": coords["latitude"], "lon": coords["longitude"], "name": name, "city": city, "type": primary_type, "type_name": TYPE_NAMES.get(primary_type, "Other"), "color": TYPE_COLORS.get(primary_type, "#999999"), "website": website, "wikidata_id": wikidata_id, "description": description[:200] + "..." if len(description) > 200 else description, }) # Generate HTML html = f''' NDE Heritage Institutions Map

Institution Types

{"".join(f'
{TYPE_NAMES.get(t, t)}
' for t, color in TYPE_COLORS.items())}
''' return html def main(): """Main function.""" logger.info("Loading entries with coordinates...") entries = load_entries_with_coordinates() logger.info(f"Found {len(entries)} entries with coordinates") logger.info("Generating map...") html = generate_html_map(entries) # Save HTML EXPORTS_DIR.mkdir(parents=True, exist_ok=True) map_path = EXPORTS_DIR / "nde_map.html" with open(map_path, "w", encoding="utf-8") as f: f.write(html) logger.info(f"Map saved to: {map_path}") print(f"\nOpen in browser: file://{map_path}") if __name__ == "__main__": main()