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
+41
-3
@@ -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
|
||||
Reference in new issue
Block a user