Routing: merge the countries' extracts with osmium before Valhalla, which fails on overlapping extracts
This commit is contained in:
1 parent
92efe118b0
commit
acf5256c74
4 files changed
+85
-8
No files matched your search
+10
-3
@@ -61,9 +61,14 @@ build() {
|
|||||||
mkdir -p "$S" "$R/pbf"
|
mkdir -p "$S" "$R/pbf"
|
||||||
mv "$R/build-request.json" "$S/request.json"
|
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")
|
regions=$(python3 -c 'import json,sys; print(" ".join(json.load(open(sys.argv[1]))["regions"]))' "$S/request.json")
|
||||||
for code in $regions; do
|
pbf=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("pbf", ""))' "$S/request.json")
|
||||||
ln "/data/geofabrik_cache/$code.osm.pbf" "$R/pbf/" || { status error "" "no downloaded extract for $code"; return 1; }
|
if [ -n "$pbf" ]; then # several countries, merged by maptool (Valhalla breaks on overlapping extracts)
|
||||||
done
|
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"
|
: > "$R/build.log"
|
||||||
run() { # step command...
|
run() { # step command...
|
||||||
status running "$1"
|
status running "$1"
|
||||||
@@ -80,6 +85,8 @@ build() {
|
|||||||
config "$R"
|
config "$R"
|
||||||
mv "$S/request.json" "$R/built.json"
|
mv "$S/request.json" "$R/built.json"
|
||||||
rm -rf "$S" "$R/pbf"
|
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
|
serve
|
||||||
status done ""
|
status done ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,7 +86,11 @@ The two sides talk through files in `data/routing/`:
|
|||||||
|
|
||||||
| `build-request.json`
|
| `build-request.json`
|
||||||
| maptool
|
| 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`
|
| `status.json`
|
||||||
| container
|
| container
|
||||||
|
|||||||
+41
-3
@@ -1,6 +1,7 @@
|
|||||||
require "fileutils"
|
require "fileutils"
|
||||||
require "json"
|
require "json"
|
||||||
require "net/http"
|
require "net/http"
|
||||||
|
require "open3"
|
||||||
require "time"
|
require "time"
|
||||||
require_relative "pmtiles_builder"
|
require_relative "pmtiles_builder"
|
||||||
|
|
||||||
@@ -13,6 +14,7 @@ module Maptool
|
|||||||
SERVICE_SCRIPT = File.expand_path("../../bin/routing-service.sh", __dir__)
|
SERVICE_SCRIPT = File.expand_path("../../bin/routing-service.sh", __dir__)
|
||||||
MUTEX = Mutex.new
|
MUTEX = Mutex.new
|
||||||
@pending = false
|
@pending = false
|
||||||
|
@merging = nil # status while osmium merges the extracts, then nil (or the error)
|
||||||
|
|
||||||
def self.dir(data_dir)
|
def self.dir(data_dir)
|
||||||
File.join(data_dir, "routing")
|
File.join(data_dir, "routing")
|
||||||
@@ -48,10 +50,44 @@ module Maptool
|
|||||||
running = read_json(File.join(dir(data_dir), "staging", "request.json"))
|
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"
|
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
|
||||||
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)
|
def self.offline_build_finished(data_dir)
|
||||||
pending = MUTEX.synchronize { @pending.tap { @pending = false } }
|
pending = MUTEX.synchronize { @pending.tap { @pending = false } }
|
||||||
request_build(data_dir) if pending
|
request_build(data_dir) if pending
|
||||||
@@ -62,7 +98,9 @@ module Maptool
|
|||||||
# The DataFileRow build-status shape (offline_data.js): state, step,
|
# The DataFileRow build-status shape (offline_data.js): state, step,
|
||||||
# started_at, message.
|
# started_at, message.
|
||||||
def self.build_status(data_dir)
|
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"))
|
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 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"))
|
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
|
end
|
||||||
|
|
||||||
def self.reset!
|
def self.reset!
|
||||||
MUTEX.synchronize { @pending = false }
|
MUTEX.synchronize { @pending = false; @merging = nil }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
+29
-1
@@ -71,11 +71,27 @@ class RoutingTest < Minitest::Test
|
|||||||
# Different countries do queue, and the running build's step still shows.
|
# Different countries do queue, and the running build's step still shows.
|
||||||
File.write(File.join(@data, "geofabrik_cache", "CZ.osm.pbf"), "pbf")
|
File.write(File.join(@data, "geofabrik_cache", "CZ.osm.pbf"), "pbf")
|
||||||
Maptool::PmtilesBuilder.set_selected_regions(@data, %w[SK CZ])
|
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 File.exist?(File.join(@routing, "build-request.json"))
|
||||||
assert_equal "routing graph (SK)", Maptool::Routing.build_status(@data)[:step]
|
assert_equal "routing graph (SK)", Maptool::Routing.build_status(@data)[:step]
|
||||||
end
|
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
|
def test_the_vehicle_is_validated_and_stored
|
||||||
assert_equal Maptool::Routing::DEFAULT_VEHICLE, Maptool::Routing.vehicle(@data)
|
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")
|
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
|
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.
|
# Answers one HTTP request with `response`; returns what was received.
|
||||||
def serve_once(response)
|
def serve_once(response)
|
||||||
server = TCPServer.new("127.0.0.1", 0)
|
server = TCPServer.new("127.0.0.1", 0)
|
||||||
|
|||||||
Reference in new issue
Block a user