Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion src/opensak/gui/dialogs/shortcuts_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"trip_planner": "Ctrl+T",
"coord_converter": "Ctrl+K",
"projection": "Ctrl+P",
"autofit_columns": "Ctrl+W",
"autofit_columns": "Ctrl+Alt+W",
}


Expand Down
9 changes: 6 additions & 3 deletions src/opensak/gui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e-tests/test_e2e_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/test_cache_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/unit-tests/test_map_maximize_popout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ───────────────────────────────────────────────────
Expand Down
195 changes: 195 additions & 0 deletions tests/unit-tests/test_osm_extract.py
Original file line number Diff line number Diff line change
@@ -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'<node id="{nid}" version="1" lat="{lat}" lon="{lon}"/>')
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'<way id="{wid}" version="1">'
+ "".join(f'<nd ref="{n}"/>' for n in ring) + "</way>")
all_tags = {"type": "boundary", "boundary": "administrative", **tags}
rel_xml.append(f'<relation id="{rid}" version="1">'
f'<member type="way" ref="{wid}" role="outer"/>'
+ "".join(f'<tag k="{k}" v="{v}"/>' for k, v in all_tags.items())
+ "</relation>")
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('<way id="999" version="1">' + "".join(f'<nd ref="{n}"/>' for n in ring)
+ "".join(f'<tag k="{k}" v="{v}"/>'
for k, v in {"boundary": "administrative", **tags}.items())
+ "</way>")
path.write_text(
'<?xml version="1.0" encoding="UTF-8"?>\n<osm version="0.6" generator="test">\n'
+ "\n".join(node_xml + way_xml + rel_xml) + "\n</osm>\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('<tag k="ISO3166-2" v="XX-OU"/>', "")
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"]) == {}
4 changes: 2 additions & 2 deletions tests/unit-tests/test_self_update_wiring_572.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
65 changes: 65 additions & 0 deletions tools/boundaries/osm_countries.toml
Original file line number Diff line number Diff line change
@@ -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/<iso3>.geojson, counties/<iso3>_<county_pack>.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_<iso3>")
# 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"
Loading
Loading