gpxkit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +14 -0
- data/LICENSE +21 -0
- data/README.md +95 -0
- data/SPECIFICATION.md +306 -0
- data/lib/gpxkit/activity.rb +268 -0
- data/lib/gpxkit/error.rb +40 -0
- data/lib/gpxkit/field.rb +51 -0
- data/lib/gpxkit/limits.rb +63 -0
- data/lib/gpxkit/message.rb +63 -0
- data/lib/gpxkit/message_builder.rb +223 -0
- data/lib/gpxkit/reader.rb +112 -0
- data/lib/gpxkit/schema.rb +169 -0
- data/lib/gpxkit/types.rb +270 -0
- data/lib/gpxkit/version.rb +5 -0
- data/lib/gpxkit/xml/clock.rb +41 -0
- data/lib/gpxkit/xml/element.rb +60 -0
- data/lib/gpxkit/xml/parser.rb +193 -0
- data/lib/gpxkit/xml/source.rb +265 -0
- data/lib/gpxkit.rb +26 -0
- metadata +110 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 9cce4988ec89f3b85872139e16dfafba7683ce0592b0e4edd409b86549963fd4
|
|
4
|
+
data.tar.gz: 0f9b172924637dbdc860a01e32bb2b73eecc7fdf92e9783f189a07c3ad187d5f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '08bc4e62fbb39f21b3115ef89ec661bc30b13cd0cff4fcedbed88dc4d556c834eb98ae5f54bd8849185d0d633535809a1f7a509b1134da76ad131f6952e1c7f8'
|
|
7
|
+
data.tar.gz: d8a761a6bcdc73099d74ca6a98b57d92c0601dde4af25f1624a061426de28677742086a45856594a48beae330847de80d7b7ee1cbadd2b2b4e83307e258d3952
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release: a read-only, streaming reader for GPX 1.1 and 1.0 files, with typed messages and fields, the common fitness extensions, enforced limits, refusal of DTDs, and an `Activity` view in the shape of `FitKit::Activity`. Requires Ruby 3.1 and Nokogiri 1.17.
|
|
6
|
+
|
|
7
|
+
Development, by milestone:
|
|
8
|
+
|
|
9
|
+
- M4: hardening. Reading 24-hour files is about a third faster (the time limit's clock now pauses once per message, and paths, names and types are cached per place in a message). Seeded fuzz tests of bytes, markup and values run with the suite. Exporter quirks are reproduced in tests; undeclared namespace prefixes no longer stop reading (decision 4), a DTD in a UTF-16 document is refused before parsing, and numbers beyond a Float are decoded without Ruby warnings.
|
|
10
|
+
- M3: `Activity` view in the shape of `FitKit::Activity`: sessions (one per `<trk>`, with segments), records, waypoints and routes. Record values come from the first valid source in the order of decision 2. Checked against FIT exports of the same activities.
|
|
11
|
+
- `copyright/year` is decoded as an `xsd:gYear` (an `Integer`) instead of unchecked text, with the new invalid reason `:not_a_year`. Accepting `+` and `-0` on non-negative integers is now documented.
|
|
12
|
+
- M2: messages and fields. The Reader yields a `Message` per metadata, waypoint, route, route point, track, track segment and track point, and a final `:document`, each with its `context`. Every leaf element and attribute is a `Field` with the text as written, a value typed from the GPX 1.1 or 1.0 schema, and `valid?`/`invalid_reason`. TrackPointExtension v1 and v2, PowerExtension v1, Cluetrust GPXDATA and a bare `power` are typed; other extensions are kept untyped.
|
|
13
|
+
- M1: safe streaming reader. `Reader` (paths, Pathnames and IO, `close_io:`, re-reading seekable IO), `Limits` (`max_bytes`, `max_elements`, `max_depth`, `max_text_bytes`, `max_seconds`), refusal of DTD internal subsets and DTD entities, `TruncatedFileError` for files cut off, `UnsupportedFormatError` for anything but GPX 1.1 or 1.0 (a root without a namespace is read by its version attribute), and skipping whitespace before the XML declaration. Yields no messages yet. Requires Nokogiri 1.17.
|
|
14
|
+
- M0: specification, gem skeleton and CI.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The gpxkit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# GpxKit
|
|
2
|
+
|
|
3
|
+
A read-only, streaming Ruby parser for GPX 1.1 and GPX 1.0 (`.gpx`) files.
|
|
4
|
+
|
|
5
|
+
**Status: 0.1.0, the first release.** All four milestones of [`SPECIFICATION.md`](SPECIFICATION.md) are done. The API may still change before 1.0.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
# Gemfile
|
|
11
|
+
gem "gpxkit"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires Ruby 3.1 or newer and Nokogiri 1.17 or newer (the only runtime dependency).
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
require "gpxkit"
|
|
20
|
+
|
|
21
|
+
activity = GpxKit::Activity.read("ride.gpx", limits: { max_seconds: 30 })
|
|
22
|
+
puts "#{activity.sport}: #{activity.start_time} to #{activity.end_time}"
|
|
23
|
+
|
|
24
|
+
activity.records.each do |record|
|
|
25
|
+
puts [record.timestamp, record.latitude, record.longitude, record.altitude, record.heart_rate, record.power].inspect
|
|
26
|
+
end
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The low-level reader yields one message per element:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require "gpxkit"
|
|
33
|
+
|
|
34
|
+
GpxKit::Reader.new("ride.gpx", limits: { max_seconds: 30 }).each_message do |message|
|
|
35
|
+
next unless message.name == :track_point
|
|
36
|
+
|
|
37
|
+
puts [message.timestamp, message[:lat], message[:lon], message[:ele], message[:hr], message[:power_in_watts]].inspect
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Every value is a `Field` with the text as written, a typed value, and a reason when the value does not fit the schema:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
field = message.field(:ele)
|
|
45
|
+
field.text # "1e3"
|
|
46
|
+
field.value # nil
|
|
47
|
+
field.invalid_reason # :not_a_number (xsd:decimal has no exponent)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Errors carry the line and column:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
rescue GpxKit::TruncatedFileError, GpxKit::InvalidFileError, GpxKit::UnsupportedFormatError,
|
|
54
|
+
GpxKit::UnsafeContentError, GpxKit::LimitExceededError => error
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
GpxKit is a sister project of [fitkit](https://github.com/scarhand/fitkit) (FIT) and [tcxkit](https://github.com/scarhand/tcxkit) (TCX), and shares their API shape: the same reader contract and limits, matching error classes, and an `Activity` view with fitkit's method names.
|
|
58
|
+
|
|
59
|
+
Common fitness extensions (Garmin TrackPointExtension v1/v2, PowerExtension, Cluetrust GPXDATA) are decoded; others are kept as written.
|
|
60
|
+
|
|
61
|
+
## Principles
|
|
62
|
+
|
|
63
|
+
- Read-only: never writes or repairs files.
|
|
64
|
+
- Untrusted input: no network access, no DTD entities, enforced limits on size, depth and time.
|
|
65
|
+
- Values exactly as written next to typed values; unknown elements and extensions kept.
|
|
66
|
+
- Nothing filled in, estimated or derived: no distances or speeds computed from positions.
|
|
67
|
+
|
|
68
|
+
## Development
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
bundle install
|
|
72
|
+
bundle exec rake test
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Requires Ruby 3.1 or newer. The only runtime dependency is Nokogiri.
|
|
76
|
+
|
|
77
|
+
The suite includes seeded fuzz tests. Longer or different rounds:
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
FUZZ_ITERATIONS=50000 FUZZ_SEED=7 bundle exec ruby -Ilib -Itest test/gpxkit/fuzz_test.rb
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Test fixtures must be synthetic. Never commit real tracks: they contain locations, times and health data.
|
|
84
|
+
|
|
85
|
+
## Performance
|
|
86
|
+
|
|
87
|
+
Reading streams, so memory stays flat however long the file is (the `Activity` view keeps every message, by design). A one-hour activity reads in well under a second. A synthetic 24-hour file with a point every second takes about 9 s of CPU time on a laptop; set `limits: { max_seconds: ... }` for untrusted files.
|
|
88
|
+
|
|
89
|
+
## Releasing
|
|
90
|
+
|
|
91
|
+
Releases are published by the maintainer: bump `lib/gpxkit/version.rb`, move the changelog's notes under the new version, then `bundle exec rake release`, which builds the gem, tags the commit and pushes the gem to RubyGems.
|
|
92
|
+
|
|
93
|
+
## Licence
|
|
94
|
+
|
|
95
|
+
MIT, see [`LICENSE`](LICENSE). Extension schemas by third parties are not part of this repository.
|
data/SPECIFICATION.md
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# GpxKit — Project Specification
|
|
2
|
+
|
|
3
|
+
**Status:** Approved by the maintainer on 2026-11-20; all milestones done for 0.1.0 (2026-11-23)
|
|
4
|
+
**Project type:** Read-only Ruby library for GPS Exchange Format (GPX) files
|
|
5
|
+
**Gem name:** `gpxkit` (free on RubyGems on 2026-11-20)
|
|
6
|
+
**Ruby namespace / require path:** `GpxKit` / `require "gpxkit"`
|
|
7
|
+
**Ruby support:** 3.1 and newer
|
|
8
|
+
**Licence:** MIT
|
|
9
|
+
|
|
10
|
+
Sister projects: [`fitkit`](https://github.com/scarhand/fitkit) (FIT) and [`tcxkit`](https://github.com/scarhand/tcxkit) (TCX). The three gems expose the same shape of API (section 6.4) so an application can handle all three formats the same way.
|
|
11
|
+
|
|
12
|
+
## 1. Purpose
|
|
13
|
+
|
|
14
|
+
Build a reliable, read-only parser for GPX 1.1 and GPX 1.0 files (`.gpx`).
|
|
15
|
+
|
|
16
|
+
GPX describes tracks, routes and waypoints, not workouts. Sport, heart rate, cadence and power appear only through extensions, and laps not at all. The library therefore exposes:
|
|
17
|
+
|
|
18
|
+
1. A low-level streaming `Reader` that yields one `Message` per meaningful GPX element (track point, segment, track, waypoint, route, ...), keeping every value exactly as written in the file, including elements and extensions the library does not recognise.
|
|
19
|
+
2. An optional `Activity` view in the same shape as `FitKit::Activity` and `TcxKit::Activity`, built from the tracks, which reports only what the file states.
|
|
20
|
+
|
|
21
|
+
The library must not create, modify, rewrite or repair GPX files.
|
|
22
|
+
|
|
23
|
+
## 2. Goals
|
|
24
|
+
|
|
25
|
+
- Parse GPX 1.1 and GPX 1.0 documents according to TopoGrafix's published schemas.
|
|
26
|
+
- Decode the common fitness extensions (section 6.3) and keep all others.
|
|
27
|
+
- Stream: memory use must not grow with the number of points unless the caller keeps them.
|
|
28
|
+
- Treat every file as untrusted input: no network access, no external entities, no DTD-defined entities, and enforced limits on size, depth, element count and time.
|
|
29
|
+
- Keep the text exactly as it appears in the file next to every typed value.
|
|
30
|
+
- Never fill in, estimate, smooth or derive measurements the file does not contain. In particular, never compute distance, speed or durations from positions and timestamps; that belongs to the application, labelled as derived. A distance the file states (GPXDATA `distance`) is reported as stated.
|
|
31
|
+
- Raise useful, explicit exceptions with line and column numbers.
|
|
32
|
+
- One runtime dependency: Nokogiri 1.17 or later, used through its SAX interface.
|
|
33
|
+
|
|
34
|
+
## 3. Non-goals
|
|
35
|
+
|
|
36
|
+
- Writing, editing or repairing GPX files.
|
|
37
|
+
- XSD validation at runtime (section 7).
|
|
38
|
+
- Geometry: distance, bearing, simplification, bounds calculation.
|
|
39
|
+
- Mapping, charting, analysis, unit preferences.
|
|
40
|
+
|
|
41
|
+
## 4. Reference material
|
|
42
|
+
|
|
43
|
+
- GPX 1.1: `http://www.topografix.com/GPX/1/1`, schema and documentation at https://www.topografix.com/GPX/1/1/gpx.xsd and https://www.topografix.com/gpx/1/1/
|
|
44
|
+
- GPX 1.0: `http://www.topografix.com/GPX/1/0`, schema at https://www.topografix.com/GPX/1/0/gpx.xsd
|
|
45
|
+
- Garmin TrackPointExtension v1 and v2: `http://www.garmin.com/xmlschemas/TrackPointExtension/v1` and `/v2`
|
|
46
|
+
- Garmin PowerExtension v1: `http://www.garmin.com/xmlschemas/PowerExtension/v1`
|
|
47
|
+
- Cluetrust GPXDATA 1.0: `http://www.cluetrust.com/XML/GPXDATA/1/0`
|
|
48
|
+
|
|
49
|
+
TopoGrafix describes GPX as an open standard with no fees or licensing. Garmin publishes its extension schemas without stating a licence, so **do not copy any extension schema into this repository**. Implement from reading them and cite type names in comments and tests.
|
|
50
|
+
|
|
51
|
+
Every element, type and range this gem relies on must be checked against the published schemas, not taken from memory or inferred from sample files.
|
|
52
|
+
|
|
53
|
+
## 5. Architecture
|
|
54
|
+
|
|
55
|
+
Three layers, each usable on its own, as in tcxkit.
|
|
56
|
+
|
|
57
|
+
### 5.1 XML layer
|
|
58
|
+
|
|
59
|
+
- Nokogiri `XML::SAX::Parser#parse_io` over the source IO, wrapped so bytes read are counted (for `max_bytes`) as they are read.
|
|
60
|
+
- Keeps an element stack (namespace URI, local name, attributes, line, column) and collects text.
|
|
61
|
+
- Rejects unsafe or malformed input (section 7, section 8).
|
|
62
|
+
|
|
63
|
+
### 5.2 GPX layer
|
|
64
|
+
|
|
65
|
+
- Recognises the root `gpx` in the 1.1 or 1.0 namespace (and without a namespace, see section 7); anything else raises `UnsupportedFormatError`.
|
|
66
|
+
- Decides which elements become messages (section 6.2) and which are fields.
|
|
67
|
+
- Types each field from the schema of its version and from known extension namespaces.
|
|
68
|
+
- Assigns each message its context: which track, segment, route or waypoint it belongs to.
|
|
69
|
+
|
|
70
|
+
### 5.3 Activity layer
|
|
71
|
+
|
|
72
|
+
- Collects track points into sessions (tracks) and segments (section 6.4).
|
|
73
|
+
- Keeps every underlying `Message` reachable.
|
|
74
|
+
|
|
75
|
+
## 6. Public API
|
|
76
|
+
|
|
77
|
+
### 6.1 Reader
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
reader = GpxKit::Reader.new("ride.gpx")
|
|
81
|
+
|
|
82
|
+
reader.each_message do |message|
|
|
83
|
+
puts message[:ele] if message.name == :track_point
|
|
84
|
+
end
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Same contract as `FitKit::Reader` and `TcxKit::Reader`:
|
|
88
|
+
|
|
89
|
+
- `Reader.new(source, close_io: false, source_label: nil, limits: Limits::DEFAULT)`. `source` is a path, a `Pathname` or an IO-like object that responds to `read`. A `Pathname` is read as a path even though it responds to `read`.
|
|
90
|
+
- `each_message` streams, and returns an Enumerator without a block.
|
|
91
|
+
- Seekable sources can be iterated again; a non-seekable IO can be read once, and a second iteration raises `Error`.
|
|
92
|
+
- Caller-owned IO is closed only with `close_io: true`.
|
|
93
|
+
- Time spent in the caller's block does not count towards `max_seconds`.
|
|
94
|
+
|
|
95
|
+
### 6.2 Messages
|
|
96
|
+
|
|
97
|
+
A message is emitted when its element closes, so it carries everything inside it: a segment's points arrive before the segment, a track's segments before the track. Elements in the document's GPX namespace become messages only where the schema puts them:
|
|
98
|
+
|
|
99
|
+
| Message name | Element (where) | Fields include |
|
|
100
|
+
|---|---|---|
|
|
101
|
+
| `:metadata` | `metadata` (1.1, in `gpx`) | `name`, `desc`, `author_name`, `author_email_id`, `author_email_domain`, `author_link_href`, `copyright_author`, `copyright_year`, `link_href`, `link_text`, `link_type`, `time`, `keywords`, `bounds_minlat` ... |
|
|
102
|
+
| `:waypoint` | `wpt` (in `gpx`) | the point fields below |
|
|
103
|
+
| `:route_point` | `rtept` (in `rte`) | the point fields below |
|
|
104
|
+
| `:route` | `rte` (in `gpx`) | `name`, `cmt`, `desc`, `src`, `link_href`, `number`, `type` (1.1), `url`, `urlname` (1.0) |
|
|
105
|
+
| `:track_point` | `trkpt` (in `trkseg`) | the point fields below, and in 1.0 `course` and `speed` |
|
|
106
|
+
| `:track_segment` | `trkseg` (in `trk`) | none of its own |
|
|
107
|
+
| `:track` | `trk` (in `gpx`) | as `:route` |
|
|
108
|
+
| `:document` | `gpx` | `version`, `creator`; in 1.0 also the top-level `name`, `desc`, `author`, `email`, `url`, `urlname`, `time`, `keywords`, `bounds_*`; anything else directly in `gpx`, such as its `extensions`; always the last message |
|
|
109
|
+
|
|
110
|
+
Point fields (`wptType`): `lat`, `lon`, `ele`, `time`, `magvar`, `geoidheight`, `name`, `cmt`, `desc`, `src`, `link_href`, `link_text`, `link_type` (1.1), `url`, `urlname` (1.0), `sym`, `type`, `fix`, `sat`, `hdop`, `vdop`, `pdop`, `ageofdgpsdata`, `dgpsid`, plus the known extensions in 6.3.
|
|
111
|
+
|
|
112
|
+
Every leaf element and every attribute inside a message, outside the messages nested in it, is one of its fields. An element with only attributes (`<bounds .../>`) gives only its attribute fields, and an empty `extensions` is not a field.
|
|
113
|
+
|
|
114
|
+
Field names join the snake_case names of every element on the path, attributes included, and leave out the wrappers `extensions`, `Extensions` and `TrackPointExtension`: `link/@href` is `link_href`, `author/email/@id` is `author_email_id`, `extensions/TrackPointExtension/hr` is `hr`, `extensions/PowerInWatts` is `power_in_watts`. The path keeps every element, with local names only, since prefixes are chosen by each file. When names repeat (`hr` from TrackPointExtension and from GPXDATA), `fields_named` returns all of them, and the path or `namespace` tells them apart.
|
|
115
|
+
|
|
116
|
+
`context` places a message: its own index and its ancestors'. `metadata`, `waypoint`, `route` and `track` count across the document; `route_point` counts within its route, `track_segment` within its track and `track_point` within its segment.
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
message.name # :track_point
|
|
120
|
+
message.element_name # "trkpt"
|
|
121
|
+
message.context # { track: 0, track_segment: 1, track_point: 812 }
|
|
122
|
+
message.line, message.column
|
|
123
|
+
message.fields # Array of Field, in document order (names can repeat)
|
|
124
|
+
message[:ele] # typed value of the first field with that name
|
|
125
|
+
message["extensions/TrackPointExtension/hr"] # lookup by path
|
|
126
|
+
message.field(:hr)
|
|
127
|
+
message.fields_named(:hr)
|
|
128
|
+
message.attributes # fields for the message element's own attributes
|
|
129
|
+
message.timestamp # the time field
|
|
130
|
+
message.to_h
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 6.3 Fields
|
|
134
|
+
|
|
135
|
+
```ruby
|
|
136
|
+
field.name # :hr
|
|
137
|
+
field.path # "extensions/TrackPointExtension/hr"
|
|
138
|
+
field.namespace # the TrackPointExtension v2 namespace URI
|
|
139
|
+
field.text # " 142 ", exactly as written
|
|
140
|
+
field.raw_value # "142": the text after the type's whitespace rule
|
|
141
|
+
field.value # 142, typed; nil when invalid
|
|
142
|
+
field.type # :decimal, :latitude, :date_time, :fix, :beats_per_minute, :string, :unknown, ...
|
|
143
|
+
field.known?
|
|
144
|
+
field.valid?
|
|
145
|
+
field.invalid_reason # nil, :empty, :not_a_number, :not_finite, :not_an_integer, :out_of_range,
|
|
146
|
+
# :not_a_date_time, :no_utc_offset, :not_in_enumeration, :not_a_year
|
|
147
|
+
field.line
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Types come from tables written from the schemas of the document's version, keyed by path and namespace. In GPX 1.1 extensions are typed inside `extensions`; in GPX 1.0, which has no `extensions` element, directly inside the point.
|
|
151
|
+
|
|
152
|
+
- Whitespace: `xsd:string`, and `fixType` (an enumeration based on `xsd:string`), keep the text as written; every other type collapses it.
|
|
153
|
+
- `xsd:decimal` → `Float`, with no exponent, `INF` or `NaN` (`:not_a_number`). Extension values typed `xsd:double` allow exponents. Callers that need exact decimals use `BigDecimal(field.raw_value)`.
|
|
154
|
+
- Integer types → `Integer`, range-checked: `nonNegativeInteger` (`sat`, `number`), `dgpsStationType` (0 to 1023). An optional `+`, and `-` before zero, are accepted, as XSD 1.0 and 1.1 both allow for `nonNegativeInteger` and XSD 1.1 for the unsigned types; `-1` is `:not_an_integer`.
|
|
155
|
+
- `xsd:dateTime` → `Time` in UTC when the text has `Z` or an offset; without one, `nil` and `:no_utc_offset` (decision 1). Same rules as tcxkit.
|
|
156
|
+
- `fixType` → `:none`, `:"2d"`, `:"3d"`, `:dgps`, `:pps`; matching is exact.
|
|
157
|
+
- `xsd:anyURI` → the collapsed `String`.
|
|
158
|
+
- `xsd:gYear` (`copyright/year`) → the year as an `Integer` (`2026`); a time zone, if written, stays in `raw_value`. Anything else, such as `26` or `2026-01`, is `:not_a_year`.
|
|
159
|
+
- Ranges are validity, not errors. GPX 1.1: `latitudeType` (-90 to 90), `longitudeType` (-180 up to, not including, 180), `degreesType` (0 up to, not including, 360). GPX 1.0 allows 180 and 360 themselves.
|
|
160
|
+
- Unknown elements and attributes: `known?` false, `type` `:unknown`, `value` is the text as written. That includes GPX elements of the other version (`course` in a 1.1 track point) and elements in the other version's namespace.
|
|
161
|
+
|
|
162
|
+
Known extensions:
|
|
163
|
+
|
|
164
|
+
| Namespace | Elements and types |
|
|
165
|
+
|---|---|
|
|
166
|
+
| TrackPointExtension v1 | `atemp`, `wtemp` (°C, double), `depth` (m, double), `hr` (1 to 255), `cad` (0 to 254) |
|
|
167
|
+
| TrackPointExtension v2 | v1's, plus `speed` (m/s, double), `course`, `bearing` (decimal, 0 to 360) |
|
|
168
|
+
| PowerExtension v1 | `PowerInWatts` (unsigned short) |
|
|
169
|
+
| Cluetrust GPXDATA 1.0 | `hr`, `cadence` (non-negative integers), `temp` (decimal), `distance` (decimal, with a `sensor` attribute: `wheel` or `pedometer`) |
|
|
170
|
+
| the document's GPX namespace (1.1) | `power` inside `extensions`, as several exporters write it; read as a decimal |
|
|
171
|
+
|
|
172
|
+
### 6.4 Activity view
|
|
173
|
+
|
|
174
|
+
The shared shape across fitkit, tcxkit and gpxkit, applied to what GPX can state:
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
activity = GpxKit::Activity.read("ride.gpx", limits: { max_seconds: 30 })
|
|
178
|
+
# also Activity.from_file(path) and Activity.from_io(io); options go to Reader.new
|
|
179
|
+
|
|
180
|
+
activity.sessions # one per <trk>
|
|
181
|
+
activity.laps # always empty: GPX has no laps
|
|
182
|
+
activity.records # every track point, in file order
|
|
183
|
+
activity.waypoints # waypoints, as records
|
|
184
|
+
activity.routes # routes, each with its points as records
|
|
185
|
+
activity.metadata # the 1.1 :metadata message, or in 1.0 the :document message
|
|
186
|
+
activity.document # the :document message
|
|
187
|
+
activity.version # "1.1" or "1.0"
|
|
188
|
+
activity.creator # gpx@creator
|
|
189
|
+
activity.messages
|
|
190
|
+
activity.session
|
|
191
|
+
|
|
192
|
+
# Summary methods read the first session, as in fitkit:
|
|
193
|
+
activity.sport # the first track's <type>, as written; nil if absent
|
|
194
|
+
activity.start_time # the first track's first valid point time
|
|
195
|
+
activity.end_time # the last track's last valid point time
|
|
196
|
+
activity.total_distance, activity.total_timer_time, ... # nil: GPX states no totals
|
|
197
|
+
|
|
198
|
+
session = activity.session
|
|
199
|
+
session.name, session.description, session.number
|
|
200
|
+
session.sport # <type>, as written ("running", "9", "treadmill_running", ...)
|
|
201
|
+
session.segments # each with its records
|
|
202
|
+
session.records
|
|
203
|
+
|
|
204
|
+
record = activity.records.first
|
|
205
|
+
record.timestamp # Time (UTC)
|
|
206
|
+
record.latitude, record.longitude
|
|
207
|
+
record.altitude # ele, m
|
|
208
|
+
record.name
|
|
209
|
+
record.heart_rate # bpm
|
|
210
|
+
record.cadence # rpm
|
|
211
|
+
record.power # W
|
|
212
|
+
record.temperature # °C
|
|
213
|
+
record.speed # m/s
|
|
214
|
+
record.course # degrees
|
|
215
|
+
record.distance # GPXDATA distance, m, as stated; nil without it
|
|
216
|
+
record.raw_message
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Method names match `FitKit::Activity` wherever the meaning matches. A method whose value GPX cannot state returns `nil` instead of computing one. The view does not assume the file is an activity: a GPX with only routes or waypoints reads fine, with no sessions or records.
|
|
220
|
+
|
|
221
|
+
Record values come from the first valid source, in this order (decision 2); every source stays on the raw message, and an invalid value (a heart rate of 0) falls through to the next source:
|
|
222
|
+
|
|
223
|
+
| Value | Sources, in order |
|
|
224
|
+
|---|---|
|
|
225
|
+
| `heart_rate` | TrackPointExtension v2 `hr`, v1 `hr`, GPXDATA `hr` |
|
|
226
|
+
| `cadence` | TrackPointExtension v2 `cad`, v1 `cad`, GPXDATA `cadence` |
|
|
227
|
+
| `power` | PowerExtension `PowerInWatts`, then a bare `power` (a decimal, so a `Float`) |
|
|
228
|
+
| `temperature` | TrackPointExtension v2 `atemp`, v1 `atemp`, GPXDATA `temp` |
|
|
229
|
+
| `speed`, `course` | GPX 1.0 `speed`/`course`, then TrackPointExtension v2 |
|
|
230
|
+
| `distance` | GPXDATA `distance` |
|
|
231
|
+
|
|
232
|
+
Garmin's GPX export of an activity without GPS, such as a treadmill run, has an empty track: no points, and so no heart rate either.
|
|
233
|
+
|
|
234
|
+
## 7. Leniency and strictness
|
|
235
|
+
|
|
236
|
+
Strict (raise):
|
|
237
|
+
|
|
238
|
+
- Not well-formed XML, or a root element other than `gpx`.
|
|
239
|
+
- A DTD internal subset, or any entity reference other than the five predefined XML entities and character references (section 8).
|
|
240
|
+
- Input cut off before the root element closes (`TruncatedFileError`). As in tcxkit, a file counts as cut off when libxml2's error is on the last line with at most part of one token (no `<` or `>`) after it, and the input starts with `<`. Anything else that is not well-formed is `InvalidFileError`.
|
|
241
|
+
- Any limit crossed.
|
|
242
|
+
|
|
243
|
+
Lenient (keep reading, record it on the field or message):
|
|
244
|
+
|
|
245
|
+
- Values that do not fit their type or range (`valid?` false).
|
|
246
|
+
- Unknown elements, attributes and extension namespaces; GPX 1.0 foreign elements anywhere.
|
|
247
|
+
- Elements in an order the schema does not allow, or missing elements: the field is simply absent.
|
|
248
|
+
- A `version` attribute that disagrees with the namespace: the namespace decides; `version` is kept as a field.
|
|
249
|
+
|
|
250
|
+
Known real-world quirks, each reproduced in `test/gpxkit/exporter_quirks_test.rb`:
|
|
251
|
+
|
|
252
|
+
- A `gpx` root without any namespace. Read it as the version its `version` attribute names; the `:document` message's `namespace` is then `nil` (decision 3, section 13).
|
|
253
|
+
- Whitespace before the XML declaration. Skipped, as in tcxkit. A UTF-8 byte order mark is accepted as XML allows.
|
|
254
|
+
- A namespace prefix used without being declared (`<gpxtpx:hr>` with no `xmlns:gpxtpx`), or bound to an empty namespace: libxml2 reports these and reads on, and so does the reader. The element has no namespace, so its values are kept as untyped fields; the gem does not guess which namespace was meant (decision 4). Any other error still makes the file invalid.
|
|
255
|
+
- UTF-16 documents (with or without a byte order mark) and single-byte encodings named in the XML declaration (`ISO-8859-1`, `windows-1252`) are read; values come out as UTF-8. The DTD check decodes the start of a UTF-16 document, so a DTD is refused there too.
|
|
256
|
+
- Bytes that do not match the declared encoding (Latin-1 bytes in a UTF-8 document) make the file invalid.
|
|
257
|
+
- Carriage returns: XML turns `\r\n` into `\n` in text.
|
|
258
|
+
- Near-miss values stay invalid, with their text kept: `+0000` or a lowercase `z` in a time, a decimal comma in `ele`.
|
|
259
|
+
|
|
260
|
+
## 8. Security
|
|
261
|
+
|
|
262
|
+
Identical to tcxkit:
|
|
263
|
+
|
|
264
|
+
- Never access the network or the filesystem beyond the source.
|
|
265
|
+
- Raise `UnsafeContentError` on a DTD internal subset (`<!DOCTYPE x [ ... ]>`) before the XML parser sees it, and on any reference to a DTD-declared entity. A document type declaration without an internal subset is allowed; its external DTD is never loaded.
|
|
266
|
+
- Enforce `Limits` while reading: `max_bytes` (64 MB), `max_elements` (5,000,000), `max_depth` (64), `max_text_bytes` (1 MB: the text of one element without child elements, one attribute value, or the prolog), `max_seconds` (none, and the caller's block is not counted). `nil` switches a limit off; `Limits.none` switches all off.
|
|
267
|
+
- Never include file content in exception messages beyond element names and positions.
|
|
268
|
+
|
|
269
|
+
## 9. Errors
|
|
270
|
+
|
|
271
|
+
```
|
|
272
|
+
GpxKit::Error base; source, line, column
|
|
273
|
+
├── InvalidFileError not well-formed XML
|
|
274
|
+
├── UnsupportedFormatError not a GPX 1.0 or 1.1 document
|
|
275
|
+
├── TruncatedFileError ended before the root element closed
|
|
276
|
+
├── UnsafeContentError DTD-declared entity
|
|
277
|
+
└── LimitExceededError `limit` names the bound crossed
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## 10. Testing
|
|
281
|
+
|
|
282
|
+
- Minitest, no other development dependencies beyond rake.
|
|
283
|
+
- Fixtures are synthetic, with made-up positions and values. Never commit real tracks. Document every fixture in `test/fixtures/README.md`.
|
|
284
|
+
- Cover both GPX versions; each message, field, type and range; each known extension, including two extensions stating the same value; unknown elements; every error and limit; hostile inputs (entity expansion, deep nesting, huge text, truncated files, binary garbage, a TCX file passed by mistake); and IO ownership and re-iteration.
|
|
285
|
+
- Assert decoded values, not merely that parsing succeeds.
|
|
286
|
+
|
|
287
|
+
## 11. Compatibility and packaging
|
|
288
|
+
|
|
289
|
+
- Ruby 3.1+, CI on 3.1, 3.2, 3.3, 3.4 and 4.0.
|
|
290
|
+
- Runtime dependency: `nokogiri ~> 1.17` (1.17 added the SAX `reference` callback).
|
|
291
|
+
- Semantic versioning. Publish to RubyGems only after the maintainer approves a release.
|
|
292
|
+
|
|
293
|
+
## 12. Milestones
|
|
294
|
+
|
|
295
|
+
- **M0 — Foundation** (done): specification, gem skeleton, CI.
|
|
296
|
+
- **M1 — Safe streaming reader** (done): XML layer, `Limits`, errors, root and version detection, section 7, security tests.
|
|
297
|
+
- **M2 — Messages and fields** (done): the message table in 6.2, schema typing for both versions, known extensions, unknown preservation.
|
|
298
|
+
- **M3 — Activity view** (done): section 6.4.
|
|
299
|
+
- **M4 — Hardening and release** (done; release on approval): quirks from real exporters (reproduced synthetically), a benchmark on a 24-hour track, README examples verified, first release on approval. Also seeded fuzz tests of bytes, markup and values, and reading without Ruby warnings.
|
|
300
|
+
|
|
301
|
+
## 13. Decisions (approved 2026-11-20)
|
|
302
|
+
|
|
303
|
+
1. **Times without a UTC offset.** Decided: `value` nil, `invalid_reason` `:no_utc_offset`, raw text kept. Same as tcxkit.
|
|
304
|
+
2. **Which source wins for a record value** when two extensions state it. Decided: TrackPointExtension v2, then v1, then PowerExtension, then GPXDATA, then bare `power`; GPX 1.0 `speed`/`course` before extensions.
|
|
305
|
+
3. **A `gpx` root without a namespace.** Decided: accept it, as in section 7.
|
|
306
|
+
4. **Namespace errors** (2026-11-23). Decided: an undeclared or empty namespace prefix does not stop reading; the elements involved have no namespace, so their values are untyped fields. The same as tcxkit's decision 6. Accepted by the maintainer on 2026-11-23.
|