Search: look through all saved layers and offline hiking trails

GET /api/search (lib/maptool/search.rb) matches feature name and
description, case- and diacritics-insensitive, across every file in
Tracks & Points (shown or not) and every offline hiking-trails country.
Parsed features are cached in memory per file by mtime.

The search box now queries local sources and Nominatim side by side,
labels each result with where it was found, lets you untick sources,
and switches a result's layer on if it was off before zooming to it.
This commit is contained in:
randy committed 2026-11-12 11:37:42 +01:00
1 parent 2611713489
commit 0051a0337c
10 files changed
+316 -21

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
require_relative "test_helper"
require "maptool/gpx_store"
require "maptool/search"
require "stringio"
class SearchTest < Minitest::Test
def setup
@data_dir = Dir.mktmpdir
@store = Maptool::GpxStore.new(File.join(@data_dir, "gpx"))
camp = { "type" => "FeatureCollection", "features" => [
point("Kemp Košice", 21.26, 48.72, "pitná voda"),
{ "type" => "Feature", "properties" => { "kind" => "track" },
"geometry" => { "type" => "LineString", "coordinates" => [[17.0, 48.0, 150], [18.0, 49.0, 300]] } },
] }
@store.save(folder: "trips", filename: "camps.geojson", io: StringIO.new(JSON.generate(camp)))
trails = File.join(@data_dir, "hiking_trails")
FileUtils.mkdir_p(trails)
File.write(File.join(trails, "SK.geojson"),
JSON.generate("type" => "FeatureCollection", "features" => [point("Cesta hrdinov SNP", 19.0, 48.8)]))
end
def test_finds_across_sources_ignoring_case_and_diacritics_and_says_where
layer = search("kosice")
assert_equal [["Kemp Košice", "layers", "trips/camps.geojson"]], layer.map { |r| r.values_at(:name, :source, :ref) }
trail = search("snp hrdinov").first
assert_equal ["hiking_trails", "SK", "Hiking trails SK"], trail.values_at(:source, :ref, :source_label)
end
def test_matches_description_and_falls_back_to_file_name_with_bbox
assert_equal ["Kemp Košice"], search("voda").map { |r| r[:name] }
track = search("camps").first
assert_equal [17.0, 48.0, 18.0, 49.0], track[:bbox]
end
def test_source_filter_and_blank_query
assert_empty search("snp", sources: ["layers"])
assert_empty search(" ")
end
private
def search(text, **opts)
Maptool::Search.query(text, gpx_store: @store, data_dir: @data_dir, **opts)
end
def point(name, lng, lat, description = nil)
{ "type" => "Feature", "geometry" => { "type" => "Point", "coordinates" => [lng, lat] },
"properties" => { "name" => name, "description" => description }.compact }
end
end