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.
This commit is contained in:
1 parent
ba9c40ac3d
commit
3f973a26d7
14 files changed
+549
-90
No files matched your search
@@ -124,4 +124,44 @@ class KiwixTest < Minitest::Test
|
||||
geojson = Maptool::Kiwix.nearby("http://127.0.0.1:1", [48.0, 17.0, 48.2, 17.3], open_timeout: 1)
|
||||
assert_equal({ "type" => "FeatureCollection", "features" => [] }, geojson)
|
||||
end
|
||||
|
||||
def test_search_returns_every_hit_with_its_position_when_it_has_one
|
||||
server = TCPServer.new("127.0.0.1", 0)
|
||||
port = server.addr[1]
|
||||
search_line = nil
|
||||
thread = Thread.new do
|
||||
3.times do
|
||||
client = server.accept
|
||||
request_line = client.gets
|
||||
body =
|
||||
if request_line.include?("/kiwix/search")
|
||||
search_line = request_line
|
||||
<<~XML
|
||||
<rss><channel>
|
||||
<item><title>Devín</title><link>/kiwix/content/wikipedia/Dev%C3%ADn</link></item>
|
||||
<item><title>Pivo</title><link>/kiwix/content/wikipedia/Pivo</link></item>
|
||||
</channel></rss>
|
||||
XML
|
||||
elsif request_line.include?("/Dev")
|
||||
'<meta name="geo.position" content="48.17;16.98">'
|
||||
else
|
||||
"no coordinates here"
|
||||
end
|
||||
client.print("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}")
|
||||
client.close
|
||||
end
|
||||
end
|
||||
|
||||
results = Maptool::Kiwix.search("http://127.0.0.1:#{port}", "hrad devín", limit: 5)
|
||||
thread.join
|
||||
server.close
|
||||
|
||||
assert_includes search_line, "pattern=hrad+dev%C3%ADn"
|
||||
assert_equal [["Devín", [16.98, 48.17]], ["Pivo", nil]], results.map { |r| r.values_at(:title, :position) }
|
||||
end
|
||||
|
||||
def test_search_is_empty_without_a_kiwix_url_or_query
|
||||
assert_empty Maptool::Kiwix.search("", "x")
|
||||
assert_empty Maptool::Kiwix.search("http://127.0.0.1:1", " ")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,68 @@
|
||||
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
|
||||
@@ -209,7 +209,7 @@ class PmtilesBuilderTest < Minitest::Test
|
||||
assert_path_exists File.join(@data_dir, "offline.pmtiles")
|
||||
refute_path_exists File.join(@data_dir, "offline.tmp.pmtiles")
|
||||
assert_equal "done", Maptool::PmtilesBuilder.offline_status[:state]
|
||||
assert_equal ["planetiler --osm-path", "osmium tags-filter", "osmium add-locations-to-ways", "osmium cat"], log_lines
|
||||
assert_equal ["planetiler --osm-path", "osmium tags-filter", "osmium add-locations-to-ways", "osmium cat", "osmium tags-filter", "osmium export"], log_lines
|
||||
assert_equal(
|
||||
{ "sources" => [{ "code" => "SK", "slug" => "europe/slovakia", "last_modified" => "Wed, 10 Sep 2026 08:00:01 GMT" }] },
|
||||
Maptool::PmtilesBuilder.offline_meta(@data_dir),
|
||||
@@ -225,7 +225,7 @@ class PmtilesBuilderTest < Minitest::Test
|
||||
failed, done = Maptool::PmtilesBuilder.build_history(@data_dir)
|
||||
assert_equal ["error", "done"], [failed["state"], done["state"]]
|
||||
assert_equal ["offline", ["SK"]], done.values_at("kind", "regions")
|
||||
assert_equal ["fetching SK (1/1)", "tiling with planetiler", "extracting hiking trails"], done["timings"].map { |t| t["step"] }
|
||||
assert_equal ["fetching SK (1/1)", "tiling with planetiler", "extracting hiking trails", "indexing places"], done["timings"].map { |t| t["step"] }
|
||||
assert(done["timings"].all? { |t| t["seconds"] >= 0 })
|
||||
assert done.key?("log")
|
||||
assert_match(/boom/, failed["message"])
|
||||
@@ -261,12 +261,14 @@ class PmtilesBuilderTest < Minitest::Test
|
||||
# since the first build), so only AT's hiking trails get (re-)extracted
|
||||
# — SK.osm.pbf itself is also skipped by cached_extract's own
|
||||
# Last-Modified check, "osmium merge" runs regardless since two
|
||||
# countries are now selected.
|
||||
# countries are now selected. Same for the places index.
|
||||
assert_equal(
|
||||
["osmium merge", "planetiler --osm-path", "osmium tags-filter", "osmium add-locations-to-ways", "osmium cat"],
|
||||
["osmium merge", "planetiler --osm-path", "osmium tags-filter", "osmium add-locations-to-ways", "osmium cat",
|
||||
"osmium tags-filter", "osmium export"],
|
||||
log_lines,
|
||||
)
|
||||
assert_equal %w[AT SK], Maptool::PmtilesBuilder.hiking_trails_countries(@data_dir)
|
||||
assert_equal %w[AT SK], Maptool::PmtilesBuilder.places_countries(@data_dir)
|
||||
end
|
||||
|
||||
# The actual reasoning: hiking trails are a bonus overlay bolted onto the
|
||||
@@ -297,6 +299,8 @@ class PmtilesBuilderTest < Minitest::Test
|
||||
"osmium merge", "planetiler --osm-path",
|
||||
"osmium tags-filter", "osmium add-locations-to-ways", "osmium cat", # SK's own hiking-trails extraction
|
||||
"osmium tags-filter", "osmium add-locations-to-ways", "osmium cat", # AT's own
|
||||
"osmium tags-filter", "osmium export", # SK's places index
|
||||
"osmium tags-filter", "osmium export", # AT's
|
||||
],
|
||||
log_lines,
|
||||
)
|
||||
|
||||
@@ -35,6 +35,17 @@ class SearchTest < Minitest::Test
|
||||
assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox]
|
||||
end
|
||||
|
||||
def test_with_a_map_center_results_come_nearest_first_with_their_distance
|
||||
camps = { "type" => "FeatureCollection", "features" => [
|
||||
point("Kemp far", 21.0, 48.7), point("Kemp near", 17.1, 48.15),
|
||||
] }
|
||||
@store.save(folder: "", filename: "more.geojson", io: StringIO.new(JSON.generate(camps)))
|
||||
|
||||
results = search("kemp", center: [48.14, 17.10])
|
||||
assert_equal ["Kemp near", "Kemp far", "Kemp Košice"], results.map { |r| r[:name] }
|
||||
assert_in_delta 1.3, results.first[:distance_km], 0.2
|
||||
end
|
||||
|
||||
def test_source_filter_and_blank_query
|
||||
assert_empty search("snp", sources: ["layers"])
|
||||
assert_empty search(" ")
|
||||
|
||||
Reference in new issue
Block a user