From cd0d5d690219486501b85fca475636e9b40f51ab Mon Sep 17 00:00:00 2001 From: AgreeDK <17005023+AgreeDK@users.noreply.github.com> Date: Wed, 28 Oct 2026 21:55:29 +0100 Subject: [PATCH 1/4] Add OSM boundary extraction tool for state/county packs (#968, #788) Builds state/county GeoJSON packs from a Geofabrik extract at the admin_level configured per country in osm_countries.toml (Switzerland first), in the same format as gsak_to_opensak.py. Reads relations only, keeps same-named regions apart, supports official_name, prefix stripping, renames and exclusions per country, and writes a report comparing the packs with existing ones and with names known from caches. Adds pyosmium to the dev dependencies. --- pyproject.toml | 2 + tests/unit-tests/test_osm_extract.py | 195 ++++++++ tools/boundaries/osm_countries.toml | 65 +++ tools/boundaries/osm_extract.py | 659 +++++++++++++++++++++++++++ 4 files changed, 921 insertions(+) create mode 100644 tests/unit-tests/test_osm_extract.py create mode 100644 tools/boundaries/osm_countries.toml create mode 100644 tools/boundaries/osm_extract.py diff --git a/pyproject.toml b/pyproject.toml index 8ac213e1..59e17baf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,6 +52,8 @@ dev = [ "pytest-qt>=4.0,<5", "mypy>=1.8,<2.5", "pillow>=10.0,<14", + # tools/boundaries/osm_extract.py (boundary data from OpenStreetMap, #968) + "osmium>=4.0,<5", ] # Release builds only (build.yml, build-msix.yml): `pip install -e ".[build]"` build = [ diff --git a/tests/unit-tests/test_osm_extract.py b/tests/unit-tests/test_osm_extract.py new file mode 100644 index 00000000..a6290911 --- /dev/null +++ b/tests/unit-tests/test_osm_extract.py @@ -0,0 +1,195 @@ +# tests/unit-tests/test_osm_extract.py — OSM boundary extraction (tools/boundaries/osm_extract.py). + +import csv +import json +from pathlib import Path + +import pytest + +pytest.importorskip("osmium", reason="pyosmium is a dev dependency (pip install -e '.[dev]')") + +from tools.boundaries import osm_extract as ox # noqa: E402 + + +# A tiny country "XX" (lon 0–10, lat 40–50) with two states and four counties, +# plus a state of a neighbouring country that the extract also contains. +# +# state "Ouest/West" (XX-OU) = lon 0–5 "Est" (XX-ES) = lon 5–10 "Nachbar" = lon 10–15 +# county "Biel/Bienne" = 0–5, 40–45 "Doppel" = 0–5, 45–50 +# county "Doppel" = 5–10, 40–45 "See" = 5–10, 45–50 (excluded in the config) +# a county without a name = 10–15 (outside XX anyway) +# a closed way tagged as a state ("Amt") — a mistagged building, never a region +_RECTS = { + # relation id: (min_lon, min_lat, max_lon, max_lat, tags) + 1: (0, 40, 10, 50, {"admin_level": "2", "ISO3166-1:alpha2": "XX", "name": "Xland"}), + 2: (0, 40, 5, 50, {"admin_level": "4", "name": "Ouest/West", "ISO3166-2": "XX-OU"}), + 3: (5, 40, 10, 50, {"admin_level": "4", "name": "Est", "ISO3166-2": "XX-ES"}), + 4: (10, 40, 15, 50, {"admin_level": "4", "name": "Nachbar"}), + 5: (0, 40, 5, 45, {"admin_level": "8", "name": "Biel/Bienne"}), + 6: (0, 45, 5, 50, {"admin_level": "8", "name": "Doppel"}), + 7: (5, 40, 10, 45, {"admin_level": "8", "name": "Doppel"}), + 8: (5, 45, 10, 50, {"admin_level": "8", "name": "See"}), + 9: (10, 40, 15, 50, {"admin_level": "8"}), +} +_WAY_AREA = (1, 41, 2, 42, {"admin_level": "4", "name": "Amt"}) + + +def _write_osm(path: Path) -> None: + nodes: dict[tuple[int, int], int] = {} + node_xml, way_xml, rel_xml = [], [], [] + + def node(lon: int, lat: int) -> int: + if (lon, lat) not in nodes: + nid = len(nodes) + 1 + nodes[(lon, lat)] = nid + node_xml.append(f'') + return nodes[(lon, lat)] + + for rid, (x0, y0, x1, y1, tags) in _RECTS.items(): + ring = [node(x0, y0), node(x1, y0), node(x1, y1), node(x0, y1), node(x0, y0)] + wid = 100 + rid + way_xml.append(f'' + + "".join(f'' for n in ring) + "") + all_tags = {"type": "boundary", "boundary": "administrative", **tags} + rel_xml.append(f'' + f'' + + "".join(f'' for k, v in all_tags.items()) + + "") + x0, y0, x1, y1, tags = _WAY_AREA + ring = [node(x0, y0), node(x1, y0), node(x1, y1), node(x0, y1), node(x0, y0)] + way_xml.append('' + "".join(f'' for n in ring) + + "".join(f'' + for k, v in {"boundary": "administrative", **tags}.items()) + + "") + path.write_text( + '\n\n' + + "\n".join(node_xml + way_xml + rel_xml) + "\n\n", + encoding="utf-8", + ) + + +def _cfg(**overrides: object) -> ox.CountryConfig: + values: dict = {"code": "XX", "iso3": "xxx", "state_level": 4, "county_level": 8, + "state_name_split": "/", "county_name_split": "/", "county_name_join": "-", + "county_exclude": ["See"]} + values.update(overrides) + return ox.CountryConfig(**values) + + +def _names(path: Path) -> list[str]: + return [f["properties"]["name"] for f in json.loads(path.read_text("utf-8"))["features"]] + + +@pytest.fixture() +def osm_file(tmp_path: Path) -> Path: + path = tmp_path / "xx.osm" + _write_osm(path) + return path + + +def test_writes_packs_in_the_pipeline_format(osm_file: Path, tmp_path: Path) -> None: + out = tmp_path / "out" + meta = ox.run(_cfg(), osm_file, out, version=3, state_tolerance=0.0005, county_tolerance=0.0) + + states = out / "states" / "xxx.geojson" + counties = out / "counties" / "xxx_all_xxx.geojson" + assert sorted(_names(states)) == ["Est", "Ouest"] # split; neighbour and closed way left out + assert sorted(_names(counties)) == ["Biel-Bienne", "Doppel (ES)", "Doppel (OU)"] + + feature = json.loads(counties.read_text("utf-8"))["features"][0] + props = feature["properties"] + assert props["parent"] == "xxx" and props["version"] == 3 + assert props["source"] == "osm" and props["licence"] == "ODbL" + assert feature["geometry"]["type"] == "MultiPolygon" + assert len(feature["bbox"]) == 4 + + assert meta["counts"] == {"state": 2, "county": 3} + assert any("1 closed ways" in w for w in meta["warnings"]) + assert any("county_exclude" in w and "See" in w for w in meta["warnings"]) + provenance = json.loads((out / "osm_extract_xxx.json").read_text("utf-8")) + assert provenance["attribution"] == "© OpenStreetMap contributors" + + +def test_duplicate_names_in_the_same_state_get_the_osm_id(osm_file: Path, tmp_path: Path) -> None: + # Without state codes the two "Doppel" can't be told apart by state: OSM ids. + text = osm_file.read_text("utf-8").replace('', "") + osm_file.write_text(text, "utf-8") + out = tmp_path / "out" + ox.run(_cfg(), osm_file, out, version=1, state_tolerance=0, county_tolerance=0) + names = _names(out / "counties" / "xxx_all_xxx.geojson") + assert sorted(names) == ["Biel-Bienne", "Doppel (r6)", "Doppel (r7)"] + + +def test_names_split_join_and_rename() -> None: + cfg = _cfg(names={"Est": "East", "Biel-Bienne": "Biel/Bienne (BE)"}) + assert ox.pick_name({"name": "Valais/Wallis"}, cfg, "state") == "Valais" + assert ox.pick_name({"name": "Biel/Bienne"}, cfg, "county") == "Biel/Bienne (BE)" + assert ox.pick_name({"name": "Est"}, cfg, "state") == "East" + assert ox.pick_name({"name:de": "Genf"}, cfg, "state") is None + assert ox.pick_name({"name:de": "Genf", "name": "Genève"}, + _cfg(name_tags=["name:de", "name"]), "state") == "Genf" + official = _cfg(county_name_tags=["official_name", "name"]) + tags = {"name": "Muri", "official_name": "Muri (AG)"} + assert ox.pick_name(tags, official, "county") == "Muri (AG)" + assert ox.pick_name(tags, official, "state") == "Muri" # states keep name_tags + assert ox.pick_name({"name": "Bissone"}, official, "county") == "Bissone" # fallback + stripped = _cfg(county_strip_prefixes=["Gemeinde "]) + assert ox.pick_name({"name": "Gemeinde Avers"}, stripped, "county") == "Avers" + assert ox.pick_name({"name": "Gemeinde "}, stripped, "county") == "Gemeinde" + assert ox.pick_name({"name": "Gemeinde Avers"}, stripped, "state") == "Gemeinde Avers" + + +def test_comparison_report(osm_file: Path, tmp_path: Path) -> None: + # Reference: "Ouest" named "West", and "Nord" missing; a cache CSV with one + # matching and one different county name. + ref = tmp_path / "ref" + square = {"type": "Polygon", "coordinates": [[[0, 40], [5, 40], [5, 50], [0, 50], [0, 40]]]} + ox.write_pack(ref / "states" / "xxx.geojson", [ + {"type": "Feature", "properties": {"name": "West"}, "geometry": square}]) + ox.write_pack(ref / "counties" / "xxx_all_xxx.geojson", [ + {"type": "Feature", "properties": {"name": "Biel Bienne"}, + "geometry": {"type": "Polygon", "coordinates": [[[0, 40], [6.5, 40], [6.5, 45], [0, 45], [0, 40]]]}}]) + points = tmp_path / "location_gaps.csv" + with points.open("w", newline="", encoding="utf-8") as fh: + w = csv.writer(fh) + w.writerow(["code", "lat", "lon", "country", "level", "known_value", "known_from"]) + w.writerow(["GC1", "42", "2", "Xland", "county", "Biel-Bienne", "cache"]) + w.writerow(["GC2", "48", "2", "Xland", "county", "Doppelheim", "cache"]) + w.writerow(["GC3", "48", "12", "Nachbarland", "county", "Elsewhere", "cache"]) + + out = tmp_path / "out" + meta = ox.run(_cfg(), osm_file, out, version=1, state_tolerance=0, county_tolerance=0, + compare_dir=ref, points_csv=points) + report = Path(meta["report"]).read_text("utf-8") + + assert "- Ouest — same area as the reference's 'West' (100% overlap)" in report + assert "- Est\n" in report # no false hint from the spelling + assert "West → Ouest" in report # a point in "West" lands in "Ouest" + assert "- Doppel (ES)\n" in report # county only in OSM + assert "Spelled differently (1), reference → OSM:** Biel Bienne → Biel-Bienne" in report + assert "Matched: 1;" in report + assert "**The reference is larger (1)**" in report # reaches 1.5° into the next county + assert "| county | cache | 1 | 1 |" in report # GC3 lies outside XX + assert "county: Doppelheim → Doppel (OU) (1)" in report + + +def test_unknown_country_in_config(tmp_path: Path) -> None: + config = tmp_path / "c.toml" + config.write_text('[CH]\niso3 = "che"\nstate_level = 4\n', encoding="utf-8") + assert ox.load_config(config, "ch").county_pack_name == "che_all_che.geojson" + with pytest.raises(SystemExit, match="XX is not in"): + ox.load_config(config, "XX") + + +def test_bundled_config_is_valid() -> None: + cfg = ox.load_config(ox.DEFAULT_CONFIG, "CH") + assert cfg.iso3 == "che" and cfg.state_level == 4 and cfg.county_level == 8 + assert cfg.county_pack_name == "che_all_che.geojson" + + +def test_match_names_keeps_accent_only_differences_apart() -> None: + # Brugg and Brügg are two different Swiss municipalities. + pairs = ox.match_names(["Brugg", "Brügg", "Basel-Stadt"], ["Brügg", "Brugg", "Basel Stadt"]) + assert pairs == {"Brugg": "Brugg", "Brügg": "Brügg", "Basel Stadt": "Basel-Stadt"} + # Ambiguous after normalising: no guess. + assert ox.match_names(["Brugg", "Brügg"], ["BRUGG"]) == {} diff --git a/tools/boundaries/osm_countries.toml b/tools/boundaries/osm_countries.toml new file mode 100644 index 00000000..e46ecee2 --- /dev/null +++ b/tools/boundaries/osm_countries.toml @@ -0,0 +1,65 @@ +# tools/boundaries/osm_countries.toml — countries built from OpenStreetMap by osm_extract.py. +# +# One table per country, keyed by its ISO 3166-1 alpha-2 code (the value of the +# country relation's ISO3166-1 / ISO3166-1:alpha2 tag in OSM). +# +# iso3 OpenSAK's country code: pack file names and the `parent` property +# (states/.geojson, counties/_.geojson) +# geofabrik path of the country extract on download.geofabrik.de, without +# "-latest.osm.pbf" (see https://download.geofabrik.de) +# state_level OSM admin_level used for OpenSAK's State +# county_level OSM admin_level used for OpenSAK's County (omit for none) +# county_pack pack name part of the county file (default "all_") +# name_tags OSM tags tried in order for the name (default ["name"]) +# county_name_tags the same for counties only (default: name_tags) +# county_strip_prefixes prefixes removed from county names, e.g. ["Gemeinde "] +# state_name_split multilingual names such as "Graubünden/Grischun/Grigioni" are +# county_name_split cut at this separator and the first part is kept (default: none) +# county_name_join instead of cutting, replace the separator in county names with +# this text, e.g. "Biel/Bienne" -> "Biel-Bienne" +# names renames applied last, OSM name -> OpenSAK name, so names match +# those geocaching.com / Project-GC use (both layers) +# county_exclude county names (after renaming) to leave out, e.g. lake areas that +# OSM tags at the municipality level but belong to no municipality +# +# Closed ways tagged boundary=administrative are always left out (mostly mistagged +# buildings); counties sharing a name get their state's ISO 3166-2 code appended, +# "Reinach (AG)" / "Reinach (BL)". +# +# admin_level differs per country — see +# https://wiki.openstreetmap.org/wiki/Tag:boundary%3Dadministrative#Country_specific_values + +[CH] +iso3 = "che" +geofabrik = "europe/switzerland" +state_level = 4 # cantons (26) +county_level = 8 # municipalities (Gemeinden / communes / comuni) +county_pack = "all_che" # same file name as the GSAK-derived pack in #788 +# Official municipality names (BFS) carry the canton when the name exists more +# than once: "Muri (AG)", "Reinach (BL)". OSM keeps them in official_name. +county_name_tags = ["official_name", "name"] +county_strip_prefixes = ["Gemeinde "] # some GR official_name values: "Gemeinde Avers" +state_name_split = "/" # "Valais/Wallis" -> "Valais" (as in the GSAK-derived pack) +county_name_split = "/" +county_name_join = "-" # "Biel/Bienne" -> "Biel-Bienne" (as in the GSAK-derived pack) +# Lake areas not part of any municipality (gemeindefreie Gebiete) — a cache +# position in these lakes gets no county, rather than the lake's name. +county_exclude = [ + "Bielersee (BE)", "Bielersee (NE)", "Brienzersee", "Thunersee", + "Lac de Neuchâtel (BE)", "Lac de Neuchâtel (NE)", +] + +[CH.names] +"Fribourg - Freiburg" = "Fribourg" # municipality; the canton is "Fribourg/Freiburg" +# Errors in OSM (no official_name) — to be fixed upstream in OSM, then removed here: +"Biss'one" = "Bissone" # r1684629, BFS 5154 +"Roveredo GR" = "Roveredo (GR)" # r1684142, BFS 3834 +# official_name without the canton the BFS name carries: +"Bremgarten" = "Bremgarten (AG)" +"Carouge" = "Carouge (GE)" +"Corsier" = "Corsier (GE)" +"Hauterive" = "Hauterive (FR)" +"Romont" = "Romont (FR)" +"Crans" = "Crans (VD)" # Crans-près-Céligny until 2021 +# State names: check against geocaching.com before adding, e.g. +# "Basel-Landschaft" = "Basel Landschaft" diff --git a/tools/boundaries/osm_extract.py b/tools/boundaries/osm_extract.py new file mode 100644 index 00000000..7cd0100f --- /dev/null +++ b/tools/boundaries/osm_extract.py @@ -0,0 +1,659 @@ +#!/usr/bin/env python3 +# tools/boundaries/osm_extract.py — build OpenSAK state/county packs from OpenStreetMap. +# +# Part of epic #968 (OSM as the preferred source for new and replacement +# boundary data). Reads a Geofabrik country extract (.osm.pbf), takes the +# administrative boundaries at the admin_level configured per country in +# osm_countries.toml, and writes them in the same pack format as +# gsak_to_opensak.py: +# +# /states/.geojson simplified (--state-tolerance) +# /counties/_.geojson full resolution by default +# /osm_extract_.json provenance: source file, OSM date +# /osm_extract__report.md summary + comparison report +# +# It does not touch boundaries.db or manifest.json: merging the packs into a +# release is the pipeline's job (#968 Phase 1, contributions override +# GSAK-derived data per country and layer). +# +# Usage (needs pyosmium from the dev dependencies: pip install -e ".[dev]"): +# +# .venv/bin/python3 tools/boundaries/osm_extract.py CH +# .venv/bin/python3 tools/boundaries/osm_extract.py CH --compare /path/to/CHE \ +# --check-points location_gaps.csv +# +# --pbf FILE use a local .osm.pbf (or .osm) instead of downloading +# --compare DIR compare with existing packs (DIR/states/.geojson, +# DIR/counties/_.geojson), e.g. the GSAK-derived ones +# --check-points CSV compare with names known from caches: the CSV written by the +# "Fill missing location" macro (lat, lon, level, known_value, +# known_from) — rows from Pocket Queries carry geocaching.com's names +# +# The download is cached in --cache-dir and reused for a day (--refresh to force). +# Geofabrik extracts are updated daily; please don't download more often than needed. + +from __future__ import annotations + +import argparse +import csv +import datetime as dt +import difflib +import json +import re +import sys +import tomllib +import unicodedata +import urllib.request +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +import shapely +from shapely.prepared import prep +from shapely.geometry import mapping as shp_mapping +from shapely.geometry.base import BaseGeometry + +_HERE = Path(__file__).resolve().parent +_REPO_ROOT = _HERE.parents[1] +DEFAULT_CONFIG = _HERE / "osm_countries.toml" +DEFAULT_OUT = _REPO_ROOT / "data" / "osm" +DEFAULT_CACHE = _REPO_ROOT / "data" / "osm" / "cache" +GEOFABRIK_URL = "https://download.geofabrik.de/{path}-latest.osm.pbf" +USER_AGENT = "OpenSAK-boundary-tools (+https://github.com/OpenSAK-Org/OpenSAK)" + +# Same baseline tolerance as gsak_to_opensak.py (~55 m at the equator). +DEFAULT_STATE_TOLERANCE = 0.0005 +# An outer ring narrower than this survives as a sliver, not a region. +MIN_AREA = 1e-9 + + +# ── Configuration ───────────────────────────────────────────────────────────── + +@dataclass +class CountryConfig: + code: str # ISO 3166-1 alpha-2, e.g. "CH" + iso3: str # OpenSAK country code, e.g. "che" + state_level: int + county_level: int | None = None + geofabrik: str | None = None + county_pack: str = "" + name_tags: list[str] = field(default_factory=lambda: ["name"]) + county_name_tags: list[str] | None = None # default: name_tags + county_strip_prefixes: list[str] = field(default_factory=list) + state_name_split: str | None = None + county_name_split: str | None = None + county_name_join: str | None = None + names: dict[str, str] = field(default_factory=dict) + county_exclude: list[str] = field(default_factory=list) + + def __post_init__(self) -> None: + if not self.county_pack: + self.county_pack = f"all_{self.iso3}" + + @property + def state_pack_name(self) -> str: + return f"{self.iso3}.geojson" + + @property + def county_pack_name(self) -> str: + return f"{self.iso3}_{self.county_pack}.geojson" + + +def load_config(path: Path, code: str) -> CountryConfig: + with path.open("rb") as fh: + data = tomllib.load(fh) + code = code.upper() + if code not in data: + known = ", ".join(sorted(data)) or "none" + raise SystemExit(f"{code} is not in {path} (configured: {known})") + entry: dict[str, Any] = dict(data[code]) + try: + return CountryConfig(code=code, **entry) + except TypeError as exc: # unknown or missing key + raise SystemExit(f"{path} [{code}]: {exc}") from exc + + +# ── Names ───────────────────────────────────────────────────────────────────── + +def pick_name(tags: dict[str, str], cfg: CountryConfig, layer: str) -> str | None: + """The OpenSAK name for a boundary: first of name_tags, then split/join, then renames.""" + keys = cfg.county_name_tags if layer == "county" and cfg.county_name_tags else cfg.name_tags + raw = next((tags[t].strip() for t in keys if tags.get(t, "").strip()), None) + if raw is None: + return None + name = raw + if layer == "county": + for prefix in cfg.county_strip_prefixes: + if name.startswith(prefix) and len(name) > len(prefix): + name = name[len(prefix):].strip() + break + split = cfg.state_name_split if layer == "state" else cfg.county_name_split + join = cfg.county_name_join if layer == "county" else None + if split and split in name: + parts = [p.strip() for p in name.split(split)] + name = join.join(parts) if join is not None else parts[0] + # A rename may be keyed by the raw OSM name or by the name after splitting. + return cfg.names.get(raw, cfg.names.get(name, name)) + + +def normalize(name: str) -> str: + """Case, accents and punctuation ignored — like the macro API's region matching.""" + text = unicodedata.normalize("NFKD", name) + text = "".join(ch for ch in text if not unicodedata.combining(ch)) + return re.sub(r"[^0-9a-z]+", "", text.casefold()) + + +# ── Reading OSM ─────────────────────────────────────────────────────────────── + +@dataclass +class Boundary: + osm_id: str # "r51701" + level: int + tags: dict[str, str] + geom: BaseGeometry + + +def read_boundaries(pbf: Path, levels: set[int]) -> tuple[list[Boundary], int]: + """All boundary=administrative relations at one of `levels` (2 is always included), + and the number of closed ways with those tags that were left out. + + Administrative boundaries are relations in OSM. A closed way tagged + boundary=administrative is nearly always a mistagged building or site + (in Switzerland: "Gemeindehaus", "Statistisches Amt", ...).""" + import osmium # optional dependency, only needed here + + wanted = {str(level) for level in levels | {2}} + factory = osmium.geom.WKBFactory() + result: list[Boundary] = [] + skipped_ways = 0 + processor = ( + osmium.FileProcessor(str(pbf)) + .with_areas(osmium.filter.TagFilter(("boundary", "administrative"))) + .with_filter(osmium.filter.EntityFilter(osmium.osm.AREA)) + ) + for area in processor: + if not isinstance(area, osmium.osm.Area): + continue + level = area.tags.get("admin_level", "") + if level not in wanted: + continue + if area.from_way(): + skipped_ways += 1 + continue + try: + geom = shapely.from_wkb(bytes.fromhex(factory.create_multipolygon(area))) + except RuntimeError: + # An incomplete relation: at the edge of the extract, or broken in OSM. + continue + result.append(Boundary(f"r{area.orig_id()}", int(level), + {t.k: t.v for t in area.tags}, geom)) + return result, skipped_ways + + +def osm_timestamp(pbf: Path) -> str | None: + """The replication timestamp in the file header (when the extract was cut).""" + try: + import osmium + reader = osmium.io.Reader(str(pbf), osmium.osm.osm_entity_bits.NOTHING) + try: + stamp = reader.header().get("osmosis_replication_timestamp") + finally: + reader.close() + return stamp or None + except Exception: # noqa: BLE001 — provenance is nice to have, never fatal + return None + + +def find_country(boundaries: list[Boundary], code: str) -> Boundary: + for b in boundaries: + if b.level == 2 and code in (b.tags.get("ISO3166-1:alpha2"), b.tags.get("ISO3166-1")): + return b + raise SystemExit(f"No admin_level=2 boundary with ISO3166-1 = {code} in the extract") + + +# ── Building the packs ──────────────────────────────────────────────────────── + +@dataclass +class Region: + name: str + osm_ids: list[str] + geom: BaseGeometry + code: str | None = None # "AG" from ISO3166-2 = "CH-AG" (states) + + +def collect_layer(boundaries: list[Boundary], country: BaseGeometry, level: int, + cfg: CountryConfig, layer: str, warnings: list[str], + states: list[Region] | None = None) -> list[Region]: + """The regions of one admin_level inside the country. + + Two regions with the same name are different places (Reinach AG and + Reinach BL), never merged: each gets the code of its state appended, + "Reinach (AG)", the way Swiss official names do it.""" + prepared = prep(country) + regions: list[Region] = [] + excluded: list[str] = [] + for b in boundaries: + if b.level != level: + continue + # Extracts carry pieces of neighbouring countries; keep what lies inside. + if not prepared.contains(b.geom.representative_point()): + continue + name = pick_name(b.tags, cfg, layer) + if name is None: + warnings.append(f"{layer} {b.osm_id} has no name — skipped") + continue + if layer == "county" and name in cfg.county_exclude: + excluded.append(name) + continue + iso = b.tags.get("ISO3166-2", "") + code = iso.split("-", 1)[1] if "-" in iso else None + regions.append(Region(name, [b.osm_id], b.geom, code)) + if excluded: + warnings.append(f"{layer}: left out as configured (county_exclude): " + + ", ".join(sorted(excluded))) + + by_name: dict[str, list[Region]] = {} + for r in regions: + by_name.setdefault(r.name, []).append(r) + for name, same in by_name.items(): + if len(same) < 2: + continue + codes = [_state_code(r.geom, states) for r in same] + if None in codes or len(set(codes)) < len(codes): # no state, or the same state + codes = [r.osm_ids[0] for r in same] + for r, code in zip(same, codes): + r.name = f"{name} ({code})" + warnings.append(f"{layer} '{name}' is the name of {len(same)} regions — renamed to " + + ", ".join(r.name for r in same)) + return sorted(regions, key=lambda r: normalize(r.name)) + + +def _state_code(geom: BaseGeometry, states: list[Region] | None) -> str | None: + point = geom.representative_point() + for s in states or []: + if s.code and s.geom.contains(point): + return s.code + return None + + +def _repair(geom: BaseGeometry) -> BaseGeometry: + if not geom.is_valid: + geom = shapely.make_valid(geom) + # make_valid can return a collection with lines/points; keep the polygons. + if isinstance(geom, shapely.GeometryCollection): + geom = shapely.union_all([g for g in geom.geoms + if g.geom_type in ("Polygon", "MultiPolygon")]) + return geom + + +def simplify_layer(regions: list[Region], tolerance: float, warnings: list[str]) -> None: + """Simplify as a coverage, so neighbours keep sharing their borders (no gaps, #782).""" + if tolerance > 0 and regions: + geoms = [r.geom for r in regions] + simplified: list[BaseGeometry] + try: + simplified = list(shapely.coverage_simplify(geoms, tolerance)) + except Exception as exc: # noqa: BLE001 — not a clean coverage: fall back + warnings.append(f"coverage simplification failed ({exc}); " + "simplified each region on its own") + simplified = [g.simplify(tolerance, preserve_topology=True) for g in geoms] + for region, geom in zip(regions, simplified): + region.geom = geom + for region in regions: + region.geom = _repair(region.geom) + if region.geom.is_empty or region.geom.area < MIN_AREA: + warnings.append(f"'{region.name}' is empty after simplification") + + +def to_feature(region: Region, cfg: CountryConfig, version: int, layer: str) -> dict[str, Any]: + geom = region.geom + if isinstance(geom, shapely.Polygon): + geom = shapely.MultiPolygon([geom]) + minx, miny, maxx, maxy = geom.bounds + if maxx - minx > 180: + raise SystemExit(f"'{region.name}' crosses the antimeridian — not supported yet") + return { + "type": "Feature", + "properties": { + "name": region.name, + "parent": cfg.iso3, + "version": version, + "source": "osm", + "licence": "ODbL", + "layer": layer, + "osm_id": ",".join(region.osm_ids), + }, + "bbox": [round(minx, 7), round(miny, 7), round(maxx, 7), round(maxy, 7)], + "geometry": shp_mapping(shapely.set_precision(geom, 1e-7)), + } + + +def write_pack(path: Path, features: list[dict[str, Any]]) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text( + json.dumps({"type": "FeatureCollection", "features": features}, + ensure_ascii=False, separators=(",", ":")), + encoding="utf-8", + ) + + +# ── Comparison with existing packs and with names from caches ───────────────── + +def _load_pack(path: Path) -> list[tuple[str, BaseGeometry]]: + data = json.loads(path.read_text(encoding="utf-8")) + return [(f["properties"]["name"], _repair(shapely.geometry.shape(f["geometry"]))) + for f in data["features"]] + + +class Resolver: + """Point → region name, for one layer.""" + + def __init__(self, regions: list[tuple[str, BaseGeometry]]) -> None: + self.names = [n for n, _ in regions] + self.geoms = [g for _, g in regions] + self.tree = shapely.STRtree(self.geoms) + + def names_at(self, point: BaseGeometry) -> list[str]: + idx = self.tree.query(point, predicate="intersects") + return sorted(self.names[i] for i in idx) + + +def _iou(a: BaseGeometry, b: BaseGeometry) -> float: + union = shapely.union(a, b).area + return shapely.intersection(a, b).area / union if union else 0.0 + + +def _rename_hint(geom: BaseGeometry, name: str, only_ref: list[str], + ref_by: dict[str, BaseGeometry]) -> str: + """Which reference region this OSM-only region probably is: same area, else similar name.""" + best, best_iou = None, 0.5 + overlapping: dict[str, str] = {} # a renamed region is still in the same place + for ref_name in only_ref: + other = ref_by[ref_name] + if not geom.intersects(other) or shapely.intersection(geom, other).area <= 0: + continue # elsewhere, or a neighbour that only touches + overlapping[normalize(ref_name)] = ref_name + iou = _iou(geom, other) + if iou > best_iou: + best, best_iou = ref_name, iou + if best is not None: + return f" — same area as the reference's '{best}' ({best_iou:.0%} overlap)" + close = difflib.get_close_matches(normalize(name), list(overlapping), n=1, cutoff=0.85) + return f" — similar name in the reference: '{overlapping[close[0]]}'" if close else "" + + +def match_names(osm_names: list[str], ref_names: list[str]) -> dict[str, str]: + """Reference name -> OSM name: equal names first, then names that differ only in + case, accents or punctuation ("Basel Stadt" / "Basel-Stadt") when that is unambiguous. + Equal names come first because real regions can differ by an accent only + (Brugg and Brügg are two Swiss municipalities).""" + pairs = {n: n for n in set(ref_names) & set(osm_names)} + rest_osm: dict[str, list[str]] = {} + for n in set(osm_names) - set(pairs.values()): + rest_osm.setdefault(normalize(n), []).append(n) + rest_ref: dict[str, list[str]] = {} + for n in set(ref_names) - set(pairs): + rest_ref.setdefault(normalize(n), []).append(n) + for key, refs in rest_ref.items(): + if len(refs) == 1 and len(rest_osm.get(key, [])) == 1: + pairs[refs[0]] = rest_osm[key][0] + return pairs + + +def compare_layer(layer: str, osm: list[Region], ref: list[tuple[str, BaseGeometry]]) -> list[str]: + """Markdown lines comparing the OSM regions of one layer with a reference pack.""" + lines = [f"### {layer.capitalize()}: OSM {len(osm)} vs reference {len(ref)}", ""] + osm_by = {r.name: r.geom for r in osm} + ref_by = dict(ref) + pairs = match_names(list(osm_by), list(ref_by)) + + only_osm = sorted(set(osm_by) - set(pairs.values()), key=normalize) + only_ref = sorted(set(ref_by) - set(pairs), key=normalize) + if only_osm: + lines.append(f"**Only in OSM ({len(only_osm)}):**") + for n in only_osm: + lines.append(f"- {n}{_rename_hint(osm_by[n], n, only_ref, ref_by)}") + lines.append("") + if only_ref: + lines.append(f"**Only in the reference ({len(only_ref)}):** " + ", ".join(only_ref)) + lines.append("") + spelled = sorted((r, o) for r, o in pairs.items() if r != o) + if spelled: + lines.append(f"**Spelled differently ({len(spelled)}), reference → OSM:** " + + ", ".join(f"{r} → {o}" for r, o in spelled)) + lines.append("") + + # Same region, different shape (intersection over union). When one polygon + # lies almost entirely inside the other, the larger one has an extra piece + # (often lake area: the GSAK-derived municipalities reach into the lakes). + differ: list[tuple[float, str]] = [] + ref_larger: list[str] = [] + osm_larger: list[str] = [] + for r, o in pairs.items(): + a, b = osm_by[o], ref_by[r] + iou = _iou(a, b) + if iou >= 0.9: + continue + shared = shapely.intersection(a, b).area + if a.area and shared / a.area >= 0.95: + ref_larger.append(o) + elif b.area and shared / b.area >= 0.95: + osm_larger.append(o) + else: + differ.append((iou, o)) + n_shape = len(differ) + len(ref_larger) + len(osm_larger) + lines.append(f"Matched: {len(pairs)}; of those {n_shape} differ in shape (overlap below 90 %).") + lines.append("") + if ref_larger: + lines.append(f"**The reference is larger ({len(ref_larger)})** — OSM's polygon lies " + "inside it; often lake area: " + ", ".join(sorted(ref_larger, key=normalize))) + lines.append("") + if osm_larger: + lines.append(f"**OSM is larger ({len(osm_larger)})** — the reference lies inside it; " + "often a merger: " + ", ".join(sorted(osm_larger, key=normalize))) + lines.append("") + if differ: + lines.append(f"**Different shape ({len(differ)}):**") + for iou, name in sorted(differ)[:40]: + lines.append(f"- {name}: {iou:.0%} overlap") + if len(differ) > 40: + lines.append(f"- … and {len(differ) - 40} more") + lines.append("") + + # Where would a cache in the middle of the reference region end up? + resolver = Resolver([(r.name, r.geom) for r in osm]) + moved: list[str] = [] + for name, geom in ref: + found = resolver.names_at(geom.representative_point()) + if pairs.get(name, name) not in found: + moved.append(f"- {name} → {', '.join(found) or 'nothing'}") + lines.append(f"A point inside each reference {layer} resolves to another name in OSM: " + f"{len(moved)} of {len(ref)}" + (":" if moved else ".")) + lines.extend(moved[:40]) + if len(moved) > 40: + lines.append(f"- … and {len(moved) - 40} more") + lines.append("") + return lines + + +def check_points(csv_path: Path, country: BaseGeometry, + layers: dict[str, list[Region]]) -> list[str]: + """Compare names from the gap-report CSV (macro 'Fill missing location') with OSM.""" + resolvers = {layer: Resolver([(r.name, r.geom) for r in regions]) + for layer, regions in layers.items() if regions} + prepared = prep(country) + stats: dict[tuple[str, str], list[int]] = {} + mismatches: dict[tuple[str, str, str], int] = {} + with csv_path.open(encoding="utf-8", newline="") as fh: + for row in csv.DictReader(fh): + layer, known = row.get("level", ""), (row.get("known_value") or "").strip() + source = row.get("known_from") or "?" + if layer not in resolvers or not known: + continue + point = shapely.Point(float(row["lon"]), float(row["lat"])) + if not prepared.contains(point): + continue + found = resolvers[layer].names_at(point) + ok = any(normalize(f) == normalize(known) for f in found) + s = stats.setdefault((layer, source), [0, 0]) + s[0 if ok else 1] += 1 + if not ok: + key = (layer, known, ", ".join(found) or "nothing") + mismatches[key] = mismatches.get(key, 0) + 1 + lines = [f"### Names known from caches ({csv_path.name})", ""] + if not stats: + return lines + ["No rows with a known name inside the country.", ""] + lines.append("| Level | Known from | Same name | Other name |") + lines.append("|---|---|---|---|") + for (level, source), (same, other) in sorted(stats.items()): + lines.append(f"| {level} | {source} | {same} | {other} |") + lines.append("") + if mismatches: + lines.append("Different names (known → OSM, number of caches):") + for (level, name, osm_names), n in sorted(mismatches.items(), key=lambda kv: -kv[1])[:40]: + lines.append(f"- {level}: {name} → {osm_names} ({n})") + lines.append("") + return lines + + +# ── Download ────────────────────────────────────────────────────────────────── + +def download(cfg: CountryConfig, cache_dir: Path, refresh: bool) -> Path: + if not cfg.geofabrik: + raise SystemExit(f"[{cfg.code}] has no 'geofabrik' path — pass --pbf") + dest = cache_dir / (cfg.geofabrik.replace("/", "_") + "-latest.osm.pbf") + if dest.exists() and not refresh: + age = dt.datetime.now() - dt.datetime.fromtimestamp(dest.stat().st_mtime) + if age < dt.timedelta(days=1): + print(f"Using cached {dest} ({age.seconds // 3600} h old)") + return dest + url = GEOFABRIK_URL.format(path=cfg.geofabrik) + print(f"Downloading {url} …") + dest.parent.mkdir(parents=True, exist_ok=True) + tmp = dest.with_suffix(".part") + request = urllib.request.Request(url, headers={"User-Agent": USER_AGENT}) + with urllib.request.urlopen(request, timeout=60) as resp, tmp.open("wb") as out: + while chunk := resp.read(1 << 20): + out.write(chunk) + tmp.replace(dest) + print(f" {dest.stat().st_size / 1e6:.0f} MB") + return dest + + +# ── Main ────────────────────────────────────────────────────────────────────── + +def run(cfg: CountryConfig, pbf: Path, out_dir: Path, *, version: int, + state_tolerance: float, county_tolerance: float, + compare_dir: Path | None = None, points_csv: Path | None = None, + source_url: str | None = None) -> dict[str, Any]: + levels = {cfg.state_level} | ({cfg.county_level} if cfg.county_level else set()) + print(f"Reading administrative boundaries from {pbf.name} …") + boundaries, skipped_ways = read_boundaries(pbf, levels) + country = find_country(boundaries, cfg.code) + warnings: list[str] = [] + if skipped_ways: + warnings.append(f"{skipped_ways} closed ways tagged boundary=administrative left out " + "(administrative boundaries are relations; these are mostly buildings)") + + layers: dict[str, list[Region]] = { + "state": collect_layer(boundaries, country.geom, cfg.state_level, cfg, "state", warnings), + } + if cfg.county_level: + layers["county"] = collect_layer(boundaries, country.geom, cfg.county_level, + cfg, "county", warnings, states=layers["state"]) + simplify_layer(layers["state"], state_tolerance, warnings) + if "county" in layers: + simplify_layer(layers["county"], county_tolerance, warnings) + + files: dict[str, str] = {} + pack_names = {"state": cfg.state_pack_name, "county": cfg.county_pack_name} + dirs = {"state": "states", "county": "counties"} + for layer, regions in layers.items(): + if not regions: + warnings.append(f"no {layer} boundaries at admin_level " + f"{cfg.state_level if layer == 'state' else cfg.county_level}") + continue + path = out_dir / dirs[layer] / pack_names[layer] + write_pack(path, [to_feature(r, cfg, version, layer) for r in regions]) + files[layer] = str(path.relative_to(out_dir)) + + timestamp = osm_timestamp(pbf) + meta: dict[str, Any] = { + "country": cfg.code, "iso3": cfg.iso3, + "source": "OpenStreetMap", "licence": "ODbL-1.0", + "attribution": "© OpenStreetMap contributors", + "extract": source_url or pbf.name, "osm_timestamp": timestamp, + "created": dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"), + "admin_levels": {"state": cfg.state_level, "county": cfg.county_level}, + "counts": {layer: len(regions) for layer, regions in layers.items()}, + "files": files, "version": version, + } + (out_dir / f"osm_extract_{cfg.iso3}.json").write_text( + json.dumps(meta, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") + + report = [f"# OSM boundaries for {cfg.code} ({cfg.iso3})", "", + f"- Extract: {meta['extract']}", f"- OSM data as of: {timestamp or 'unknown'}", + f"- States (admin_level {cfg.state_level}): {len(layers['state'])}"] + if "county" in layers: + report.append(f"- Counties (admin_level {cfg.county_level}): {len(layers['county'])}") + report.append("") + if warnings: + report += ["## Warnings", ""] + [f"- {w}" for w in warnings] + [""] + if compare_dir is not None: + report += ["## Comparison with the reference packs", "", f"Reference: `{compare_dir}`", ""] + for layer, regions in layers.items(): + ref_path = compare_dir / dirs[layer] / pack_names[layer] + if ref_path.exists(): + report += compare_layer(layer, regions, _load_pack(ref_path)) + else: + report += [f"### {layer.capitalize()}: no reference file {ref_path.name}", ""] + if points_csv is not None: + report += ["## Comparison with names from caches", ""] + report += check_points(points_csv, country.geom, layers) + report_path = out_dir / f"osm_extract_{cfg.iso3}_report.md" + report_path.write_text("\n".join(report), encoding="utf-8") + meta["report"] = str(report_path) + meta["warnings"] = warnings + return meta + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__ or "", + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("country", help="ISO 3166-1 alpha-2 code configured in the config file, e.g. CH") + parser.add_argument("--config", type=Path, default=DEFAULT_CONFIG) + parser.add_argument("--pbf", type=Path, help="local .osm.pbf/.osm instead of downloading") + parser.add_argument("--out-dir", type=Path, default=DEFAULT_OUT) + parser.add_argument("--cache-dir", type=Path, default=DEFAULT_CACHE) + parser.add_argument("--refresh", action="store_true", help="download even if a cached copy is fresh") + parser.add_argument("--compare", type=Path, metavar="DIR", help="existing packs to compare with") + parser.add_argument("--check-points", type=Path, metavar="CSV", + help="gap-report CSV from the 'Fill missing location' macro") + parser.add_argument("--version", type=int, default=1, help="polygon version written to each feature") + parser.add_argument("--state-tolerance", type=float, default=DEFAULT_STATE_TOLERANCE, + metavar="DEGREES", help="simplification of the state layer (default: %(default)s)") + parser.add_argument("--county-tolerance", type=float, default=0.0, + metavar="DEGREES", help="simplification of the county layer (default: none)") + args = parser.parse_args(argv) + + cfg = load_config(args.config, args.country) + source_url = None + if args.pbf: + pbf = args.pbf + else: + pbf = download(cfg, args.cache_dir, args.refresh) + source_url = GEOFABRIK_URL.format(path=cfg.geofabrik) + meta = run(cfg, pbf, args.out_dir / cfg.iso3, version=args.version, + state_tolerance=args.state_tolerance, county_tolerance=args.county_tolerance, + compare_dir=args.compare, points_csv=args.check_points, source_url=source_url) + for layer, count in meta["counts"].items(): + print(f"{layer}: {count}") + for w in meta["warnings"]: + print(f"warning: {w}") + print(f"Packs and report in {args.out_dir / cfg.iso3}") + print(f"Report: {meta['report']}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) From dfef2d0db15a981a9a4ac10776a08dc3d7595f05 Mon Sep 17 00:00:00 2001 From: nagisml Date: Wed, 28 Oct 2026 16:50:58 +0100 Subject: [PATCH 2/4] Change the auto-fit columns shortcut from Ctrl+W to Ctrl+Alt+W Ctrl+W is the usual "close window/tab" key, and on macOS Qt turns it into Cmd+W. Pressing it out of habit would silently replace the user's column widths, with no undo. Ctrl+Alt+W is now the default on every platform, and "Reset all" in the Shortcuts dialog restores it. Users who clicked OK in the Shortcuts dialog in beta.11 keep Ctrl+W, because the dialog saved it, until they use "Reset all". Follow-up to #1212. --- src/opensak/gui/dialogs/shortcuts_dialog.py | 2 +- src/opensak/gui/mainwindow.py | 4 ++-- tests/unit-tests/test_cache_table.py | 2 +- tests/unit-tests/test_map_maximize_popout.py | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/opensak/gui/dialogs/shortcuts_dialog.py b/src/opensak/gui/dialogs/shortcuts_dialog.py index a3dce603..47854379 100644 --- a/src/opensak/gui/dialogs/shortcuts_dialog.py +++ b/src/opensak/gui/dialogs/shortcuts_dialog.py @@ -29,7 +29,7 @@ "trip_planner": "Ctrl+T", "coord_converter": "Ctrl+K", "projection": "Ctrl+P", - "autofit_columns": "Ctrl+W", + "autofit_columns": "Ctrl+Alt+W", } diff --git a/src/opensak/gui/mainwindow.py b/src/opensak/gui/mainwindow.py index d2f9fcee..d5d11c12 100644 --- a/src/opensak/gui/mainwindow.py +++ b/src/opensak/gui/mainwindow.py @@ -550,7 +550,7 @@ def _setup_menu(self) -> None: view_menu.addAction(act_columns) self._act_autofit_columns = QAction(tr("action_autofit_columns"), self) - self._act_autofit_columns.setShortcut(QKeySequence("Ctrl+W")) + self._act_autofit_columns.setShortcut(QKeySequence("Ctrl+Alt+W")) self._act_autofit_columns.triggered.connect(self._autofit_columns) view_menu.addAction(self._act_autofit_columns) @@ -3394,7 +3394,7 @@ def _open_column_chooser(self) -> None: self._refresh_table_only() def _autofit_columns(self) -> None: - """Size every cache-list column to fit its contents (Ctrl+W).""" + """Size every cache-list column to fit its contents (Ctrl+Alt+W).""" self._cache_table.autofit_columns() def _populate_column_view_combo(self) -> None: diff --git a/tests/unit-tests/test_cache_table.py b/tests/unit-tests/test_cache_table.py index fdfdb9c6..fca6354d 100644 --- a/tests/unit-tests/test_cache_table.py +++ b/tests/unit-tests/test_cache_table.py @@ -2068,7 +2068,7 @@ def test_tooltip_is_always_the_full_title(self, gpx_view): assert self._header(gpx_view, Qt.ItemDataRole.ToolTipRole) == "Last GPX update" -# ── Auto-fit column widths (Ctrl+W) ────────────────────────────────────────── +# ── Auto-fit column widths (Ctrl+Alt+W) ────────────────────────────────────── class TestAutofitColumns: @pytest.fixture diff --git a/tests/unit-tests/test_map_maximize_popout.py b/tests/unit-tests/test_map_maximize_popout.py index 8211d3b2..6638d4f1 100644 --- a/tests/unit-tests/test_map_maximize_popout.py +++ b/tests/unit-tests/test_map_maximize_popout.py @@ -488,11 +488,11 @@ def test_popout_shortcut_is_ctrl_shift_m(self, window): from PySide6.QtGui import QKeySequence assert window._act_popout_map.shortcut() == QKeySequence("Ctrl+Shift+M") - def test_autofit_columns_is_ctrl_w_and_in_registry(self, window): + def test_autofit_columns_is_ctrl_alt_w_and_in_registry(self, window): from PySide6.QtGui import QKeySequence keys = [key for key, _, _ in window._shortcut_registry] assert "autofit_columns" in keys - assert window._act_autofit_columns.shortcut() == QKeySequence("Ctrl+W") + assert window._act_autofit_columns.shortcut() == QKeySequence("Ctrl+Alt+W") # ── Settings property tests ─────────────────────────────────────────────────── From 36557705dd8c8dd695b4f02a666c8425efd467c0 Mon Sep 17 00:00:00 2001 From: nagisml Date: Wed, 28 Oct 2026 17:38:51 +0100 Subject: [PATCH 3/4] Escape "&" in the "Download & Install" update button (#1218) Qt treats "&" in button labels as a keyboard-shortcut marker, so the update dialog showed "Download _Install" ("Herunterladen _installieren" in German/Swiss German, also Dutch). Escape it as "&&" where the button is created; the translations stay unchanged because the same wording in the dialog's info text displays "&" correctly. Fixes #1218 --- src/opensak/gui/mainwindow.py | 5 ++++- tests/unit-tests/test_self_update_wiring_572.py | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/opensak/gui/mainwindow.py b/src/opensak/gui/mainwindow.py index d5d11c12..ccd16320 100644 --- a/src/opensak/gui/mainwindow.py +++ b/src/opensak/gui/mainwindow.py @@ -4335,8 +4335,11 @@ def _on_update_available( tr("update_appimage_upgrade_button"), QMessageBox.ButtonRole.AcceptRole ) elif can_self_download: + # Escape "&" — Qt would otherwise eat it as a mnemonic marker + # ("Download & Install" rendered as "Download _Install"). btn_primary = msg.addButton( - tr("update_download_button"), QMessageBox.ButtonRole.AcceptRole + tr("update_download_button").replace("&", "&&"), + QMessageBox.ButtonRole.AcceptRole, ) else: btn_primary = msg.addButton( diff --git a/tests/unit-tests/test_self_update_wiring_572.py b/tests/unit-tests/test_self_update_wiring_572.py index cf3e24cc..dcbda83f 100644 --- a/tests/unit-tests/test_self_update_wiring_572.py +++ b/tests/unit-tests/test_self_update_wiring_572.py @@ -80,7 +80,7 @@ def test_windows_offers_download_and_install(self, window): patch.object(MainWindow, "_start_self_download_update") as mock_start: fake_msg = _run_dialog_and_click_primary(window) primary_text = fake_msg.addButton.call_args_list[0][0][0] - assert primary_text == "Download & Install" + assert primary_text == "Download && Install" mock_start.assert_called_once_with("v1.20.0") def test_macos_offers_download_and_install(self, window): @@ -90,7 +90,7 @@ def test_macos_offers_download_and_install(self, window): patch.object(MainWindow, "_start_self_download_update") as mock_start: fake_msg = _run_dialog_and_click_primary(window) primary_text = fake_msg.addButton.call_args_list[0][0][0] - assert primary_text == "Download & Install" + assert primary_text == "Download && Install" mock_start.assert_called_once_with("v1.20.0") def test_appimage_integrated_still_takes_priority_over_self_download(self, window): From ba536109843fcd610093e83e6808d4e673195c44 Mon Sep 17 00:00:00 2001 From: nagisml Date: Wed, 28 Oct 2026 18:10:55 +0100 Subject: [PATCH 4/4] mypy fix --- tests/e2e-tests/test_e2e_mainwindow.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/e2e-tests/test_e2e_mainwindow.py b/tests/e2e-tests/test_e2e_mainwindow.py index b26050e4..0b0967a3 100644 --- a/tests/e2e-tests/test_e2e_mainwindow.py +++ b/tests/e2e-tests/test_e2e_mainwindow.py @@ -1798,7 +1798,10 @@ def test_on_update_available_download_button_self_downloads( monkeypatch.setattr( seeded_window, "_start_self_download_update", lambda tag: started.append(tag) ) - self._click_update_dialog_button(monkeypatch, tr("update_download_button")) + # The button label escapes "&" as "&&" so Qt doesn't eat it as a mnemonic (#1218). + self._click_update_dialog_button( + monkeypatch, tr("update_download_button").replace("&", "&&") + ) seeded_window._on_update_available("v9.9.9", "http://example.com/release", manual=True)