Search the offline map and Wikipedia, nearest results first
CI / check (push) Successful in 26s
CI / deploy (push) Successful in 33s

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:
randy committed 2026-11-12 12:13:13 +01:00
1 parent ba9c40ac3d
commit 3f973a26d7
14 files changed
+549 -90

No files matched your search

+48
View File
@@ -6,6 +6,7 @@ require "tmpdir"
require "fileutils"
require "etc"
require_relative "hiking_trails"
require_relative "places"
module Maptool
# Regenerates data/offline.pmtiles and data/terrain.pmtiles for the
@@ -367,6 +368,8 @@ module Maptool
report_step.call("extracting hiking trails")
extract_hiking_trails(sources, data_dir)
report_step.call("indexing places")
extract_places(sources, data_dir)
{ log: planetiler_log }
ensure
FileUtils.remove_entry(scratch_dir) if scratch_dir && Dir.exist?(scratch_dir)
@@ -409,6 +412,50 @@ module Maptool
end
private_class_method :extract_hiking_trails
# The place-name search index (Places), one SQLite file per country —
# same per-country, cached-by-mtime, best-effort story as hiking trails
# above, from the same PBFs.
def self.extract_places(sources, data_dir)
FileUtils.mkdir_p(places_dir(data_dir))
sources.each do |source|
out_path = places_country_path(data_dir, source["code"])
next if File.exist?(out_path) && File.mtime(out_path) >= File.mtime(source["path"])
tmp_path = "#{out_path}.tmp"
begin
Places.extract(source["path"], tmp_path)
File.rename(tmp_path, out_path)
rescue Places::ExtractError, SQLite3::Exception => e
File.delete(tmp_path) if File.exist?(tmp_path)
warn "places index failed for #{source["code"]}, skipping: #{e.message}"
end
end
end
private_class_method :extract_places
# Same, for every already-downloaded country, without a whole offline
# build — `rake places:index`, e.g. right after this index was added
# and offline.pmtiles itself was already up to date.
def self.index_cached_places(data_dir)
sources = cached_extracts(data_dir).map do |extract|
extract.merge("path" => File.join(geofabrik_cache_dir(data_dir), "#{extract["code"]}.osm.pbf"))
end
extract_places(sources, data_dir)
places_countries(data_dir)
end
def self.places_dir(data_dir)
File.join(data_dir, "places")
end
def self.places_country_path(data_dir, code)
File.join(places_dir(data_dir), "#{code}.sqlite")
end
def self.places_countries(data_dir)
Dir.glob(File.join(places_dir(data_dir), "*.sqlite")).map { |path| File.basename(path, ".sqlite") }.sort
end
def self.hiking_trails_dir(data_dir)
File.join(data_dir, "hiking_trails")
end
@@ -573,6 +620,7 @@ module Maptool
File.delete(File.join(data_dir, "offline.pmtiles")) if File.exist?(File.join(data_dir, "offline.pmtiles"))
File.delete(offline_meta_path(data_dir)) if File.exist?(offline_meta_path(data_dir))
FileUtils.remove_entry(hiking_trails_dir(data_dir)) if Dir.exist?(hiking_trails_dir(data_dir))
FileUtils.remove_entry(places_dir(data_dir)) if Dir.exist?(places_dir(data_dir))
cache_dir = geofabrik_cache_dir(data_dir)
FileUtils.remove_entry(cache_dir) if Dir.exist?(cache_dir)
end