Files
maptool/test/test_places.rb
T
randy 3f973a26d7
CI / check (push) Successful in 26s
CI / deploy (push) Successful in 33s
Search the offline map and Wikipedia, nearest results first
Every named OSM object in each country's Geofabrik extract goes into a
SQLite FTS5 index (data/places/<CODE>.sqlite, lib/maptool/places.rb),
built as the last offline-build step or on its own with
rake places:index. Kiwix full-text search is another source; clicking a
hit opens the article. Results from every source are sorted by distance
from the map center (settlements weighted closer) and show it; each
source is queried separately so a slow one doesn't hold up the list.
2026-11-12 12:13:13 +01:00

69 lines
3.1 KiB
Ruby

require_relative "test_helper"
require "maptool/places"
require "stringio"
# No real osmium — import is fed canned `osmium export -f geojsonseq`
# lines (same split HikingTrailsTest makes around parse_opl).
class PlacesTest < Minitest::Test
def setup
@dir = Dir.mktmpdir
@db = File.join(@dir, "SK.sqlite")
lines = [
feature("Point", [17.107, 48.148], "name" => "Bratislava", "place" => "city"),
feature("Point", [17.20, 48.30], "name" => "Bratislavská reštaurácia", "amenity" => "restaurant"),
feature("Point", [21.26, 48.72], "name" => "Hlavná", "place" => "square"),
# two segments of one street a few hundred metres apart -> one row
feature("LineString", [[17.10, 48.14], [17.101, 48.141], [17.102, 48.142]], "name" => "Hlavná", "highway" => "residential"),
feature("LineString", [[17.103, 48.143], [17.104, 48.144]], "name" => "Hlavná", "highway" => "residential"),
feature("Polygon", [[[19.0, 48.0], [19.2, 48.0], [19.2, 48.2], [19.0, 48.0]]], "name" => "Košické jazero", "natural" => "water"),
feature("Point", [18.0, 48.0], "highway" => "crossing"), # no name -> skipped
"not json",
]
Maptool::Places.import(StringIO.new(lines.join("\n")), @db)
end
def teardown
FileUtils.remove_entry(@dir)
end
def test_prefix_and_diacritics_insensitive_with_more_important_kinds_first
results = search("bratisl")
assert_equal ["Bratislava", "Bratislavská reštaurácia"], results.map { |r| r[:name] }
assert_equal ["city", "restaurant"], results.map { |r| r[:kind] }
assert_equal ["Košické jazero"], search("kosicke").map { |r| r[:name] }
assert_equal [19.0, 48.0, 19.2, 48.2], search("jazero").first[:bbox]
end
def test_street_segments_are_merged_and_nearer_matches_come_first
assert_equal ["square", "street"], search("hlavna", center: [48.7, 21.2]).map { |r| r[:kind] }
assert_equal ["street", "square"], search("hlavna", center: [48.14, 17.1]).map { |r| r[:kind] }
street = search("hlavna").find { |r| r[:kind] == "street" }
assert_equal [17.10, 48.14, 17.102, 48.142], street[:bbox]
assert_equal 2, search("hlavna").size
end
def test_a_city_counts_as_closer_than_it_is_but_a_shop_next_door_still_wins
# restaurant ~18 km from Bratislava's center point; from right next to
# the restaurant it wins, from halfway the city does (weighted x0.01)
assert_equal "Bratislavská reštaurácia", search("bratisl", center: [48.30, 17.20]).first[:name]
assert_equal "Bratislava", search("bratisl", center: [48.22, 17.15]).first[:name]
end
def test_all_words_must_match_and_unnamed_or_broken_lines_are_skipped
assert_equal ["Bratislavská reštaurácia"], search("bratislavska restau").map { |r| r[:name] }
assert_empty search("crossing")
end
private
def search(text, **opts)
words = text.unicode_normalize(:nfkd).gsub(/\p{Mn}/, "").downcase.split
Maptool::Places.search(@db, words, **opts).map { |r| r.except(:rank) }
end
def feature(type, coordinates, tags)
JSON.generate("type" => "Feature", "geometry" => { "type" => type, "coordinates" => coordinates }, "properties" => tags)
end
end