Bicycle and foot route profiles (Valhalla bicycle/pedestrian costing); hiking trail colours in the elevation profile; power, splits and best efforts in the profile; TCX import; FIT course export
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 18s

This commit is contained in:
randy committed 2026-11-16 12:18:06 +01:00
1 parent 4c704045c5
commit f8755ea65a
21 files changed
+634 -40

No files matched your search

+39 -11
View File
@@ -162,24 +162,38 @@ module Maptool
class RouteError < StandardError; end
class Unavailable < StandardError; end
# A camper by default; one profile for now (docs/routing-design.adoc).
# Vehicle profiles: a name plus optional limits (nil: no limit, a plain
# car). Stored for everyone, picked per browser (route.js).
# Profiles: a name, a type (car, bicycle, foot) and that type's
# settings, nil meaning Valhalla's default (no limit for a car's size).
# No type: a car.
# Stored for everyone, picked per browser (route.js).
DEFAULT_VEHICLES = [
{ id: "car", name: "Car" },
{ id: "camper", name: "Camper", height: 3.0, width: 2.3, length: 7.0, weight: 3.5 },
].freeze
VEHICLE_LIMITS = { height: 1..6, width: 1..4, length: 2..25, weight: 0.5..60 }.freeze
ACTIVE_VEHICLES = [
{ id: "bike", name: "Bicycle", type: "bicycle", bicycle_type: "Hybrid" },
{ id: "mtb", name: "Mountain bike", type: "bicycle", bicycle_type: "Mountain" },
{ id: "run", name: "Run", type: "foot", speed: 10.0, sac: 2.0 },
{ id: "hike", name: "Hike", type: "foot", speed: 4.5, sac: 3.0 },
].freeze
TYPES = %w[car bicycle foot].freeze
BICYCLE_TYPES = %w[Road Hybrid Cross Mountain].freeze
# speed: km/h (bicycle, foot); hills: 0 avoid .. 1 seek (Valhalla's
# use_hills); sac: the hardest SAC hiking grade allowed on foot.
VEHICLE_LIMITS = { height: 1..6, width: 1..4, length: 2..25, weight: 0.5..60, speed: 1..60, hills: 0..1, sac: 1..6 }.freeze
# Up to this weight Valhalla's "auto" costing (with height/width) fits
# better than "truck": a camper isn't kept off roads closed to lorries.
CAR_MAX_WEIGHT = 3.5
def self.vehicles(data_dir)
read_json(File.join(data_dir, "vehicles.json")) || begin
list = read_json(File.join(data_dir, "vehicles.json")) || begin
# The single vehicle stored before profiles became the camper.
old = read_json(File.join(data_dir, "vehicle.json"))
old ? [DEFAULT_VEHICLES[0], DEFAULT_VEHICLES[1].merge(old.slice(*VEHICLE_LIMITS.keys))] : DEFAULT_VEHICLES
old ? [DEFAULT_VEHICLES[0], DEFAULT_VEHICLES[1].merge(old.slice(:height, :width, :length, :weight))] : DEFAULT_VEHICLES
end
# A list saved before types existed gets the bicycle/foot profiles
# once; every save stores the type, so deleting them sticks.
list.none? { |v| v.key?(:type) } ? list + ACTIVE_VEHICLES : list
end
# The profile with this id, or the first one.
@@ -188,7 +202,7 @@ module Maptool
list.find { |v| v[:id] == id } || list.first
end
# Replaces the whole list: [{ "id", "name", "height", ... }, ...].
# Replaces the whole list: [{ "id", "name", "type", "height", ... }, ...].
def self.save_vehicles(data_dir, list)
raise ArgumentError, "at least one vehicle is needed" unless list.is_a?(Array) && list.any? && list.size <= 20
@@ -197,8 +211,13 @@ module Maptool
name = values["name"].to_s.strip
id = values["id"].to_s
type = values["type"] || "car"
raise ArgumentError, "every vehicle needs a name" if name.empty? || name.size > 40
raise ArgumentError, "invalid vehicle id" unless id.match?(/\A[\w-]{1,40}\z/)
raise ArgumentError, "#{name}: invalid type" unless TYPES.include?(type)
bicycle_type = values["bicycle_type"] if type == "bicycle"
raise ArgumentError, "#{name}: invalid bicycle type" unless bicycle_type.nil? || BICYCLE_TYPES.include?(bicycle_type)
limits = VEHICLE_LIMITS.to_h do |key, range|
raw = values[key.to_s]
@@ -209,7 +228,7 @@ module Maptool
[key, value]
end
{ id: id, name: name, **limits.compact }
{ id: id, name: name, type: type, bicycle_type: bicycle_type, **limits }.compact
end
raise ArgumentError, "vehicle ids must be unique" unless vehicles.map { |v| v[:id] }.uniq.size == vehicles.size
@@ -233,7 +252,7 @@ module Maptool
geometry: { type: "LineString", coordinates: legs.flat_map { |leg| decode_polyline(leg["shape"]) } },
properties: { kind: "route", costing: costing, distance_km: data["trip"]["summary"]["length"],
time_s: data["trip"]["summary"]["time"],
countries: countries(valhalla_url, legs, costing, options),
countries: (countries(valhalla_url, legs, costing, options) if %w[auto truck].include?(costing)),
maneuvers: legs.flat_map { |leg| leg["maneuvers"].map { |m| { text: m["instruction"], km: m["length"] } } } } }
rescue ArgumentError, TypeError
raise RouteError, "invalid coordinates"
@@ -280,9 +299,18 @@ module Maptool
raise RouteError, "invalid center or times"
end
# Up to CAR_MAX_WEIGHT "auto" with height/width, above it "truck".
# bicycle / pedestrian from the profile's own settings; a car is "auto"
# with height/width up to CAR_MAX_WEIGHT, "truck" above it. avoid only
# applies to a car.
def self.costing(vehicle, avoid = {})
limits = vehicle.slice(*VEHICLE_LIMITS.keys).compact
case vehicle[:type]
when "bicycle"
return ["bicycle", { bicycle_type: vehicle[:bicycle_type], cycling_speed: vehicle[:speed], use_hills: vehicle[:hills] }.compact]
when "foot"
return ["pedestrian", { walking_speed: vehicle[:speed], max_hiking_difficulty: vehicle[:sac]&.to_i, use_hills: vehicle[:hills] }.compact]
end
limits = vehicle.slice(:height, :width, :length, :weight).compact
costing, options = limits[:weight].to_f <= CAR_MAX_WEIGHT ? ["auto", limits.slice(:height, :width)] : ["truck", limits]
avoid = {} unless avoid.is_a?(Hash)
options = options.merge(use_tolls: 0.0) if avoid["tolls"]