Profile: 1/5/10 km splits or laps (FIT/TCX) shaded in the charts, in the table and as numbered posts on the map; tooltip follows the pointer over the plots only
CI / check (push) Successful in 19s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-11-17 16:47:22 +01:00
1 parent 3e487539bc
commit 38368fc801
9 files changed
+292 -101

No files matched your search

+31 -11
View File
@@ -5,7 +5,7 @@ module Maptool
# to a GeoJSON FeatureCollection, in the same shape Gpx.to_geojson produces
# for a <trk>: one Feature per file, geometry MultiLineString, "kind" =>
# "track". Only "record" messages (the track points) and one "sport"/
# "session" field (the activity type) are read — FIT also carries laps,
# "session" field (the activity type) and lap start times are read — FIT also carries
# events, device info, developer fields, none of which this app has
# anywhere to show, so they're skipped by byte-length rather than
# interpreted.
@@ -16,6 +16,8 @@ module Maptool
RECORD_MESSAGE_NUM = 20
SPORT_MESSAGE_NUM = 12
SESSION_MESSAGE_NUM = 18
LAP_MESSAGE_NUM = 19
FIELD_LAP_START_TIME = 2
FIELD_POSITION_LAT = 0
FIELD_POSITION_LONG = 1
FIELD_ALTITUDE = 2
@@ -46,7 +48,7 @@ module Maptool
}.freeze
Definition = Struct.new(:global_mesg_num, :little_endian, :fields, :total_size)
ParseResult = Struct.new(:records, :activity_type)
ParseResult = Struct.new(:records, :activity_type, :lap_starts)
def self.kinds(path)
to_geojson(path)["features"].empty? ? [] : ["track"]
@@ -62,6 +64,7 @@ module Maptool
def self.track_feature(result)
coordinates = result.records.map { |r| r[:coordinate] }
laps = lap_indices(result)
extras = result.records.map { |r| { speed_mps: r[:speed_mps], cadence_rpm: r[:cadence], heart_rate_bpm: r[:heart_rate], power_w: r[:power], temperature_c: r[:temperature] } }
{
@@ -69,11 +72,20 @@ module Maptool
"geometry" => { "type" => "MultiLineString", "coordinates" => [coordinates] },
"properties" => { "kind" => "track", "name" => nil, "activity_type" => result.activity_type }
.merge(TrackStats.compute(coordinates, times: result.records.map { |r| r[:time] }))
.merge("profile" => TrackStats.build_profile(coordinates, times: result.records.map { |r| r[:time] }, extras: extras)),
.merge("profile" => TrackStats.build_profile(coordinates, times: result.records.map { |r| r[:time] }, extras: extras))
.merge(laps ? { "laps" => laps } : {}),
}
end
private_class_method :track_feature
# Index of the first record of each lap, nil for a single lap.
def self.lap_indices(result)
indices = result.lap_starts.sort.map { |t| result.records.index { |r| r[:time] && r[:time].to_i >= t } }.compact.uniq
indices[0] = 0 if indices.any?
indices.size > 1 ? indices : nil
end
private_class_method :lap_indices
def self.parse(data)
raise ParseError, "not a FIT file" if data.bytesize < 12 || data.byteslice(8, 4) != ".FIT"
@@ -85,6 +97,7 @@ module Maptool
offset = header_size
definitions = {}
records = []
lap_starts = []
activity_type = nil
while offset < data_end
@@ -94,7 +107,7 @@ module Maptool
if header & 0x80 != 0
local_type = (header >> 5) & 0x3
definition = definitions[local_type] or raise ParseError, "data message before its definition"
offset, sport = read_data_message(data, offset, definition, records)
offset, sport = read_data_message(data, offset, definition, records, lap_starts)
activity_type ||= sport
elsif header & 0x40 != 0
local_type = header & 0x0F
@@ -103,12 +116,12 @@ module Maptool
else
local_type = header & 0x0F
definition = definitions[local_type] or raise ParseError, "data message before its definition"
offset, sport = read_data_message(data, offset, definition, records)
offset, sport = read_data_message(data, offset, definition, records, lap_starts)
activity_type ||= sport
end
end
ParseResult.new(records, activity_type)
ParseResult.new(records, activity_type, lap_starts)
rescue ArgumentError, NoMethodError => e
raise ParseError, e.message
end
@@ -143,11 +156,14 @@ module Maptool
# Returns [next_offset, sport_name_or_nil] — the latter only set when
# this message happens to be the one sport/session message carrying it.
def self.read_data_message(data, offset, definition, records)
def self.read_data_message(data, offset, definition, records, lap_starts)
sport = nil
if definition.global_mesg_num == RECORD_MESSAGE_NUM
record = extract_record(data, offset, definition)
records << record if record
elsif definition.global_mesg_num == LAP_MESSAGE_NUM
start = field_value(data, offset, definition, FIELD_LAP_START_TIME)
lap_starts << FIT_EPOCH + start if start && !invalid?(start, 4)
elsif (field_num = SPORT_FIELD_BY_MESSAGE[definition.global_mesg_num])
sport = extract_sport(data, offset, definition, field_num)
end
@@ -156,16 +172,20 @@ module Maptool
private_class_method :read_data_message
def self.extract_sport(data, offset, definition, target_field_num)
value = field_value(data, offset, definition, target_field_num)
value && SPORT_NAMES[value]
end
def self.field_value(data, offset, definition, target_field_num)
pos = offset
definition.fields.each do |field_num, size|
if field_num == target_field_num
value = unpack_int(data, pos, size, definition.little_endian, signed: false)
return SPORT_NAMES[value]
end
return unpack_int(data, pos, size, definition.little_endian, signed: false) if field_num == target_field_num
pos += size
end
nil
end
private_class_method :field_value
private_class_method :extract_sport
# Only reads the fields the map/its properties need; every other field
+2 -2
View File
@@ -37,7 +37,7 @@ module Maptool
EXTENSIONS = %w[.gpx .fit .tcx .geojson].freeze
# Bumped when a parser starts reading more (power, temperature, ...):
# tracks imported before get their profile re-read once (reparse).
PARSER_VERSION = 1
PARSER_VERSION = 2
def initialize(db_path, gpx_store:, sources_dir:)
@path = db_path
@@ -92,7 +92,7 @@ module Maptool
write do |db, rev, now|
db.execute("UPDATE items SET properties = ?, updated_at = ?, rev = ? WHERE id = ?",
[JSON.generate(JSON.parse(props).merge("profile" => feature["properties"]["profile"])), now, rev, id])
[JSON.generate(JSON.parse(props).merge(feature["properties"].slice("profile", "laps"))), now, rev, id])
end
updated += 1
rescue Gpx::ParseError, Fit::ParseError, Errno::ENOENT, JSON::ParserError, GpxStore::InvalidPath
+15 -9
View File
@@ -22,24 +22,30 @@ module Maptool
points = []
times = []
extras = []
activity.each_element("Lap/Track/Trackpoint") do |pt|
lat = text(pt, "Position/LatitudeDegrees")
lon = text(pt, "Position/LongitudeDegrees")
next unless lat && lon
laps = [] # index of each lap's first positioned point
activity.each_element("Lap") do |lap|
laps << points.size
lap.each_element("Track/Trackpoint") do |pt|
lat = text(pt, "Position/LatitudeDegrees")
lon = text(pt, "Position/LongitudeDegrees")
next unless lat && lon
ele = text(pt, "AltitudeMeters")
points << (ele ? [lon.to_f, lat.to_f, ele.to_f] : [lon.to_f, lat.to_f])
times << (Time.iso8601(text(pt, "Time")) rescue nil)
extras << extensions(pt).merge({ heart_rate_bpm: text(pt, "HeartRateBpm/Value")&.to_i, cadence_rpm: text(pt, "Cadence")&.to_i }.compact)
ele = text(pt, "AltitudeMeters")
points << (ele ? [lon.to_f, lat.to_f, ele.to_f] : [lon.to_f, lat.to_f])
times << (Time.iso8601(text(pt, "Time")) rescue nil)
extras << extensions(pt).merge({ heart_rate_bpm: text(pt, "HeartRateBpm/Value")&.to_i, cadence_rpm: text(pt, "Cadence")&.to_i }.compact)
end
end
next if points.empty?
laps = laps.uniq.reject { |i| i >= points.size }
features << {
"type" => "Feature",
"geometry" => { "type" => "MultiLineString", "coordinates" => [points] },
"properties" => { "kind" => "track", "name" => nil, "activity_type" => SPORTS.fetch(activity.attributes["Sport"], activity.attributes["Sport"]&.downcase) }
.merge(TrackStats.compute(points, times: times))
.merge("profile" => TrackStats.build_profile(points, times: times, extras: extras)),
.merge("profile" => TrackStats.build_profile(points, times: times, extras: extras))
.merge(laps.size > 1 ? { "laps" => laps } : {}),
}
end
{ "type" => "FeatureCollection", "features" => features }