Routing: merge the countries' extracts with osmium before Valhalla, which fails on overlapping extracts
CI / check (push) Successful in 23s
CI / deploy (push) Successful in 12s

This commit is contained in:
randy committed 2026-11-14 12:11:17 +01:00
1 parent 92efe118b0
commit acf5256c74
4 files changed
+85 -8

No files matched your search

+10 -3
View File
@@ -61,9 +61,14 @@ build() {
mkdir -p "$S" "$R/pbf"
mv "$R/build-request.json" "$S/request.json"
regions=$(python3 -c 'import json,sys; print(" ".join(json.load(open(sys.argv[1]))["regions"]))' "$S/request.json")
for code in $regions; do
ln "/data/geofabrik_cache/$code.osm.pbf" "$R/pbf/" || { status error "" "no downloaded extract for $code"; return 1; }
done
pbf=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("pbf", ""))' "$S/request.json")
if [ -n "$pbf" ]; then # several countries, merged by maptool (Valhalla breaks on overlapping extracts)
ln "/data/$pbf" "$R/pbf/" || { status error "" "missing $pbf"; return 1; }
else
for code in $regions; do
ln "/data/geofabrik_cache/$code.osm.pbf" "$R/pbf/" || { status error "" "no downloaded extract for $code"; return 1; }
done
fi
: > "$R/build.log"
run() { # step command...
status running "$1"
@@ -80,6 +85,8 @@ build() {
config "$R"
mv "$S/request.json" "$R/built.json"
rm -rf "$S" "$R/pbf"
# Not if a newer request (with a freshly merged file) is already waiting.
[ -n "$pbf" ] && [ ! -f "$R/build-request.json" ] && rm -f "/data/$pbf"
serve
status done ""
}
+5 -1
View File
@@ -86,7 +86,11 @@ The two sides talk through files in `data/routing/`:
| `build-request.json`
| maptool
| `{"regions": [...]}`: build these countries.
| `{"regions": [...], "pbf": "routing/merged.osm.pbf"}`: build these countries. `pbf` is set for more than one country.
| `merged.osm.pbf`
| maptool
| The countries' extracts merged by osmium. Valhalla fails on several overlapping extracts ("Failed tile ... _M_range_check", valhalla#3925), and its image has no osmium. Removed after a successful build.
| `status.json`
| container
+41 -3
View File
@@ -1,6 +1,7 @@
require "fileutils"
require "json"
require "net/http"
require "open3"
require "time"
require_relative "pmtiles_builder"
@@ -13,6 +14,7 @@ module Maptool
SERVICE_SCRIPT = File.expand_path("../../bin/routing-service.sh", __dir__)
MUTEX = Mutex.new
@pending = false
@merging = nil # status while osmium merges the extracts, then nil (or the error)
def self.dir(data_dir)
File.join(data_dir, "routing")
@@ -48,10 +50,44 @@ module Maptool
running = read_json(File.join(dir(data_dir), "staging", "request.json"))
return if running && running[:regions] == codes && read_json(File.join(dir(data_dir), "status.json"))&.dig(:state) == "running"
write_json(File.join(dir(data_dir), "build-request.json"), regions: codes, requested_at: Time.now.utc.iso8601)
return if @merging&.dig(:state) == "running"
@merging = nil
if codes.size == 1
write_request(data_dir, codes, nil)
else
@merging = { state: "running", step: "merging extracts with osmium", started_at: Time.now.utc.iso8601 }
Thread.new { merge_and_request(data_dir, codes) }
end
end
end
# Valhalla breaks on several overlapping country extracts (border ways
# in each: "Failed tile ... _M_range_check", valhalla#3925), so it gets
# one merged file. Its image has no osmium; this container does.
def self.merge_and_request(data_dir, codes)
merged = File.join(dir(data_dir), "merged.osm.pbf")
inputs = codes.map { |code| extract_path(data_dir, code) }
stdout, stderr, status = Open3.capture3(PmtilesBuilder.osmium_bin, "merge", "-O", *inputs, "-o", "#{merged}.part")
raise PmtilesBuilder::BuildError, "osmium merge failed: #{(stderr + stdout)[-2000..] || stderr + stdout}" unless status.success?
File.rename("#{merged}.part", merged)
MUTEX.synchronize do
write_request(data_dir, codes, "routing/merged.osm.pbf")
@merging = nil
end
rescue StandardError => e
FileUtils.rm_f("#{merged}.part")
MUTEX.synchronize { @merging = { state: "error", message: e.message, finished_at: Time.now.utc.iso8601 } }
end
private_class_method :merge_and_request
# pbf: the file to build from, relative to data/ (nil: the one extract).
def self.write_request(data_dir, codes, pbf)
write_json(File.join(dir(data_dir), "build-request.json"), { regions: codes, pbf: pbf, requested_at: Time.now.utc.iso8601 }.compact)
end
private_class_method :write_request
def self.offline_build_finished(data_dir)
pending = MUTEX.synchronize { @pending.tap { @pending = false } }
request_build(data_dir) if pending
@@ -62,7 +98,9 @@ module Maptool
# The DataFileRow build-status shape (offline_data.js): state, step,
# started_at, message.
def self.build_status(data_dir)
return { state: "running", step: "waiting for the offline map build" } if MUTEX.synchronize { @pending }
pending, merging = MUTEX.synchronize { [@pending, @merging] }
return { state: "running", step: "waiting for the offline map build" } if pending
return merging if merging
status = read_json(File.join(dir(data_dir), "status.json"))
return status if status&.dig(:state) == "running" # the container is building; a queued request runs next
return { state: "running", step: "waiting for the routing container" } if File.exist?(File.join(dir(data_dir), "build-request.json"))
@@ -227,7 +265,7 @@ module Maptool
end
def self.reset!
MUTEX.synchronize { @pending = false }
MUTEX.synchronize { @pending = false; @merging = nil }
end
end
end
+29 -1
View File
@@ -71,11 +71,27 @@ class RoutingTest < Minitest::Test
# Different countries do queue, and the running build's step still shows.
File.write(File.join(@data, "geofabrik_cache", "CZ.osm.pbf"), "pbf")
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK CZ])
Maptool::Routing.request_build(@data)
with_osmium("cat \"$3\" \"$4\" > \"$6\"") { Maptool::Routing.request_build(@data) }
assert File.exist?(File.join(@routing, "build-request.json"))
assert_equal "routing graph (SK)", Maptool::Routing.build_status(@data)[:step]
end
def test_several_countries_are_merged_into_one_file_for_valhalla
%w[SK CZ].each { |code| File.write(File.join(@data, "geofabrik_cache", "#{code}.osm.pbf"), code) }
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK CZ])
with_osmium("cat \"$3\" \"$4\" > \"$6\"") { Maptool::Routing.request_build(@data) }
request = JSON.parse(File.read(File.join(@routing, "build-request.json")))
assert_equal ["routing/merged.osm.pbf", %w[SK CZ]], request.values_at("pbf", "regions")
assert_equal "SKCZ", File.read(File.join(@data, "routing", "merged.osm.pbf"))
File.delete(File.join(@routing, "build-request.json"))
with_osmium("echo broken >&2; exit 1") { Maptool::Routing.request_build(@data) }
assert_equal "error", Maptool::Routing.build_status(@data)[:state]
assert_includes Maptool::Routing.build_status(@data)[:message], "broken"
refute File.exist?(File.join(@routing, "build-request.json"))
end
def test_the_vehicle_is_validated_and_stored
assert_equal Maptool::Routing::DEFAULT_VEHICLE, Maptool::Routing.vehicle(@data)
Maptool::Routing.save_vehicle(@data, "height" => "3.4", "width" => "2.4", "length" => "8", "weight" => "4.2")
@@ -118,6 +134,18 @@ class RoutingTest < Minitest::Test
private
# A stand-in osmium ($1 merge, $2 -O, $3.. inputs, then -o OUTPUT); waits for the merge thread.
def with_osmium(script)
bin = File.join(@data, "osmium")
File.write(bin, "#!/bin/sh\n#{script}\n")
File.chmod(0o755, bin)
ENV["OSMIUM_BIN"] = bin
yield
sleep 0.02 while Maptool::Routing.build_status(@data)[:step] == "merging extracts with osmium"
ensure
ENV.delete("OSMIUM_BIN")
end
# Answers one HTTP request with `response`; returns what was received.
def serve_once(response)
server = TCPServer.new("127.0.0.1", 0)