Files

100 lines
4.1 KiB
Ruby

require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test" << "lib"
t.test_files = FileList["test/test_*.rb"]
end
task default: :test
namespace :overpass do
desc "Fetch camper-relevant POIs (water, dump stations, LPG, toilets, laundry, shops, springs) " \
"for a bbox from Overpass and write GeoJSON. Usage: rake overpass:pois[south,west,north,east,out.geojson]"
task :pois, [:south, :west, :north, :east, :out] do |_t, args|
%i[south west north east out].each { |key| abort("usage: rake overpass:pois[south,west,north,east,out.geojson]") unless args[key] }
require_relative "lib/maptool/overpass_pois"
bbox = [args[:south], args[:west], args[:north], args[:east]].map(&:to_f)
puts "Querying Overpass for #{bbox.join(",")}..."
response = Maptool::OverpassPois.fetch(bbox)
geojson = Maptool::OverpassPois.to_geojson(response)
File.write(args[:out], JSON.generate(geojson))
puts "Wrote #{geojson["features"].size} POIs to #{args[:out]}"
end
end
namespace :hiking do
desc "Extract route=hiking relations (name/colour/network/operator, full geometry) from a local " \
".osm.pbf (e.g. one of PmtilesBuilder's own data/geofabrik_cache/*.osm.pbf) and write GeoJSON. " \
"Prototype — see lib/maptool/hiking_trails.rb. Usage: rake hiking:trails[input.osm.pbf,out.geojson]"
task :trails, %i[pbf out] do |_t, args|
%i[pbf out].each { |key| abort("usage: rake hiking:trails[input.osm.pbf,out.geojson]") unless args[key] }
require_relative "lib/maptool/hiking_trails"
puts "Extracting hiking routes from #{args[:pbf]}..."
geojson = Maptool::HikingTrails.extract(args[:pbf])
File.write(args[:out], JSON.generate(geojson))
puts "Wrote #{geojson["features"].size} route(s) to #{args[:out]}"
end
end
namespace :places do
desc "Build the offline place-search index (data/places/<CODE>.sqlite) from every already-downloaded " \
"Geofabrik extract in data/geofabrik_cache, without a full offline build. Countries whose index is " \
"already newer than their extract are skipped. Usage: rake places:index [MAPTOOL_DATA_DIR=data]"
task :index do
require_relative "lib/maptool/pmtiles_builder"
data_dir = ENV.fetch("MAPTOOL_DATA_DIR", File.join(__dir__, "data"))
puts "Indexing places from #{data_dir}/geofabrik_cache..."
countries = Maptool::PmtilesBuilder.index_cached_places(data_dir)
puts "Place index available for: #{countries.empty? ? "(none)" : countries.join(", ")}"
end
end
namespace :library do
desc "Migrate data/gpx/ files into the library (runs on first start anyway; idempotent)"
task :migrate do
require_relative "lib/maptool/library"
data_dir = ENV.fetch("MAPTOOL_DATA_DIR") { File.join(__dir__, "data") }
library = Maptool::Library.new(File.join(data_dir, "library.sqlite"),
gpx_store: Maptool::GpxStore.new(File.join(data_dir, "gpx")),
sources_dir: File.join(data_dir, "library", "sources"))
library.migrate
puts "Library at rev #{library.rev}"
end
end
namespace :gpx do
desc "Backfill properties.id on Note-category features saved before edit/delete (and its id targeting) existed"
task :backfill_note_ids do
require "json"
require "securerandom"
data_dir = ENV.fetch("MAPTOOL_DATA_DIR") { File.join(__dir__, "data") }
count = 0
Dir.glob(File.join(data_dir, "gpx", "**", "*.geojson")).each do |file|
# ".gpx.geojson"/".fit.geojson" are GpxStore's own cache sidecars for
# an uploaded GPX/FIT (regenerated from the source on demand, see
# GpxStore#geojson) — never hand-drawn notes, not worth touching.
next if file.end_with?(".gpx.geojson", ".fit.geojson")
collection = JSON.parse(File.read(file))
changed = false
(collection["features"] || []).each do |feature|
properties = (feature["properties"] ||= {})
next unless properties["category"] == "Note" && !properties["id"]
properties["id"] = SecureRandom.uuid
changed = true
count += 1
end
File.write(file, JSON.generate(collection)) if changed
end
puts "Backfilled #{count} note id(s)"
end
end