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:
1 parent
2611713489
commit
0051a0337c
10 files changed
+316
-21
No files matched your search
@@ -0,0 +1,103 @@
|
||||
require "json"
|
||||
require_relative "gpx"
|
||||
require_relative "fit"
|
||||
require_relative "pmtiles_builder"
|
||||
|
||||
module Maptool
|
||||
# Server-side half of the search box (public/js/search.js): looks through
|
||||
# every saved layer, whether it's currently shown on the map or not, and
|
||||
# every offline hiking-trails country. Online providers (Nominatim) are
|
||||
# queried by the browser itself, so this route keeps working with no
|
||||
# uplink at all.
|
||||
#
|
||||
# Each source is a name -> a list of [label, ref, file on disk, reader]
|
||||
# entries (ref is what the browser needs to switch that layer on).
|
||||
# Adding one (e.g. an offline place-name index pulled from the Geofabrik
|
||||
# PBF) means adding a key to SOURCES and nothing else.
|
||||
module Search
|
||||
LIMIT = 50
|
||||
|
||||
SOURCES = {
|
||||
"layers" => lambda do |gpx_store, _data_dir|
|
||||
files(gpx_store.tree).map do |path|
|
||||
[path, path, gpx_store.resolve_file(path), -> { gpx_store.geojson(path) }]
|
||||
end
|
||||
end,
|
||||
"hiking_trails" => lambda do |_gpx_store, data_dir|
|
||||
PmtilesBuilder.hiking_trails_countries(data_dir).map do |code|
|
||||
path = PmtilesBuilder.hiking_trails_country_path(data_dir, code)
|
||||
["Hiking trails #{code}", code, path, -> { File.read(path) }]
|
||||
end
|
||||
end,
|
||||
}.freeze
|
||||
|
||||
# path => [mtime, entries] — a country's hiking-trails file runs to tens
|
||||
# of MB, so it's parsed once per change, not once per keystroke.
|
||||
# ponytail: unbounded in-memory cache, fine for a handful of files; an
|
||||
# on-disk/SQLite FTS index once the offline place-name source lands.
|
||||
@cache = {}
|
||||
|
||||
def self.query(text, gpx_store:, data_dir:, sources: SOURCES.keys)
|
||||
words = normalize(text).split
|
||||
return [] if words.empty?
|
||||
|
||||
results = []
|
||||
(sources & SOURCES.keys).each do |source|
|
||||
SOURCES[source].call(gpx_store, data_dir).each do |label, ref, disk_path, read|
|
||||
entries(disk_path, read).each do |entry|
|
||||
next unless words.all? { |word| entry[:text].include?(word) }
|
||||
|
||||
results << entry.except(:text).merge(source: source, source_label: label, ref: ref)
|
||||
return results if results.size >= LIMIT
|
||||
end
|
||||
end
|
||||
end
|
||||
results
|
||||
end
|
||||
|
||||
# Lowercased, diacritics stripped — "kosice" finds "Košice".
|
||||
def self.normalize(text)
|
||||
text.to_s.unicode_normalize(:nfkd).gsub(/\p{Mn}/, "").downcase
|
||||
end
|
||||
|
||||
def self.entries(path, read)
|
||||
mtime = File.mtime(path)
|
||||
cached = @cache[path]
|
||||
return cached[1] if cached && cached[0] == mtime
|
||||
|
||||
entries = begin
|
||||
JSON.parse(read.call).fetch("features", []).filter_map { |feature| entry(feature, File.basename(path)) }
|
||||
rescue JSON::ParserError, Gpx::ParseError, Fit::ParseError, Errno::ENOENT
|
||||
[]
|
||||
end
|
||||
@cache[path] = [mtime, entries]
|
||||
entries
|
||||
end
|
||||
|
||||
def self.entry(feature, fallback_name)
|
||||
coords = feature.dig("geometry", "coordinates")
|
||||
return nil unless coords.is_a?(Array) && !coords.empty?
|
||||
|
||||
props = feature["properties"] || {}
|
||||
name = props["name"].to_s.strip
|
||||
name = fallback_name if name.empty?
|
||||
lngs, lats = positions(coords).map { |p| p.first(2) }.transpose
|
||||
{
|
||||
name: name,
|
||||
kind: props["kind"] || feature.dig("geometry", "type"),
|
||||
bbox: [lngs.min, lats.min, lngs.max, lats.max],
|
||||
text: normalize("#{name} #{props["description"]}"),
|
||||
}
|
||||
end
|
||||
|
||||
def self.positions(coords)
|
||||
coords.first.is_a?(Numeric) ? [coords] : coords.flat_map { |c| positions(c) }
|
||||
end
|
||||
|
||||
def self.files(node)
|
||||
(node["children"] || []).flat_map do |child|
|
||||
child["type"] == "folder" ? files(child) : [child["path"]]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user