Profiles in a dialog, the chip in the map's bottom-left corner picks one (the Design canvas' Vehicle): a car gains fuel, Euro class, stickers and vignettes / toll units with dates; drive mode's Menu steps through them. Speed limits by country for cars and over 3.5 t (public/js/speed_limits.js, sources in it): the route's roads (km, road class, country), the limit shown the lower of the sign and the profile's; the Route panel's countries with their limits and how the motorways are paid; past a border a card in drive mode (GridBorder). The itinerary in the Route panel (Itinerary): the trip's name, leave time, days start; per stop a night, stay and note, planned times, day headers; another screen's change shown where it happened with Undo
This commit is contained in:
1 parent
a6158f4083
commit
5e31dfaba9
15 files changed
+733
-169
No files matched your search
+43
-6
@@ -177,6 +177,11 @@ module Maptool
|
||||
{ id: "hike", name: "Hike", type: "foot", speed: 4.5, sac: 3.0 },
|
||||
].freeze
|
||||
TYPES = %w[car bicycle foot].freeze
|
||||
# A car's fuel and emissions (low-emission zones) and what it has paid
|
||||
# for: vignettes / toll units per country, until a date (YYYY-MM-DD).
|
||||
FUELS = %w[petrol diesel lpg cng electric hybrid].freeze
|
||||
EUROS = %w[3 4 5 6 6d].freeze
|
||||
PAYMENT_KINDS = %w[vignette toll].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.
|
||||
@@ -228,7 +233,8 @@ module Maptool
|
||||
|
||||
[key, value]
|
||||
end
|
||||
{ id: id, name: name, type: type, bicycle_type: bicycle_type, **limits }.compact
|
||||
car = type == "car" ? car_details(name, values) : {}
|
||||
{ id: id, name: name, type: type, bicycle_type: bicycle_type, **limits, **car }.compact
|
||||
end
|
||||
raise ArgumentError, "vehicle ids must be unique" unless vehicles.map { |v| v[:id] }.uniq.size == vehicles.size
|
||||
|
||||
@@ -236,6 +242,30 @@ module Maptool
|
||||
vehicles
|
||||
end
|
||||
|
||||
def self.car_details(name, values)
|
||||
fuel = values["fuel"].to_s.empty? ? nil : values["fuel"].to_s
|
||||
euro = values["euro"].to_s.empty? ? nil : values["euro"].to_s
|
||||
raise ArgumentError, "#{name}: invalid fuel" unless fuel.nil? || FUELS.include?(fuel)
|
||||
raise ArgumentError, "#{name}: invalid Euro class" unless euro.nil? || EUROS.include?(euro)
|
||||
|
||||
stickers = values["stickers"].to_s.strip[0, 80]
|
||||
list = values["vignettes"] || []
|
||||
raise ArgumentError, "#{name}: invalid vignettes" unless list.is_a?(Array) && list.size <= 30
|
||||
|
||||
vignettes = list.map do |v|
|
||||
country = v.is_a?(Hash) && v["country"].to_s.upcase
|
||||
kind = v.is_a?(Hash) ? (v["kind"] || "vignette").to_s : nil
|
||||
until_date = v.is_a?(Hash) ? v["until"].to_s : ""
|
||||
raise ArgumentError, "#{name}: a vignette needs a country code" unless country&.match?(/\A[A-Z]{2}\z/)
|
||||
raise ArgumentError, "#{name}: invalid vignette kind" unless PAYMENT_KINDS.include?(kind)
|
||||
raise ArgumentError, "#{name}: invalid date #{until_date}" unless until_date.empty? || until_date.match?(/\A\d{4}-\d{2}-\d{2}\z/)
|
||||
|
||||
{ country: country, kind: kind, until: (until_date unless until_date.empty?) }.compact
|
||||
end
|
||||
{ fuel: fuel, euro: euro, stickers: (stickers unless stickers.empty?), vignettes: (vignettes unless vignettes.empty?) }
|
||||
end
|
||||
private_class_method :car_details
|
||||
|
||||
# [[lon, lat], ...] → a GeoJSON Feature (the route line) with distance
|
||||
# (km), time (s), the turn-by-turn instructions and, per country, the
|
||||
# km on motorways and toll roads (for vignettes and tolls).
|
||||
@@ -253,7 +283,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: along[:countries],
|
||||
speed_limits: along[:speed_limits], signals: along[:signals],
|
||||
speed_limits: along[:speed_limits], signals: along[:signals], roads: along[:roads],
|
||||
legs_km: legs.map { |leg| leg.dig("summary", "length") }, # to each via point, for rerouting
|
||||
legs_s: legs.map { |leg| leg.dig("summary", "time") }, # the time of each, for the stops list
|
||||
# lanes: Valhalla's [{directions:, valid:, active:}] bitmasks, at junctions that have them
|
||||
@@ -275,12 +305,15 @@ module Maptool
|
||||
# order (vignettes, tolls);
|
||||
# speed_limits: [[km from the start, km/h | "unlimited" | nil], ...], an
|
||||
# entry where the limit changes (nil: not tagged);
|
||||
# signals: [[lon, lat, km from the start], ...], the traffic signals.
|
||||
# signals: [[lon, lat, km from the start], ...], the traffic signals;
|
||||
# roads: [[km from the start, road class, country code], ...], an entry
|
||||
# where either changes (a vehicle's limits by country and road class).
|
||||
# nil if that lookup fails: it's extra.
|
||||
def self.along_route(valhalla_url, legs, costing, options)
|
||||
totals = {}
|
||||
limits = []
|
||||
signals = []
|
||||
roads = []
|
||||
km = 0.0
|
||||
legs.each do |leg|
|
||||
shape = decode_polyline(leg["shape"])
|
||||
@@ -298,13 +331,15 @@ module Maptool
|
||||
row[:toll_km] += edge["length"] if edge["toll"]
|
||||
limit = speed_limit(edge["speed_limit"])
|
||||
limits << [km.round(3), limit] if limits.empty? || limits.last[1] != limit
|
||||
road = [km.round(3), edge["road_class"], code]
|
||||
roads << road if roads.empty? || roads.last.drop(1) != road.drop(1)
|
||||
km += edge["length"]
|
||||
point = shape[edge["end_shape_index"].to_i] if edge.dig("end_node", "traffic_signal")
|
||||
signals << [*point, km.round(3)] if point
|
||||
end
|
||||
end
|
||||
{ countries: totals.values.each { |row| row.transform_values! { |v| v.is_a?(Float) ? v.round(1) : v } },
|
||||
speed_limits: limits, signals: signals }
|
||||
speed_limits: limits, signals: signals, roads: roads }
|
||||
rescue RouteError, Unavailable, Net::ReadTimeout, NoMethodError, TypeError
|
||||
nil
|
||||
end
|
||||
@@ -326,9 +361,11 @@ module Maptool
|
||||
|
||||
data = valhalla(valhalla_url, "/trace_attributes",
|
||||
shape: points.map { |lon, lat| { lon: Float(lon), lat: Float(lat) } }, costing: "auto", shape_match: "map_snap",
|
||||
filters: { attributes: %w[edge.names edge.road_class edge.speed_limit edge.surface], action: "include" })
|
||||
filters: { attributes: %w[edge.names edge.road_class edge.speed_limit edge.surface node.admin_index admin.country_code],
|
||||
action: "include" })
|
||||
edge = data["edges"]&.last or raise RouteError, "no road here"
|
||||
{ name: edge["names"]&.first, road_class: edge["road_class"], speed_limit: speed_limit(edge["speed_limit"]), surface: edge["surface"],
|
||||
country = data["admins"]&.dig(edge.dig("end_node", "admin_index").to_i, "country_code")
|
||||
{ name: edge["names"]&.first, road_class: edge["road_class"], speed_limit: speed_limit(edge["speed_limit"]), surface: edge["surface"], country: country,
|
||||
signals: signals_ahead(valhalla_url, points) }.compact
|
||||
rescue ArgumentError, TypeError
|
||||
raise RouteError, "invalid coordinates"
|
||||
|
||||
+29
-3
@@ -10,6 +10,29 @@ module Maptool
|
||||
# camera. Kept in data/trip.json across restarts.
|
||||
class TripStore
|
||||
EMPTY = { version: 0, points: [], vehicle: nil, avoid: {}, editing: nil, route: nil, error: nil }.freeze
|
||||
# The itinerary: the trip's name, when it starts (local "YYYY-MM-DDTHH:MM")
|
||||
# and when a day after a night stop starts ("HH:MM"); per point a night
|
||||
# stop (the day ends there), the stay in minutes and a note.
|
||||
MAX_STAY_MIN = 7 * 24 * 60
|
||||
DATE_TIME = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}\z/
|
||||
TIME = /\A\d{2}:\d{2}\z/
|
||||
|
||||
def self.point(p)
|
||||
stay = p[:stay].nil? || p[:stay].to_s.empty? ? nil : Integer(p[:stay], exception: false)
|
||||
raise ArgumentError, "stay must be 0–#{MAX_STAY_MIN} minutes" if stay && !(0..MAX_STAY_MIN).cover?(stay)
|
||||
|
||||
note = p[:note].to_s.strip[0, 500]
|
||||
{ lngLat: p.fetch(:lngLat).map { |v| Float(v) }, name: p[:name], nav: !!p[:nav], passed: !!p[:passed],
|
||||
night: (true if p[:night]), stay: (stay if stay&.positive?), note: (note unless note.empty?) }.compact
|
||||
end
|
||||
|
||||
def self.itinerary(name:, start:, day_start:)
|
||||
name = name.to_s.strip[0, 80]
|
||||
raise ArgumentError, "start must be YYYY-MM-DDTHH:MM" unless start.to_s.empty? || start.to_s.match?(DATE_TIME)
|
||||
raise ArgumentError, "day_start must be HH:MM" unless day_start.to_s.empty? || day_start.to_s.match?(TIME)
|
||||
|
||||
{ name: (name unless name.empty?), start: (start unless start.to_s.empty?), day_start: (day_start unless day_start.to_s.empty?) }
|
||||
end
|
||||
|
||||
def initialize(path)
|
||||
@path = path
|
||||
@@ -117,8 +140,9 @@ module Maptool
|
||||
# changed — the old route stays then. Passed points stay in the trip
|
||||
# (shown dimmed, still editable: a stop missed out added afterwards),
|
||||
# just not routed. Last write wins.
|
||||
def update(points:, vehicle:, avoid:, editing:)
|
||||
points = points.map { |p| { lngLat: p.fetch(:lngLat).map { |v| Float(v) }, name: p[:name], nav: !!p[:nav], passed: !!p[:passed] } }
|
||||
def update(points:, vehicle:, avoid:, editing:, name: nil, start: nil, day_start: nil)
|
||||
points = points.map { |p| self.class.point(p) }
|
||||
itinerary = self.class.itinerary(name: name, start: start, day_start: day_start)
|
||||
avoid = (avoid || {}).to_h { |k, v| [k.to_sym, !!v] }
|
||||
before = get
|
||||
routed = ->(list) { list.reject { |p| p[:passed] }.map { |p| p[:lngLat] } }
|
||||
@@ -135,7 +159,9 @@ module Maptool
|
||||
end
|
||||
@mutex.synchronize do
|
||||
@trip = { version: @trip[:version] + 1, points: points, vehicle: vehicle, avoid: avoid, editing: editing,
|
||||
route: route, error: error, updated_at: Time.now.to_f }
|
||||
**itinerary, route: route, error: error, updated_at: Time.now.to_f }.compact
|
||||
@trip[:route] = route # nil kept: the client reads it
|
||||
@trip[:error] = error
|
||||
File.write("#{@path}.tmp", JSON.generate(@trip))
|
||||
File.rename("#{@path}.tmp", @path)
|
||||
@trip.dup
|
||||
|
||||
Reference in new issue
Block a user