This commit is contained in:
vcoppe
2026-10-04 19:19:53 +02:00
parent 5e2f506120
commit c04367dd86
10 changed files with 129 additions and 16 deletions
+6
View File
@@ -40,6 +40,8 @@ impl Categories {
pub struct TrackpointCategories {
pub surface: Categories,
pub highway: Categories,
pub sac_scale: Categories,
pub mtb_scale: Categories,
}
#[cfg(test)]
@@ -84,7 +86,11 @@ mod tests {
let mut categories = TrackpointCategories::default();
categories.surface.code("asphalt");
assert_eq!(categories.highway.code("path"), Some(0));
assert_eq!(categories.sac_scale.code("hiking"), Some(0));
assert_eq!(categories.mtb_scale.code("1"), Some(0));
assert_eq!(categories.surface.names(), ["asphalt"]);
assert_eq!(categories.highway.names(), ["path"]);
assert_eq!(categories.sac_scale.names(), ["hiking"]);
assert_eq!(categories.mtb_scale.names(), ["1"]);
}
}
+4
View File
@@ -13,4 +13,8 @@ pub struct Trackpoint {
pub surface: Option<u8>,
/// Code of the highway, see [`crate::TrackpointCategories`].
pub highway: Option<u8>,
/// Code of the SAC hiking scale, see [`crate::TrackpointCategories`].
pub sac_scale: Option<u8>,
/// Code of the mountain biking scale, see [`crate::TrackpointCategories`].
pub mtb_scale: Option<u8>,
}
+35 -9
View File
@@ -31,6 +31,8 @@ enum GPXElement {
Power,
Surface,
Highway,
SacScale,
MtbScale,
Symbol,
Type,
Color,
@@ -50,7 +52,7 @@ fn parse_coordinates(attributes: Attributes<'_>) -> LngLat {
coordinates
}
/// Parses a GPX file. The surface and the highway of the trackpoints are stored as codes of
/// Parses a GPX file. The surface, highway, SAC scale and MTB scale of the trackpoints are stored as codes of
/// `categories`, which learns the values it does not know yet.
pub fn parse(data: &[u8], categories: &mut TrackpointCategories) -> Result<File, Error> {
let mut reader = Reader::from_reader(data);
@@ -103,6 +105,8 @@ pub fn parse(data: &[u8], categories: &mut TrackpointCategories) -> Result<File,
e if e.ends_with("PowerInWatts") => stack.push(GPXElement::Power),
"surface" => stack.push(GPXElement::Surface),
"highway" => stack.push(GPXElement::Highway),
"sac_scale" => stack.push(GPXElement::SacScale),
"mtb_scale" => stack.push(GPXElement::MtbScale),
"sym" => stack.push(GPXElement::Symbol),
"type" => stack.push(GPXElement::Type),
e if e.ends_with("color") => stack.push(GPXElement::Color),
@@ -288,6 +292,18 @@ pub fn parse(data: &[u8], categories: &mut TrackpointCategories) -> Result<File,
trkpt.highway = categories.highway.code(&e);
}
}
Some(GPXElement::SacScale) => {
stack.pop();
if let Some(GPXElement::Trackpoint(trkpt)) = stack.last_mut() {
trkpt.sac_scale = categories.sac_scale.code(&e);
}
}
Some(GPXElement::MtbScale) => {
stack.pop();
if let Some(GPXElement::Trackpoint(trkpt)) = stack.last_mut() {
trkpt.mtb_scale = categories.mtb_scale.code(&e);
}
}
Some(GPXElement::Symbol) => {
stack.pop();
if let Some(GPXElement::Waypoint(wpt)) = stack.last_mut() {
@@ -566,7 +582,7 @@ mod tests {
}
#[test]
fn test_parse_trackpoint_surface_and_highway() {
fn test_parse_trackpoint_surface_highway_and_scales() {
let (gpx, categories) = parse_data_with_categories("with_highway");
let trkseg = &gpx.trk[0].trkseg[0];
@@ -576,23 +592,33 @@ mod tests {
let values: Vec<_> = trkseg
.iter()
.map(|trkpt| {
(
[
names(trkpt.surface, &categories.surface),
names(trkpt.highway, &categories.highway),
)
names(trkpt.sac_scale, &categories.sac_scale),
names(trkpt.mtb_scale, &categories.mtb_scale),
]
})
.collect();
let some = |s: &str| Some(s.to_owned());
assert_eq!(
values,
[
(some("asphalt"), some("residential")),
(some("asphalt"), some("residential")),
(None, None),
(some("gravel"), some("track")),
(some("gravel"), None),
[some("asphalt"), some("residential"), None, None],
[some("asphalt"), some("residential"), None, None],
[None, None, None, None],
[
some("gravel"),
some("track"),
some("mountain_hiking"),
some("1")
],
[some("gravel"), None, some("hiking"), some("1")],
]
);
// the tables are separate
assert_eq!(categories.sac_scale.names(), ["mountain_hiking", "hiking"]);
assert_eq!(categories.mtb_scale.names(), ["1"]);
}
#[test]
@@ -38,6 +38,10 @@ pub struct StatisticsBuffer {
pub surface: Vec<u8>,
/// Highway of the trackpoints, as the surface.
pub highway: Vec<u8>,
/// SAC hiking scale of the trackpoints, as the surface.
pub sac_scale: Vec<u8>,
/// Mountain biking scale of the trackpoints, as the surface.
pub mtb_scale: Vec<u8>,
}
/// 0 for an unknown value, else the code plus one. The categories hold at most 255 values, so it
@@ -78,6 +82,8 @@ impl StatisticsBuffer {
self.power.clear();
self.surface.clear();
self.highway.clear();
self.sac_scale.clear();
self.mtb_scale.clear();
let optional = |value: Option<f64>| value.unwrap_or(f64::NAN);
@@ -117,6 +123,8 @@ impl StatisticsBuffer {
self.power.push(optional(trkpt.power.map(f64::from)));
self.surface.push(unknown_or_next(trkpt.surface));
self.highway.push(unknown_or_next(trkpt.highway));
self.sac_scale.push(unknown_or_next(trkpt.sac_scale));
self.mtb_scale.push(unknown_or_next(trkpt.mtb_scale));
}
self.global.merge(&stats.global);
}
@@ -196,6 +204,8 @@ mod tests {
buffer.power.len(),
buffer.surface.len(),
buffer.highway.len(),
buffer.sac_scale.len(),
buffer.mtb_scale.len(),
]
}
@@ -304,6 +314,8 @@ mod tests {
// 0 for the trackpoints that have none, else the code of the engine plus one
assert_eq!(buffer.surface, [1, 1, 0, 2, 2]);
assert_eq!(buffer.highway, [1, 1, 0, 2, 0]);
assert_eq!(buffer.sac_scale, [0, 0, 0, 1, 2]);
assert_eq!(buffer.mtb_scale, [0, 0, 0, 1, 1]);
assert!(all_lengths(&buffer).iter().all(|len| *len == 5));
}
@@ -313,12 +325,16 @@ mod tests {
let mut point = segment[0].clone();
point.surface = Some(7);
point.highway = Some(254);
point.sac_scale = Some(0);
point.mtb_scale = Some(3);
segment.splice(0, 1, vec![point]);
let s = Statistics::compute(&segment);
let mut buffer = StatisticsBuffer::default();
buffer.update(&[(&segment, &s)]);
assert_eq!((buffer.surface[0], buffer.highway[0]), (8, 255));
assert_eq!((buffer.sac_scale[0], buffer.mtb_scale[0]), (1, 4));
assert_eq!((buffer.surface[1], buffer.highway[1]), (0, 0));
assert_eq!((buffer.sac_scale[1], buffer.mtb_scale[1]), (0, 0));
}
#[test]
@@ -329,9 +345,12 @@ mod tests {
assert_eq!(buffer.surface.len(), s.local.len());
assert!(buffer.surface.iter().all(|c| *c == 0));
assert!(buffer.highway.iter().all(|c| *c == 0));
assert!(buffer.sac_scale.iter().all(|c| *c == 0));
assert!(buffer.mtb_scale.iter().all(|c| *c == 0));
// and they are reset by the next update
buffer.update(&[]);
assert!(buffer.surface.is_empty() && buffer.highway.is_empty());
assert!(buffer.sac_scale.is_empty() && buffer.mtb_scale.is_empty());
}
#[test]
+6
View File
@@ -1252,6 +1252,12 @@ mod tests {
// the trackpoints of the selection refer to them
assert_eq!(engine.statistics().surface, [1, 1, 0, 2, 2]);
assert_eq!(engine.statistics().highway, [1, 1, 0, 2, 0]);
assert_eq!(
engine.categories().sac_scale.names(),
["mountain_hiking", "hiking"]
);
assert_eq!(engine.statistics().sac_scale, [0, 0, 0, 1, 2]);
assert_eq!(engine.statistics().mtb_scale, [0, 0, 0, 1, 1]);
// a second file goes on with the same table
load(&mut engine, "data/with_surface.gpx");