Tracks and routes as containers of points: listed (collapsed) under them in the tree, added via a point's popup, a new note or the route planner's navigation pins, and written as course points into the FIT course
This commit is contained in:
1 parent
6aa7b34f92
commit
4265421457
10 files changed
+354
-20
No files matched your search
@@ -18,10 +18,23 @@ module Maptool
|
||||
[7, 0x86, "L<"], [8, 0x86, "L<"], [9, 0x86, "L<"]]].freeze
|
||||
EVENT = [21, [[253, 0x86, "L<"], [0, 0x00, "C"], [1, 0x00, "C"]]].freeze
|
||||
RECORD = [20, [[253, 0x86, "L<"], [0, 0x85, "l<"], [1, 0x85, "l<"], [2, 0x84, "S<"], [5, 0x86, "L<"]]].freeze
|
||||
COURSE_POINT = [32, [[254, 0x84, "S<"], [1, 0x86, "L<"], [2, 0x85, "l<"], [3, 0x85, "l<"], [4, 0x86, "L<"], [5, 0x00, "C"], [6, 0x07, "a16"]]].freeze
|
||||
DEFAULT_SPEED = 4.0 # m/s, when the line has no time of its own
|
||||
# FIT course_point types, guessed from a point's name/category/icon
|
||||
# (English and Slovak words); anything else is generic (0).
|
||||
POINT_TYPES = [
|
||||
[3, /water|spring|fountain|drink|voda|prame|studni/i],
|
||||
[4, /food|restaurant|caf|shop|bakery|jedlo|reštaur|krčm|obchod/i],
|
||||
[9, /first.?aid|hospital|pharmac|doctor|lekár|nemocn/i],
|
||||
[5, /danger|warning|caution|pozor|nebezpe/i],
|
||||
[1, /summit|peak|top\b|vrch|štít|kopec/i],
|
||||
[2, /valley|saddle|pass\b|údoli|sedlo/i],
|
||||
].freeze
|
||||
|
||||
# coordinates: [[lon, lat(, ele)], ...]; duration: seconds or nil.
|
||||
def self.encode(coordinates, name:, sport: nil, duration: nil)
|
||||
# coordinates: [[lon, lat(, ele)], ...]; duration: seconds or nil;
|
||||
# points: [{ coordinate:, name:, hint: }] shown along the course (each
|
||||
# placed at the nearest point of the line).
|
||||
def self.encode(coordinates, name:, sport: nil, duration: nil, points: [])
|
||||
raise ArgumentError, "a course needs at least two points" if coordinates.size < 2
|
||||
|
||||
distances = [0.0]
|
||||
@@ -41,6 +54,15 @@ module Maptool
|
||||
at = start + (total.positive? ? duration * distances[i] / total : 0).round
|
||||
body << data(4, RECORD, [at, *semicircles([lon, lat]), ele ? ((ele + 500) * 5).round.clamp(0, 0xFFFE) : 0xFFFF, (distances[i] * 100).round])
|
||||
end
|
||||
unless points.empty?
|
||||
body << definition(5, COURSE_POINT)
|
||||
at_index = ->(i) { start + (total.positive? ? duration * distances[i] / total : 0).round }
|
||||
points.map { |point| [nearest(coordinates, point[:coordinate]), point] }.sort_by(&:first).each_with_index do |(i, point), n|
|
||||
type = POINT_TYPES.find { |_, pattern| point[:hint].to_s.match?(pattern) }&.first || 0
|
||||
label = point[:name].to_s.byteslice(0, 15).scrub("")
|
||||
body << data(5, COURSE_POINT, [n, at_index.(i), *semicircles(point[:coordinate]), (distances[i] * 100).round, type, label])
|
||||
end
|
||||
end
|
||||
body << data(3, EVENT, [finish, 0, 9]) # timer, stop_disable_all
|
||||
|
||||
header = [14, 0x10, 2132, body.bytesize].pack("CCS<L<") + ".FIT"
|
||||
@@ -49,6 +71,12 @@ module Maptool
|
||||
file + [crc(file)].pack("S<")
|
||||
end
|
||||
|
||||
# ponytail: linear scan per point; fine for hundreds of points on a few thousand.
|
||||
def self.nearest(coordinates, (lon, lat))
|
||||
coordinates.each_index.min_by { |i| (coordinates[i][0] - lon)**2 + (coordinates[i][1] - lat)**2 }
|
||||
end
|
||||
private_class_method :nearest
|
||||
|
||||
def self.semicircles((lon, lat))
|
||||
[(lat * SEMICIRCLES).round, (lon * SEMICIRCLES).round]
|
||||
end
|
||||
@@ -59,7 +87,7 @@ module Maptool
|
||||
end
|
||||
private_class_method :message
|
||||
|
||||
SIZES = { "C" => 1, "S<" => 2, "L<" => 4, "l<" => 4, "a32" => 32 }.freeze
|
||||
SIZES = { "C" => 1, "S<" => 2, "L<" => 4, "l<" => 4, "a16" => 16, "a32" => 32 }.freeze
|
||||
|
||||
def self.definition(local, (global, fields))
|
||||
[0x40 | local, 0, 0, global, fields.size].pack("CCCS<C") +
|
||||
@@ -68,7 +96,7 @@ module Maptool
|
||||
private_class_method :definition
|
||||
|
||||
def self.data(local, (_, fields), values)
|
||||
[local].pack("C") + fields.zip(values).map { |(_, _, format), value| [format == "a32" ? value : value.round].pack(format) }.join
|
||||
[local].pack("C") + fields.zip(values).map { |(_, _, format), value| [format.start_with?("a") ? value : value.round].pack(format) }.join
|
||||
end
|
||||
private_class_method :data
|
||||
|
||||
|
||||
+24
-4
@@ -133,13 +133,14 @@ module Maptool
|
||||
[db.execute("SELECT id, parent_id, name FROM folders WHERE deleted_at IS NULL ORDER BY name COLLATE NOCASE"),
|
||||
db.execute(<<~SQL)]
|
||||
SELECT id, folder_id, parent_id, type, name, source_path,
|
||||
CASE WHEN type IN ('layer', 'track', 'route') THEN properties END
|
||||
CASE WHEN type IN ('layer', 'track', 'route') THEN properties END,
|
||||
CASE WHEN parent_id IS NOT NULL THEN geometry END
|
||||
FROM items WHERE deleted_at IS NULL ORDER BY name COLLATE NOCASE
|
||||
SQL
|
||||
end
|
||||
children_of = folders.group_by { |f| f[1] }
|
||||
items_in = items.group_by { |i| i[1] }
|
||||
has_children = items.filter_map { |i| i[2] }.to_set
|
||||
children = items.select { |i| i[2] && i[3] == "point" }.group_by { |i| i[2] }
|
||||
build = lambda do |id, path|
|
||||
subfolders = (children_of[id] || []).map do |fid, _, name|
|
||||
sub = path.empty? ? name : "#{path}/#{name}"
|
||||
@@ -148,9 +149,11 @@ module Maptool
|
||||
own = items_in[id] || []
|
||||
nodes = own.select { |i| %w[track route layer].include?(i[3]) }.map do |iid, _, _, type, name, source, props|
|
||||
props = JSON.parse(props)
|
||||
kinds = type == "layer" ? props["kinds"] || [] : [type, *("waypoint" if has_children.include?(iid))]
|
||||
points = (children[iid] || []).map { |pid, *, pname, _, _, geometry| { "id" => pid, "name" => pname.to_s.empty? ? "Point" : pname, "coordinates" => JSON.parse(geometry)["coordinates"] } }
|
||||
kinds = type == "layer" ? props["kinds"] || [] : [type, *("waypoint" if points.any?)]
|
||||
{ "type" => "file", "path" => "item:#{iid}", "name" => name.to_s.empty? ? type.capitalize : name,
|
||||
"kinds" => kinds, "original" => !source.nil?, "layer" => type == "layer", "color" => props["color"] }.compact
|
||||
"kinds" => kinds, "original" => !source.nil?, "layer" => type == "layer", "color" => props["color"],
|
||||
"points" => (points if points.any?) }.compact
|
||||
end
|
||||
loose = own.count { |i| %w[point shape].include?(i[3]) && i[2].nil? }
|
||||
if loose.positive?
|
||||
@@ -237,6 +240,23 @@ module Maptool
|
||||
File.basename(source, ".*")
|
||||
end
|
||||
|
||||
# A point into a track/route (`key` "item:<id>"), kept with it like a
|
||||
# GPX's own waypoints: shown, moved, deleted and exported together.
|
||||
def add_point(key, feature)
|
||||
kind, id = key.to_s.split(":", 2)
|
||||
raise NotFound unless kind == "item"
|
||||
raise InvalidValue unless feature.dig("geometry", "type") == "Point"
|
||||
|
||||
props = (feature["properties"] || {}).except("id").compact
|
||||
write do |db, rev, now|
|
||||
folder = db.get_first_row("SELECT folder_id FROM items WHERE id = ? AND type IN ('track', 'route') AND deleted_at IS NULL", [id]) or raise NotFound
|
||||
point = SecureRandom.uuid
|
||||
db.execute("INSERT INTO items VALUES (?, ?, ?, 'point', ?, ?, ?, ?, NULL, ?, ?, NULL, ?)",
|
||||
[point, folder[0], id, props["name"], JSON.generate(feature["geometry"]), JSON.generate(props), point, now, now, rev])
|
||||
point
|
||||
end
|
||||
end
|
||||
|
||||
# One hand-drawn note (notes.js) into `folder`; returns its node key.
|
||||
def add_note(feature, folder)
|
||||
add_features([feature], folder).first
|
||||
|
||||
Reference in new issue
Block a user