Files
maptool/lib/maptool/gpx.rb
T
randy ebd4852e98
CI / check (push) Successful in 24s
CI / deploy (push) Successful in 17s
Temperature from FIT/GPX in the profile chart, splits and summary
2026-11-16 14:59:55 +01:00

290 lines
11 KiB
Ruby

require "rexml/document"
require "time"
require_relative "track_stats"
module Maptool
# Converts a GPX file to a GeoJSON FeatureCollection: waypoints (<wpt>)
# become Point features, tracks (<trk>) and routes (<rte>) both become
# MultiLineString features — "kind" (=> "track"/"route") is the only
# difference in shape, so the file tree can show which one a file is.
module Gpx
class ParseError < StandardError; end
# Cheap content-type check for the file tree's icons: a plain substring
# search rather than a full REXML parse, since some uploaded files (e.g.
# a several-thousand-waypoint POI export) are large enough that parsing
# the whole DOM just to answer "does it have any of these tags" would
# make every tree fetch noticeably slower.
def self.kinds(path)
content = File.read(path)
kinds = []
kinds << "waypoint" if content.include?("<wpt")
kinds << "track" if content.include?("<trk")
kinds << "route" if content.include?("<rte")
kinds
end
def self.to_geojson(path)
root = REXML::Document.new(File.read(path)).root
raise ParseError, "not a GPX file" unless root && root.name == "gpx"
features = []
root.each_element("wpt") { |wpt| features << waypoint_feature(wpt) }
root.each_element("trk") { |trk| features << track_feature(trk) }
root.each_element("rte") { |rte| features << route_feature(rte) }
{ "type" => "FeatureCollection", "features" => features }
rescue REXML::ParseException => e
raise ParseError, e.message
end
def self.waypoint_feature(wpt)
ext = waypoint_style(wpt)
{
"type" => "Feature",
"geometry" => { "type" => "Point", "coordinates" => coordinate(wpt) },
"properties" => {
"kind" => "waypoint",
"name" => child_text(wpt, "name"),
"description" => child_text(wpt, "desc"),
"time" => child_text(wpt, "time"),
"category" => child_text(wpt, "type"),
"color" => ext[:color],
"icon" => ext[:icon],
"photos" => waypoint_photos(wpt),
},
}
end
private_class_method :waypoint_feature
# The other direction, for exporting to OsmAnd/Locus (Library#export):
# Point features → <wpt>, kind "route" lines → <rte>, every other line
# or polygon ring → <trk>. `group` replaces every waypoint's <type>
# (OsmAnd reads it as the favourites group); without it the point's own
# category is kept. Colors go out as <osmand:color> (what import reads
# back) and, on lines, gpx_style's <line><color>, which Locus reads.
def self.from_features(features, group: nil)
doc = REXML::Document.new
doc << REXML::XMLDecl.new("1.0", "UTF-8")
gpx = doc.add_element("gpx", "version" => "1.1", "creator" => "MapTool", "xmlns" => "http://www.topografix.com/GPX/1/1",
"xmlns:osmand" => OSMAND_NAMESPACE, "xmlns:gpx_style" => GPX_STYLE_NAMESPACE)
points, lines = features.partition { |f| f.dig("geometry", "type") == "Point" }
points.each { |f| write_waypoint(gpx, f, group) }
lines.each { |f| write_line(gpx, f) }
out = +""
doc.write(output: out)
out
end
GPX_STYLE_NAMESPACE = "http://www.topografix.com/GPX/gpx_style/0/2"
private_constant :GPX_STYLE_NAMESPACE
def self.write_waypoint(gpx, feature, group)
props = feature["properties"] || {}
wpt = gpx.add_element("wpt", position_attributes(feature["geometry"]["coordinates"]))
add_text(wpt, "ele", feature["geometry"]["coordinates"][2])
add_text(wpt, "time", props["time"])
add_text(wpt, "name", props["name"])
add_text(wpt, "desc", props["description"])
add_text(wpt, "type", group || props["category"])
return unless props["color"] || props["icon"]
extensions = wpt.add_element("extensions")
add_text(extensions, "osmand:color", props["color"])
add_text(extensions, "osmand:icon", props["icon"])
end
private_class_method :write_waypoint
def self.write_line(gpx, feature)
props = feature["properties"] || {}
geometry = feature["geometry"]
segments = case geometry["type"]
when "LineString" then [geometry["coordinates"]]
when "MultiLineString", "Polygon" then geometry["coordinates"]
when "MultiPolygon" then geometry["coordinates"].flatten(1)
else return
end
route = props["kind"] == "route"
element = gpx.add_element(route ? "rte" : "trk")
add_text(element, "name", props["name"])
add_text(element, "desc", props["description"])
add_text(element, "type", props["activity_type"])
if props["color"]
extensions = element.add_element("extensions")
add_text(extensions, "osmand:color", props["color"])
add_text(extensions.add_element("gpx_style:line"), "gpx_style:color", props["color"].delete("#"))
end
if route
segments.flatten(1).each { |c| add_text(element.add_element("rtept", position_attributes(c)), "ele", c[2]) }
else
segments.each do |segment|
trkseg = element.add_element("trkseg")
segment.each { |c| add_text(trkseg.add_element("trkpt", position_attributes(c)), "ele", c[2]) }
end
end
end
private_class_method :write_line
def self.position_attributes(coordinates)
{ "lat" => coordinates[1].to_s, "lon" => coordinates[0].to_s }
end
private_class_method :position_attributes
def self.add_text(parent, name, value)
parent.add_element(name).text = value.to_s unless value.nil? || value.to_s.empty?
end
private_class_method :add_text
# <link> is plain GPX 1.1 (a wpt/trk/rte can carry any number of them),
# not a vendor extension like waypoint_style's <osmand:*> — a newer
# p4n2gpx export started emitting one per park4night listing photo. A
# plain GPS waypoint has none, so this is almost always empty.
def self.waypoint_photos(wpt)
wpt.get_elements("link").filter_map { |link| link.attributes["href"] }
end
private_class_method :waypoint_photos
OSMAND_NAMESPACE = "https://osmand.net"
private_constant :OSMAND_NAMESPACE
# POI exports (park4night via p4n2gpx, OsmAnd) tag each waypoint with a
# category (<type>) and, in a vendor <extensions> block, a per-category
# color and an OsmAnd icon key (e.g. "amenity_parking") — used to
# color/icon-code the map instead of every waypoint rendering as the same
# plain dot. Matched by namespace *URI*, not just local element name
# (unlike point_extensions below, which only cares about local names):
# a p4n2gpx export carries both an <osmand:icon> (a category key we can
# actually render) and a <locus:icon> (a Locus-app-specific drawable
# path) side by side — both strip down to the same local name "icon",
# so matching on that alone picks whichever happens to come last.
def self.waypoint_style(wpt)
values = {}
extensions = wpt.elements["extensions"]
return values unless extensions
extensions.elements.each do |el|
next unless el.namespace == OSMAND_NAMESPACE
case el.name
when "color" then values[:color] = el.text&.strip
when "icon" then values[:icon] = el.text&.strip
end
end
values
end
private_class_method :waypoint_style
def self.track_feature(trk)
lines = []
all_points = []
times = []
extras = []
trk.each_element("trkseg") do |seg|
points = []
seg.each_element("trkpt") do |pt|
point, time, extra = process_point(pt)
points << point
all_points << point
times << time
extras << extra
end
lines << points unless points.empty?
end
line_feature(kind: "track", name: child_text(trk, "name"), activity_type: child_text(trk, "type"),
lines: lines, all_points: all_points, times: times, extras: extras)
end
private_class_method :track_feature
# Structurally simpler than a track: <rte> holds <rtept> directly, no
# <trkseg>-style grouping — a route is one continuous line by
# definition, planned rather than recorded.
def self.route_feature(rte)
points = []
times = []
extras = []
rte.each_element("rtept") do |pt|
point, time, extra = process_point(pt)
points << point
times << time
extras << extra
end
line_feature(kind: "route", name: child_text(rte, "name"), activity_type: nil,
lines: points.empty? ? [] : [points], all_points: points, times: times, extras: extras)
end
private_class_method :route_feature
def self.line_feature(kind:, name:, activity_type:, lines:, all_points:, times:, extras:)
{
"type" => "Feature",
"geometry" => { "type" => "MultiLineString", "coordinates" => lines },
"properties" => { "kind" => kind, "name" => name, "activity_type" => activity_type }
.merge(TrackStats.compute(all_points, times: times))
.merge("profile" => TrackStats.build_profile(all_points, times: times, extras: extras)),
}
end
private_class_method :line_feature
def self.process_point(pt)
[coordinate(pt), parse_time(child_text(pt, "time")), point_extensions(pt)]
end
private_class_method :process_point
# Heart rate/cadence/speed/power/temperature live in a device-specific <extensions> block
# (usually Garmin's own TrackPointExtension) — matched by local element
# name only (`Element#name` already strips the namespace prefix), so
# this doesn't care which extension schema a given file actually
# declares, as long as it names its elements the same way Garmin's does.
#
# Walked by hand (elements.each with no argument — plain child
# iteration) rather than the `"extensions//*"` XPath this used
# originally: REXML's `//` (descendant) axis turned out to cost
# milliseconds *per call* regardless of how small the subtree actually
# is, and this runs once per point — fine on a short test file, ~30s
# for an 8000-point real one. Plain traversal is ~40x faster on that
# same file.
def self.point_extensions(point_el)
values = {}
extensions = point_el.elements["extensions"]
collect_extension_values(extensions, values) if extensions
values
end
private_class_method :point_extensions
def self.collect_extension_values(el, values)
el.elements.each do |child|
case child.name
when "hr" then values[:heart_rate_bpm] = child.text&.strip&.to_i
when "cad" then values[:cadence_rpm] = child.text&.strip&.to_i
when "speed" then values[:speed_mps] = child.text&.strip&.to_f
when "power", "PowerInWatts" then values[:power_w] = child.text&.strip&.to_i
when "atemp", "temp" then values[:temperature_c] = child.text&.strip&.to_f
end
collect_extension_values(child, values)
end
end
private_class_method :collect_extension_values
def self.parse_time(text)
text && Time.parse(text)
rescue ArgumentError
nil
end
private_class_method :parse_time
def self.coordinate(point_el)
lat = point_el.attributes["lat"].to_f
lon = point_el.attributes["lon"].to_f
ele = child_text(point_el, "ele")
ele ? [lon, lat, ele.to_f] : [lon, lat]
end
private_class_method :coordinate
def self.child_text(el, name)
el.elements[name]&.text&.strip
end
private_class_method :child_text
end
end