From e2461f794c7db39cb8cffdac70612c100bb700f7 Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Thu, 12 Nov 2026 13:42:04 +0100 Subject: [PATCH] Places index: also drop localities, isolated dwellings and kindless objects Measured on France (5.3M rows): place=locality ~10% (mostly cadastral names of fields and woods), isolated_dwelling ~5%, objects with a name but none of the recognized kind keys ~4% (bus route relations etc.). Schema version 3, so rake places:index rebuilds existing indexes. --- TODO.adoc | 2 +- lib/maptool/places.rb | 20 +++++++++++++------- test/test_places.rb | 5 ++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/TODO.adoc b/TODO.adoc index 7fd00c4..4ce059f 100644 --- a/TODO.adoc +++ b/TODO.adoc @@ -63,7 +63,7 @@ independence from connectivity. * [x] Unified search box — one query across every data source, not just what's currently on the map. `GET /api/search` (`lib/maptool/search.rb`) for local sources, online ones queried from the browser (`public/js/search.js`) ** [x] Every layer under Tracks & Points (GPX, FIT, GeoJSON, notes, plugin output) and offline hiking trails — shown *and* hidden ones. Matches name + description, case/diacritics-insensitive ("kosice" finds "Košice"); parsed features cached in memory per file by mtime -** [x] Offline vector data — every named OSM object in each country's Geofabrik `.osm.pbf` (`osmium tags-filter nwr/name` + `osmium export -f geojsonseq`) into a SQLite FTS5 index, `data/places/.sqlite` (`lib/maptool/places.rb`), built as the last offline-build step ("indexing places") and cached by mtime like hiking trails. Prefix + diacritics-insensitive matching, street segments merged per ~2 km cell; named buildings and land-use areas dropped (unless they're also a shop/amenity/…), names searchable without their apostrophes ("mcdonalds") and by `brand`. `Places::SCHEMA_VERSION` makes `rake places:index` rebuild older indexes by itself +** [x] Offline vector data — every named OSM object in each country's Geofabrik `.osm.pbf` (`osmium tags-filter nwr/name` + `osmium export -f geojsonseq`) into a SQLite FTS5 index, `data/places/.sqlite` (`lib/maptool/places.rb`), built as the last offline-build step ("indexing places") and cached by mtime like hiking trails. Prefix + diacritics-insensitive matching, street segments merged per ~2 km cell; named buildings, land-use areas, `place=locality`/`isolated_dwelling` and objects with no recognized kind dropped (a shop in a building stays), names searchable without their apostrophes ("mcdonalds") and by `brand`. `Places::SCHEMA_VERSION` makes `rake places:index` rebuild older indexes by itself ** [x] Every result shows where it was found (layer file / "Hiking trails SK" / "Online · Nominatim"); clicking one zooms to it (fitBounds for a track) and switches its layer on if it was off ** [x] Source checklist in the search panel (all on by default), re-runs the search on change ** [x] Online sources pluggable — one more entry in search.js's `SOURCES`; Nominatim already in, its failure only adds a "no connection" line under the local results diff --git a/lib/maptool/places.rb b/lib/maptool/places.rb index 5f89f98..45b40b5 100644 --- a/lib/maptool/places.rb +++ b/lib/maptool/places.rb @@ -16,17 +16,23 @@ module Maptool # Bumped whenever the table layout or what goes into it changes — # PmtilesBuilder rebuilds an index whose user_version is older, so a # plain `rake places:index` picks the change up. - SCHEMA_VERSION = 2 + SCHEMA_VERSION = 3 # Most to least important; the first key a feature carries decides # its kind and rank. Within "place", the value refines it further. KIND_KEYS = %w[place natural tourism amenity historic leisure shop aeroway railway highway waterway boundary landuse man_made office building].freeze # Named buildings and land-use areas (a farm's fields, an industrial - # estate) are a large share of every country's index and nobody looks - # for them by name — dropped, but only when that's what the object - # *is*: a supermarket drawn as a building is still shop=supermarket. + # estate) are noise in results — nobody looks for them by name — so + # they're dropped, but only when that's what the object *is*: a + # supermarket drawn as a building is still shop=supermarket. (Measured: + # only ~1-7% of an index's size, so this is about result quality.) SKIPPED_KEYS = %w[landuse building].freeze + # Same idea, measured on France (5.3M rows): place=locality (~10%, + # mostly cadastral names of fields and woods) and isolated_dwelling + # (~5%, single farms/houses) — plus anything named but with none of + # KIND_KEYS (~4%: bus route relations and the like). + SKIPPED_PLACES = %w[locality isolated_dwelling].freeze APOSTROPHES = "'’ʼ`" PLACE_RANK = { "city" => 0, "town" => 1, "village" => 2, "suburb" => 3, "hamlet" => 3 }.freeze # How much closer a settlement counts as when results are ordered by @@ -108,7 +114,8 @@ module Maptool geometry = feature["geometry"] || {} coords = geometry["coordinates"] key = KIND_KEYS.find { |k| tags[k] } - return nil if name.empty? || !coords.is_a?(Array) || coords.empty? || SKIPPED_KEYS.include?(key) + return nil if name.empty? || !coords.is_a?(Array) || coords.empty? || key.nil? || SKIPPED_KEYS.include?(key) + return nil if key == "place" && SKIPPED_PLACES.include?(tags["place"]) points = positions(coords) lngs, lats = points.map { |p| p.first(2) }.transpose @@ -119,8 +126,7 @@ module Maptool lon, lat = geometry["type"] == "LineString" ? points[points.size / 2] : [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2] value = tags[key].to_s - kind = if key.nil? then "" - elsif key == "highway" && geometry["type"] != "Point" then "street" + kind = if key == "highway" && geometry["type"] != "Point" then "street" else value.tr("_", " ") end importance = key == "place" ? PLACE_RANK.fetch(value, KEY_RANK["place"]) : KEY_RANK[key] diff --git a/test/test_places.rb b/test/test_places.rb index 6f6ba1f..34b73f7 100644 --- a/test/test_places.rb +++ b/test/test_places.rb @@ -20,6 +20,9 @@ class PlacesTest < Minitest::Test feature("Point", [18.0, 48.0], "highway" => "crossing"), # no name -> skipped feature("Polygon", [[[17.0, 48.0], [17.01, 48.0], [17.01, 48.01], [17.0, 48.0]]], "name" => "Bytovka Lidl", "building" => "yes"), feature("Polygon", [[[17.0, 48.0], [17.01, 48.0], [17.01, 48.01], [17.0, 48.0]]], "name" => "Družstvo", "landuse" => "farmland"), + feature("Point", [17.3, 48.3], "name" => "Pod Družstvom", "place" => "locality"), + feature("Point", [17.3, 48.31], "name" => "Samota Družstevná", "place" => "isolated_dwelling"), + feature("LineString", [[17.0, 48.0], [17.5, 48.5]], "name" => "Bus 31 Družstvo", "type" => "route"), feature("Polygon", [[[17.2, 48.1], [17.21, 48.1], [17.21, 48.11], [17.2, 48.1]]], "name" => "Lidl Petržalka", "brand" => "Lidl", "shop" => "supermarket", "building" => "retail"), feature("Point", [17.15, 48.15], "name" => "McDonald's", "amenity" => "fast_food", "brand" => "McDonald's"), @@ -63,7 +66,7 @@ class PlacesTest < Minitest::Test def test_buildings_and_land_use_are_dropped_but_a_shop_in_a_building_is_kept assert_empty search("bytovka") - assert_empty search("druzstvo") + assert_empty search("druzstv") # landuse, locality, isolated dwelling, no kind at all assert_equal [["Lidl Petržalka", "supermarket"]], search("lidl").map { |r| r.values_at(:name, :kind) } end