gpx-rs 0.1.0

GPX 1.1 parsing, validation, path analytics, and CLI tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use std::ops::{Index, IndexMut};
use std::time::Duration;

use crate::gpx::geo::distance_between;
use crate::gpx::types::{Bounds, Route, Track, TrackSegment, Waypoint};

use super::{ProfilePoint, SpeedProfilePoint, WaypointPath};

fn bounds_from_points(points: &[Waypoint]) -> Option<Bounds> {
    if points.is_empty() {
        return None;
    }
    Some(Bounds {
        minlat: points.iter().map(|p| p.lat).fold(f64::INFINITY, f64::min),
        minlon: points.iter().map(|p| p.lon).fold(f64::INFINITY, f64::min),
        maxlat: points.iter().map(|p| p.lat).fold(f64::NEG_INFINITY, f64::max),
        maxlon: points.iter().map(|p| p.lon).fold(f64::NEG_INFINITY, f64::max),
    })
}

fn track_elevation_profile(segments: &[TrackSegment]) -> Vec<ProfilePoint> {
    let mut distance_m = 0.0;
    let mut profile = Vec::new();

    for segment in segments {
        let points_with_ele: Vec<&Waypoint> =
            segment.points.iter().filter(|p| p.ele.is_some()).collect();
        if points_with_ele.is_empty() {
            continue;
        }

        if profile.is_empty() {
            profile.push(ProfilePoint {
                distance_m,
                value: points_with_ele[0].ele.unwrap(),
            });
        }

        for i in 1..points_with_ele.len() {
            distance_m += distance_between(points_with_ele[i - 1], points_with_ele[i]);
            profile.push(ProfilePoint {
                distance_m,
                value: points_with_ele[i].ele.unwrap(),
            });
        }
    }

    profile
}

macro_rules! point_container_stats {
    ($type:ty, $field:ident) => {
        impl $type {
            /// Minimum and maximum latitude/longitude covering all points.
            pub fn bounds(&self) -> Option<Bounds> {
                bounds_from_points(&self.$field)
            }

            /// Total horizontal distance along the path, in meters.
            pub fn total_distance(&self) -> f64 {
                WaypointPath::from(self).total_distance()
            }

            /// Elapsed time from the first to the last timestamped point.
            pub fn total_duration(&self) -> Option<Duration> {
                WaypointPath::from(self).duration()
            }

            /// Time spent moving (legs above the moving-speed threshold).
            pub fn moving_duration(&self) -> Option<Duration> {
                WaypointPath::from(self).moving_duration()
            }

            /// Average speed over the full duration, in m/s.
            pub fn average_speed(&self) -> Option<f64> {
                WaypointPath::from(self).average_speed()
            }

            /// Average speed over moving time only, in m/s.
            pub fn average_moving_speed(&self) -> Option<f64> {
                WaypointPath::from(self).average_moving_speed()
            }

            /// Maximum leg speed, in m/s.
            pub fn max_speed(&self) -> Option<f64> {
                WaypointPath::from(self).max_speed()
            }

            /// Minimum leg speed, in m/s.
            pub fn min_speed(&self) -> Option<f64> {
                WaypointPath::from(self).min_speed()
            }

            /// Timestamp versus speed at the start of each timed leg.
            pub fn speed_profile(&self) -> Vec<SpeedProfilePoint> {
                WaypointPath::from(self).speed_profile()
            }

            /// Mean elevation over points that have elevation data.
            pub fn average_elevation(&self) -> Option<f64> {
                WaypointPath::from(self).average_elevation()
            }

            /// Highest elevation among points with elevation data.
            pub fn max_elevation(&self) -> Option<f64> {
                WaypointPath::from(self).max_elevation()
            }

            /// Lowest elevation among points with elevation data.
            pub fn min_elevation(&self) -> Option<f64> {
                WaypointPath::from(self).min_elevation()
            }

            /// Elevation range from lowest to highest point with elevation data.
            pub fn diff_elevation(&self) -> Option<f64> {
                WaypointPath::from(self).elevation_difference()
            }

            /// Total uphill elevation gain between consecutive elevation-known points.
            pub fn total_ascent(&self) -> Option<f64> {
                WaypointPath::from(self).total_ascent()
            }

            /// Total downhill elevation loss between consecutive elevation-known points.
            pub fn total_descent(&self) -> Option<f64> {
                WaypointPath::from(self).total_descent()
            }

            /// Distance versus elevation for points with elevation data.
            pub fn elevation_profile(&self) -> Vec<ProfilePoint> {
                WaypointPath::from(self).elevation_profile()
            }
        }
    };
}

point_container_stats!(Route, points);
point_container_stats!(TrackSegment, points);

impl Route {
    /// Number of route points.
    pub fn len(&self) -> usize {
        self.points.len()
    }

    /// Whether this route has no points.
    pub fn is_empty(&self) -> bool {
        self.points.is_empty()
    }
}

impl TrackSegment {
    /// Number of track points in this segment.
    pub fn len(&self) -> usize {
        self.points.len()
    }

    /// Whether this segment has no points.
    pub fn is_empty(&self) -> bool {
        self.points.is_empty()
    }
}

impl Index<usize> for Route {
    type Output = Waypoint;

    fn index(&self, index: usize) -> &Self::Output {
        &self.points[index]
    }
}

impl IndexMut<usize> for Route {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.points[index]
    }
}

impl Index<std::ops::Range<usize>> for Route {
    type Output = [Waypoint];

    fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
        &self.points[index]
    }
}

impl<'a> IntoIterator for &'a Route {
    type Item = &'a Waypoint;
    type IntoIter = std::slice::Iter<'a, Waypoint>;

    fn into_iter(self) -> Self::IntoIter {
        self.points.iter()
    }
}

impl Index<usize> for TrackSegment {
    type Output = Waypoint;

    fn index(&self, index: usize) -> &Self::Output {
        &self.points[index]
    }
}

impl IndexMut<usize> for TrackSegment {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.points[index]
    }
}

impl Index<std::ops::Range<usize>> for TrackSegment {
    type Output = [Waypoint];

    fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
        &self.points[index]
    }
}

impl<'a> IntoIterator for &'a TrackSegment {
    type Item = &'a Waypoint;
    type IntoIter = std::slice::Iter<'a, Waypoint>;

    fn into_iter(self) -> Self::IntoIter {
        self.points.iter()
    }
}

impl Track {
    /// Number of track segments.
    pub fn len(&self) -> usize {
        self.segments.len()
    }

    /// Whether this track has no segments.
    pub fn is_empty(&self) -> bool {
        self.segments.is_empty()
    }

    /// Minimum and maximum latitude/longitude across all segments.
    pub fn bounds(&self) -> Option<Bounds> {
        let mut minlat = f64::INFINITY;
        let mut minlon = f64::INFINITY;
        let mut maxlat = f64::NEG_INFINITY;
        let mut maxlon = f64::NEG_INFINITY;
        let mut has_points = false;

        for point in self.segments.iter().flat_map(|segment| segment.points.iter()) {
            has_points = true;
            minlat = minlat.min(point.lat);
            minlon = minlon.min(point.lon);
            maxlat = maxlat.max(point.lat);
            maxlon = maxlon.max(point.lon);
        }

        if has_points {
            Some(Bounds {
                minlat,
                minlon,
                maxlat,
                maxlon,
            })
        } else {
            None
        }
    }

    /// Total horizontal distance summed across segments, in meters.
    pub fn total_distance(&self) -> f64 {
        WaypointPath::from_track(self)
            .iter()
            .map(WaypointPath::total_distance)
            .sum()
    }

    /// Sum of per-segment durations.
    pub fn total_duration(&self) -> Option<Duration> {
        let durations: Vec<Duration> = WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::duration)
            .collect();
        if durations.is_empty() {
            None
        } else {
            Some(durations.into_iter().sum())
        }
    }

    /// Sum of per-segment moving durations.
    pub fn moving_duration(&self) -> Option<Duration> {
        let durations: Vec<Duration> = WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::moving_duration)
            .collect();
        if durations.is_empty() {
            None
        } else {
            Some(durations.into_iter().sum())
        }
    }

    /// Average speed over the full track duration, in m/s.
    pub fn average_speed(&self) -> Option<f64> {
        let secs = self.total_duration()?.as_secs_f64();
        if secs > 0.0 {
            Some(self.total_distance() / secs)
        } else {
            None
        }
    }

    /// Average speed over moving time only, in m/s.
    pub fn average_moving_speed(&self) -> Option<f64> {
        let secs = self.moving_duration()?.as_secs_f64();
        if secs > 0.0 {
            Some(self.total_distance() / secs)
        } else {
            None
        }
    }

    /// Maximum leg speed across all segments, in m/s.
    pub fn max_speed(&self) -> Option<f64> {
        WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::max_speed)
            .reduce(f64::max)
    }

    /// Minimum leg speed across all segments, in m/s.
    pub fn min_speed(&self) -> Option<f64> {
        WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::min_speed)
            .reduce(f64::min)
    }

    /// Concatenated per-segment speed profiles.
    pub fn speed_profile(&self) -> Vec<SpeedProfilePoint> {
        WaypointPath::from_track(self)
            .iter()
            .flat_map(WaypointPath::speed_profile)
            .collect()
    }

    /// Mean elevation over all points with elevation data.
    pub fn average_elevation(&self) -> Option<f64> {
        let elevations: Vec<f64> = self
            .segments
            .iter()
            .flat_map(|segment| segment.points.iter())
            .filter_map(|point| point.ele)
            .collect();
        if elevations.is_empty() {
            None
        } else {
            Some(elevations.iter().sum::<f64>() / elevations.len() as f64)
        }
    }

    /// Highest elevation across all segments.
    pub fn max_elevation(&self) -> Option<f64> {
        WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::max_elevation)
            .reduce(f64::max)
    }

    /// Lowest elevation across all segments.
    pub fn min_elevation(&self) -> Option<f64> {
        WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::min_elevation)
            .reduce(f64::min)
    }

    /// Elevation range across the track.
    pub fn diff_elevation(&self) -> Option<f64> {
        Some(self.max_elevation()? - self.min_elevation()?)
    }

    /// Total uphill gain summed across segments.
    pub fn total_ascent(&self) -> Option<f64> {
        let ascents: Vec<f64> = WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::total_ascent)
            .collect();
        if ascents.is_empty() {
            None
        } else {
            Some(ascents.into_iter().sum())
        }
    }

    /// Total downhill loss summed across segments.
    pub fn total_descent(&self) -> Option<f64> {
        let descents: Vec<f64> = WaypointPath::from_track(self)
            .iter()
            .filter_map(WaypointPath::total_descent)
            .collect();
        if descents.is_empty() {
            None
        } else {
            Some(descents.into_iter().sum())
        }
    }

    /// Continuous distance-elevation profile across all segments.
    pub fn elevation_profile(&self) -> Vec<ProfilePoint> {
        track_elevation_profile(&self.segments)
    }
}

impl Index<usize> for Track {
    type Output = TrackSegment;

    fn index(&self, index: usize) -> &Self::Output {
        &self.segments[index]
    }
}

impl IndexMut<usize> for Track {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.segments[index]
    }
}

impl Index<std::ops::Range<usize>> for Track {
    type Output = [TrackSegment];

    fn index(&self, index: std::ops::Range<usize>) -> &Self::Output {
        &self.segments[index]
    }
}

impl<'a> IntoIterator for &'a Track {
    type Item = &'a TrackSegment;
    type IntoIter = std::slice::Iter<'a, TrackSegment>;

    fn into_iter(self) -> Self::IntoIter {
        self.segments.iter()
    }
}