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