From b0c691b78916f966ab0b400a228d2679626a92e4 Mon Sep 17 00:00:00 2001 From: vcoppe Date: Tue, 16 Apr 2024 09:54:41 +0200 Subject: [PATCH] finish gpx parsing --- gpx/src/io.ts | 84 +- gpx/src/types.ts | 16 + gpx/test-data/with_cad.gpx | 2 +- gpx/test-data/with_hr.gpx | 2 +- .../{with_power.gpx => with_power_1.gpx} | 2 +- gpx/test-data/with_power_2.gpx | 500 +++++ gpx/test-data/with_power_3.gpx | 658 +++++++ gpx/test-data/with_style.gpx | 526 +++--- gpx/test-data/with_surface.gpx | 1632 +++++++++-------- gpx/test-data/with_temp.gpx | 2 +- gpx/test-data/with_tracks.gpx | 506 ++--- gpx/test/io.test.ts | 207 ++- 12 files changed, 2797 insertions(+), 1340 deletions(-) rename gpx/test-data/{with_power.gpx => with_power_1.gpx} (99%) create mode 100644 gpx/test-data/with_power_2.gpx create mode 100644 gpx/test-data/with_power_3.gpx diff --git a/gpx/src/io.ts b/gpx/src/io.ts index 3d83dbb9..b4f7b9dd 100644 --- a/gpx/src/io.ts +++ b/gpx/src/io.ts @@ -1,5 +1,6 @@ import { XMLParser } from "fast-xml-parser"; -import { Author, GPXFile, Link, Metadata, Track, TrackPoint, TrackSegment, Waypoint } from "./types"; +import { Author, GPXFile, Link, Metadata, Track, TrackPoint, TrackPointExtensions, TrackSegment, TrackStyleExtension, Waypoint } from "./types"; +import { parse } from "path"; const arrayTypes = ['trk', 'trkseg', 'trkpt', 'wpt']; @@ -83,12 +84,12 @@ function parseLink(link: any): Link { function parseWaypoint(waypoint: any): Waypoint { const result: Waypoint = { - lat: waypoint.lat, - lon: waypoint.lon, + lat: parseFloat(waypoint.lat), + lon: parseFloat(waypoint.lon), }; if (waypoint.ele) { - result.ele = waypoint.ele; + result.ele = parseFloat(waypoint.ele); } if (waypoint.time) { @@ -151,6 +152,28 @@ function parseTrack(track: any): Track { result.type = track.type; } + if (track.extensions && track.extensions.hasOwnProperty('gpx_style:line')) { + result.style = parseTrackStyleExtension(track.extensions['gpx_style:line']); + } + + return result; +} + +function parseTrackStyleExtension(extensions: any): TrackStyleExtension { + const result: TrackStyleExtension = {}; + + if (extensions.color) { + result.color = extensions.color; + } + + if (extensions.opacity) { + result.opacity = parseFloat(extensions.opacity); + } + + if (extensions.weight) { + result.weight = parseFloat(extensions.weight); + } + return result; } @@ -162,21 +185,64 @@ function parseTrackSegment(segment: any): TrackSegment { function parseTrackPoint(point: any): TrackPoint { const result: TrackPoint = { - lat: point.lat, - lon: point.lon, + lat: parseFloat(point.lat), + lon: parseFloat(point.lon), }; if (point.ele) { - result.ele = point.ele; + result.ele = parseFloat(point.ele); } if (point.time) { result.time = new Date(point.time); } + if (point.extensions) { + result.extensions = parseTrackPointExtensions(point.extensions); + } + return result; } -import * as fs from 'fs'; +function parseTrackPointExtensions(extensions: any): TrackPointExtensions { + const result: TrackPointExtensions = {}; -console.log(parseGPX(fs.readFileSync("test-data/simple.gpx", 'utf8'))); \ No newline at end of file + if (extensions.hasOwnProperty('gpxtpx:TrackPointExtension')) { + const gpxtpxExtensions = extensions['gpxtpx:TrackPointExtension']; + + if (gpxtpxExtensions.hasOwnProperty('gpxtpx:hr')) { + result.hr = parseFloat(gpxtpxExtensions['gpxtpx:hr']); + } + + if (gpxtpxExtensions.hasOwnProperty('gpxtpx:cad')) { + result.cad = parseFloat(gpxtpxExtensions['gpxtpx:cad']); + } + + if (gpxtpxExtensions.hasOwnProperty('gpxtpx:atemp')) { + result.atemp = parseFloat(gpxtpxExtensions['gpxtpx:atemp']); + } + + if (gpxtpxExtensions.hasOwnProperty('gpxtpx:Extensions')) { + const gpxtpxInnerExtensions = gpxtpxExtensions['gpxtpx:Extensions']; + + if (gpxtpxInnerExtensions.surface) { + result.surface = gpxtpxInnerExtensions.surface; + } + } + } + + if (extensions.power) { + result.power = parseFloat(extensions.power); + } else if (extensions.hasOwnProperty('gpxpx:PowerExtension')) { + const gpxpxExtensions = extensions['gpxpx:PowerExtension']; + + if (gpxpxExtensions.hasOwnProperty('gpxpx:PowerInWatts')) { + result.power = parseFloat(gpxpxExtensions['gpxpx:PowerInWatts']); + } + } else if (extensions.hasOwnProperty('gpxpx:PowerInWatts')) { + result.power = parseFloat(extensions['gpxpx:PowerInWatts']); + } + + + return result; +} \ No newline at end of file diff --git a/gpx/src/types.ts b/gpx/src/types.ts index 20c44151..b08202d0 100644 --- a/gpx/src/types.ts +++ b/gpx/src/types.ts @@ -40,6 +40,13 @@ export type Track = { link?: Link; type?: string; trkseg: TrackSegment[]; + style?: TrackStyleExtension; +}; + +export type TrackStyleExtension = { + color?: string; + opacity?: number; + weight?: number; }; export type TrackSegment = { @@ -51,6 +58,15 @@ export type TrackPoint = { lon: number; ele?: number; time?: Date; + extensions?: TrackPointExtensions; +}; + +export type TrackPointExtensions = { + hr?: number; + cad?: number; + atemp?: number; + power?: number; + surface?: string; }; export type Author = { diff --git a/gpx/test-data/with_cad.gpx b/gpx/test-data/with_cad.gpx index 76107f82..1ac66703 100644 --- a/gpx/test-data/with_cad.gpx +++ b/gpx/test-data/with_cad.gpx @@ -651,7 +651,7 @@ 129.5 - 80 + 90 diff --git a/gpx/test-data/with_hr.gpx b/gpx/test-data/with_hr.gpx index 52b67e10..f2c566a5 100644 --- a/gpx/test-data/with_hr.gpx +++ b/gpx/test-data/with_hr.gpx @@ -651,7 +651,7 @@ 129.5 - 150 + 160 diff --git a/gpx/test-data/with_power.gpx b/gpx/test-data/with_power_1.gpx similarity index 99% rename from gpx/test-data/with_power.gpx rename to gpx/test-data/with_power_1.gpx index 095c8f41..5c640776 100644 --- a/gpx/test-data/with_power.gpx +++ b/gpx/test-data/with_power_1.gpx @@ -492,7 +492,7 @@ 129.5 - 200 + 210 diff --git a/gpx/test-data/with_power_2.gpx b/gpx/test-data/with_power_2.gpx new file mode 100644 index 00000000..09ba84da --- /dev/null +++ b/gpx/test-data/with_power_2.gpx @@ -0,0 +1,500 @@ + + + + with_power + + gpx.studio + + + + + with_power + Cycling + + + 109.0 + + 200 + + + + 110.8 + + 200 + + + + 110.3 + + 200 + + + + 110.0 + + 200 + + + + 110.3 + + 200 + + + + 109.3 + + 200 + + + + 107.0 + + 200 + + + + 106.0 + + 200 + + + + 108.5 + + 200 + + + + 109.8 + + 200 + + + + 110.8 + + 200 + + + + 112.0 + + 200 + + + + 112.8 + + 200 + + + + 113.5 + + 200 + + + + 114.3 + + 200 + + + + 115.3 + + 200 + + + + 114.8 + + 200 + + + + 114.3 + + 200 + + + + 114.3 + + 200 + + + + 114.5 + + 200 + + + + 115.0 + + 200 + + + + 115.8 + + 200 + + + + 115.8 + + 200 + + + + 116.0 + + 200 + + + + 115.8 + + 200 + + + + 115.5 + + 200 + + + + 114.5 + + 200 + + + + 112.5 + + 200 + + + + 110.8 + + 200 + + + + 109.0 + + 200 + + + + 106.3 + + 200 + + + + 104.3 + + 200 + + + + 104.0 + + 200 + + + + 103.8 + + 200 + + + + 103.3 + + 200 + + + + 103.5 + + 200 + + + + 103.8 + + 200 + + + + 104.8 + + 200 + + + + 105.8 + + 200 + + + + 106.8 + + 200 + + + + 107.8 + + 200 + + + + 108.5 + + 200 + + + + 109.3 + + 200 + + + + 110.0 + + 200 + + + + 110.5 + + 200 + + + + 110.8 + + 200 + + + + 111.8 + + 200 + + + + 112.8 + + 200 + + + + 113.3 + + 200 + + + + 113.5 + + 200 + + + + 113.5 + + 200 + + + + 113.8 + + 200 + + + + 114.8 + + 200 + + + + 115.8 + + 200 + + + + 118.5 + + 200 + + + + 119.5 + + 200 + + + + 121.3 + + 200 + + + + 122.0 + + 200 + + + + 122.8 + + 200 + + + + 123.5 + + 200 + + + + 126.3 + + 200 + + + + 128.0 + + 200 + + + + 129.0 + + 200 + + + + 130.0 + + 200 + + + + 130.8 + + 200 + + + + 131.8 + + 200 + + + + 132.3 + + 200 + + + + 132.8 + + 200 + + + + 135.8 + + 200 + + + + 135.5 + + 200 + + + + 132.5 + + 200 + + + + 134.0 + + 200 + + + + 136.8 + + 200 + + + + 137.5 + + 200 + + + + 137.3 + + 200 + + + + 137.5 + + 200 + + + + 137.3 + + 200 + + + + 134.8 + + 200 + + + + 132.3 + + 200 + + + + 129.5 + + 210 + + + + + \ No newline at end of file diff --git a/gpx/test-data/with_power_3.gpx b/gpx/test-data/with_power_3.gpx new file mode 100644 index 00000000..3b9e7935 --- /dev/null +++ b/gpx/test-data/with_power_3.gpx @@ -0,0 +1,658 @@ + + + + with_power + + gpx.studio + + + + + with_power + Cycling + + + 109.0 + + + 200 + + + + + 110.8 + + + 200 + + + + + 110.3 + + + 200 + + + + + 110.0 + + + 200 + + + + + 110.3 + + + 200 + + + + + 109.3 + + + 200 + + + + + 107.0 + + + 200 + + + + + 106.0 + + + 200 + + + + + 108.5 + + + 200 + + + + + 109.8 + + + 200 + + + + + 110.8 + + + 200 + + + + + 112.0 + + + 200 + + + + + 112.8 + + + 200 + + + + + 113.5 + + + 200 + + + + + 114.3 + + + 200 + + + + + 115.3 + + + 200 + + + + + 114.8 + + + 200 + + + + + 114.3 + + + 200 + + + + + 114.3 + + + 200 + + + + + 114.5 + + + 200 + + + + + 115.0 + + + 200 + + + + + 115.8 + + + 200 + + + + + 115.8 + + + 200 + + + + + 116.0 + + + 200 + + + + + 115.8 + + + 200 + + + + + 115.5 + + + 200 + + + + + 114.5 + + + 200 + + + + + 112.5 + + + 200 + + + + + 110.8 + + + 200 + + + + + 109.0 + + + 200 + + + + + 106.3 + + + 200 + + + + + 104.3 + + + 200 + + + + + 104.0 + + + 200 + + + + + 103.8 + + + 200 + + + + + 103.3 + + + 200 + + + + + 103.5 + + + 200 + + + + + 103.8 + + + 200 + + + + + 104.8 + + + 200 + + + + + 105.8 + + + 200 + + + + + 106.8 + + + 200 + + + + + 107.8 + + + 200 + + + + + 108.5 + + + 200 + + + + + 109.3 + + + 200 + + + + + 110.0 + + + 200 + + + + + 110.5 + + + 200 + + + + + 110.8 + + + 200 + + + + + 111.8 + + + 200 + + + + + 112.8 + + + 200 + + + + + 113.3 + + + 200 + + + + + 113.5 + + + 200 + + + + + 113.5 + + + 200 + + + + + 113.8 + + + 200 + + + + + 114.8 + + + 200 + + + + + 115.8 + + + 200 + + + + + 118.5 + + + 200 + + + + + 119.5 + + + 200 + + + + + 121.3 + + + 200 + + + + + 122.0 + + + 200 + + + + + 122.8 + + + 200 + + + + + 123.5 + + + 200 + + + + + 126.3 + + + 200 + + + + + 128.0 + + + 200 + + + + + 129.0 + + + 200 + + + + + 130.0 + + + 200 + + + + + 130.8 + + + 200 + + + + + 131.8 + + + 200 + + + + + 132.3 + + + 200 + + + + + 132.8 + + + 200 + + + + + 135.8 + + + 200 + + + + + 135.5 + + + 200 + + + + + 132.5 + + + 200 + + + + + 134.0 + + + 200 + + + + + 136.8 + + + 200 + + + + + 137.5 + + + 200 + + + + + 137.3 + + + 200 + + + + + 137.5 + + + 200 + + + + + 137.3 + + + 200 + + + + + 134.8 + + + 200 + + + + + 132.3 + + + 200 + + + + + 129.5 + + 210 + + + + + \ No newline at end of file diff --git a/gpx/test-data/with_style.gpx b/gpx/test-data/with_style.gpx index 125b2ec1..f34ebfdb 100644 --- a/gpx/test-data/with_style.gpx +++ b/gpx/test-data/with_style.gpx @@ -1,263 +1,267 @@ - - - with_style - - gpx.studio - - - - - with_style - Cycling - - - 2d3ee9 - 0.36 - 5 - - - - - 109.0 - - - 110.8 - - - 110.3 - - - 110.0 - - - 110.3 - - - 109.3 - - - 107.0 - - - 106.0 - - - 108.5 - - - 109.8 - - - 110.8 - - - 112.0 - - - 112.8 - - - 113.5 - - - 114.3 - - - 115.3 - - - 114.8 - - - 114.3 - - - 114.3 - - - 114.5 - - - 115.0 - - - 115.8 - - - 115.8 - - - 116.0 - - - 115.8 - - - 115.5 - - - 114.5 - - - 112.5 - - - 110.8 - - - 109.0 - - - 106.3 - - - 104.3 - - - 104.0 - - - 103.8 - - - 103.3 - - - 103.5 - - - 103.8 - - - 104.8 - - - 105.8 - - - 106.8 - - - 107.8 - - - 108.5 - - - 109.3 - - - 110.0 - - - 110.5 - - - 110.8 - - - 111.8 - - - 112.8 - - - 113.3 - - - 113.5 - - - 113.5 - - - 113.8 - - - 114.8 - - - 115.8 - - - 118.5 - - - 119.5 - - - 121.3 - - - 122.0 - - - 122.8 - - - 123.5 - - - 126.3 - - - 128.0 - - - 129.0 - - - 130.0 - - - 130.8 - - - 131.8 - - - 132.3 - - - 132.8 - - - 135.8 - - - 135.5 - - - 132.5 - - - 134.0 - - - 136.8 - - - 137.5 - - - 137.3 - - - 137.5 - - - 137.3 - - - 134.8 - - - 132.3 - - - 129.5 - - - + + + with_style + + gpx.studio + + + + + with_style + Cycling + + + 2d3ee9 + 0.5 + 5 + + + + + 109.0 + + + 110.8 + + + 110.3 + + + 110.0 + + + 110.3 + + + 109.3 + + + 107.0 + + + 106.0 + + + 108.5 + + + 109.8 + + + 110.8 + + + 112.0 + + + 112.8 + + + 113.5 + + + 114.3 + + + 115.3 + + + 114.8 + + + 114.3 + + + 114.3 + + + 114.5 + + + 115.0 + + + 115.8 + + + 115.8 + + + 116.0 + + + 115.8 + + + 115.5 + + + 114.5 + + + 112.5 + + + 110.8 + + + 109.0 + + + 106.3 + + + 104.3 + + + 104.0 + + + 103.8 + + + 103.3 + + + 103.5 + + + 103.8 + + + 104.8 + + + 105.8 + + + 106.8 + + + 107.8 + + + 108.5 + + + 109.3 + + + 110.0 + + + 110.5 + + + 110.8 + + + 111.8 + + + 112.8 + + + 113.3 + + + 113.5 + + + 113.5 + + + 113.8 + + + 114.8 + + + 115.8 + + + 118.5 + + + 119.5 + + + 121.3 + + + 122.0 + + + 122.8 + + + 123.5 + + + 126.3 + + + 128.0 + + + 129.0 + + + 130.0 + + + 130.8 + + + 131.8 + + + 132.3 + + + 132.8 + + + 135.8 + + + 135.5 + + + 132.5 + + + 134.0 + + + 136.8 + + + 137.5 + + + 137.3 + + + 137.5 + + + 137.3 + + + 134.8 + + + 132.3 + + + 129.5 + + + \ No newline at end of file diff --git a/gpx/test-data/with_surface.gpx b/gpx/test-data/with_surface.gpx index 3e977b29..a4d53910 100644 --- a/gpx/test-data/with_surface.gpx +++ b/gpx/test-data/with_surface.gpx @@ -1,816 +1,820 @@ - - - with_surface - - gpx.studio - - - - - with_surface - Cycling - - - 109.0 - - - - asphalt - - - - - - 110.8 - - - - asphalt - - - - - - 110.3 - - - - asphalt - - - - - - 110.0 - - - - asphalt - - - - - - 110.3 - - - - asphalt - - - - - - 109.3 - - - - asphalt - - - - - - 107.0 - - - - asphalt - - - - - - 106.0 - - - - asphalt - - - - - - 108.5 - - - - asphalt - - - - - - 109.8 - - - - asphalt - - - - - - 110.8 - - - - asphalt - - - - - - 112.0 - - - - asphalt - - - - - - 112.8 - - - - asphalt - - - - - - 113.5 - - - - asphalt - - - - - - 114.3 - - - - asphalt - - - - - - 115.3 - - - - asphalt - - - - - - 114.8 - - - - asphalt - - - - - - 114.3 - - - - asphalt - - - - - - 114.3 - - - - asphalt - - - - - - 114.5 - - - - asphalt - - - - - - 115.0 - - - - asphalt - - - - - - 115.8 - - - - asphalt - - - - - - 115.8 - - - - asphalt - - - - - - 116.0 - - - - asphalt - - - - - - 115.8 - - - - asphalt - - - - - - 115.5 - - - - asphalt - - - - - - 114.5 - - - - asphalt - - - - - - 112.5 - - - - asphalt - - - - - - 110.8 - - - - asphalt - - - - - - 109.0 - - - - asphalt - - - - - - 106.3 - - - - asphalt - - - - - - 104.3 - - - - asphalt - - - - - - 104.0 - - - - asphalt - - - - - - 103.8 - - - - asphalt - - - - - - 103.3 - - - - asphalt - - - - - - 103.5 - - - - asphalt - - - - - - 103.8 - - - - asphalt - - - - - - 104.8 - - - - asphalt - - - - - - 105.8 - - - - asphalt - - - - - - 106.8 - - - - asphalt - - - - - - 107.8 - - - - asphalt - - - - - - 108.5 - - - - asphalt - - - - - - 109.3 - - - - asphalt - - - - - - 110.0 - - - - asphalt - - - - - - 110.5 - - - - asphalt - - - - - - 110.8 - - - - asphalt - - - - - - 111.8 - - - - asphalt - - - - - - 112.8 - - - - asphalt - - - - - - 113.3 - - - - asphalt - - - - - - 113.5 - - - - asphalt - - - - - - 113.5 - - - - asphalt - - - - - - 113.8 - - - - asphalt - - - - - - 114.8 - - - - asphalt - - - - - - 115.8 - - - - asphalt - - - - - - 118.5 - - - - asphalt - - - - - - 119.5 - - - - asphalt - - - - - - 121.3 - - - - asphalt - - - - - - 122.0 - - - - asphalt - - - - - - 122.8 - - - - asphalt - - - - - - 123.5 - - - - asphalt - - - - - - 126.3 - - - - asphalt - - - - - - 128.0 - - - - asphalt - - - - - - 129.0 - - - - asphalt - - - - - - 130.0 - - - - asphalt - - - - - - 130.8 - - - - asphalt - - - - - - 131.8 - - - - asphalt - - - - - - 132.3 - - - - asphalt - - - - - - 132.8 - - - - asphalt - - - - - - 135.8 - - - - asphalt - - - - - - 135.5 - - - - asphalt - - - - - - 132.5 - - - - asphalt - - - - - - 134.0 - - - - asphalt - - - - - - 136.8 - - - - asphalt - - - - - - 137.5 - - - - asphalt - - - - - - 137.3 - - - - asphalt - - - - - - 137.5 - - - - asphalt - - - - - - 137.3 - - - - asphalt - - - - - - 134.8 - - - - asphalt - - - - - - 132.3 - - - - asphalt - - - - - - 129.5 - - - - asphalt - - - - - - + + + with_surface + + gpx.studio + + + + + with_surface + Cycling + + + 109.0 + + + + asphalt + + + + + + 110.8 + + + + asphalt + + + + + + 110.3 + + + + asphalt + + + + + + 110.0 + + + + asphalt + + + + + + 110.3 + + + + asphalt + + + + + + 109.3 + + + + asphalt + + + + + + 107.0 + + + + asphalt + + + + + + 106.0 + + + + asphalt + + + + + + 108.5 + + + + asphalt + + + + + + 109.8 + + + + asphalt + + + + + + 110.8 + + + + asphalt + + + + + + 112.0 + + + + asphalt + + + + + + 112.8 + + + + asphalt + + + + + + 113.5 + + + + asphalt + + + + + + 114.3 + + + + asphalt + + + + + + 115.3 + + + + asphalt + + + + + + 114.8 + + + + asphalt + + + + + + 114.3 + + + + asphalt + + + + + + 114.3 + + + + asphalt + + + + + + 114.5 + + + + asphalt + + + + + + 115.0 + + + + asphalt + + + + + + 115.8 + + + + asphalt + + + + + + 115.8 + + + + asphalt + + + + + + 116.0 + + + + asphalt + + + + + + 115.8 + + + + asphalt + + + + + + 115.5 + + + + asphalt + + + + + + 114.5 + + + + asphalt + + + + + + 112.5 + + + + asphalt + + + + + + 110.8 + + + + asphalt + + + + + + 109.0 + + + + asphalt + + + + + + 106.3 + + + + asphalt + + + + + + 104.3 + + + + asphalt + + + + + + 104.0 + + + + asphalt + + + + + + 103.8 + + + + asphalt + + + + + + 103.3 + + + + asphalt + + + + + + 103.5 + + + + asphalt + + + + + + 103.8 + + + + asphalt + + + + + + 104.8 + + + + asphalt + + + + + + 105.8 + + + + asphalt + + + + + + 106.8 + + + + asphalt + + + + + + 107.8 + + + + asphalt + + + + + + 108.5 + + + + asphalt + + + + + + 109.3 + + + + asphalt + + + + + + 110.0 + + + + asphalt + + + + + + 110.5 + + + + asphalt + + + + + + 110.8 + + + + asphalt + + + + + + 111.8 + + + + asphalt + + + + + + 112.8 + + + + asphalt + + + + + + 113.3 + + + + asphalt + + + + + + 113.5 + + + + asphalt + + + + + + 113.5 + + + + asphalt + + + + + + 113.8 + + + + asphalt + + + + + + 114.8 + + + + asphalt + + + + + + 115.8 + + + + asphalt + + + + + + 118.5 + + + + asphalt + + + + + + 119.5 + + + + asphalt + + + + + + 121.3 + + + + asphalt + + + + + + 122.0 + + + + asphalt + + + + + + 122.8 + + + + asphalt + + + + + + 123.5 + + + + asphalt + + + + + + 126.3 + + + + asphalt + + + + + + 128.0 + + + + asphalt + + + + + + 129.0 + + + + asphalt + + + + + + 130.0 + + + + asphalt + + + + + + 130.8 + + + + asphalt + + + + + + 131.8 + + + + asphalt + + + + + + 132.3 + + + + asphalt + + + + + + 132.8 + + + + asphalt + + + + + + 135.8 + + + + asphalt + + + + + + 135.5 + + + + asphalt + + + + + + 132.5 + + + + asphalt + + + + + + 134.0 + + + + asphalt + + + + + + 136.8 + + + + asphalt + + + + + + 137.5 + + + + asphalt + + + + + + 137.3 + + + + asphalt + + + + + + 137.5 + + + + asphalt + + + + + + 137.3 + + + + asphalt + + + + + + 134.8 + + + + asphalt + + + + + + 132.3 + + + + asphalt + + + + + + 129.5 + + + + cobblestone + + + + + + \ No newline at end of file diff --git a/gpx/test-data/with_temp.gpx b/gpx/test-data/with_temp.gpx index 54a331b2..b827235c 100644 --- a/gpx/test-data/with_temp.gpx +++ b/gpx/test-data/with_temp.gpx @@ -651,7 +651,7 @@ 129.5 - 21 + 22 diff --git a/gpx/test-data/with_tracks.gpx b/gpx/test-data/with_tracks.gpx index 418e8436..937b53e5 100644 --- a/gpx/test-data/with_tracks.gpx +++ b/gpx/test-data/with_tracks.gpx @@ -1,253 +1,257 @@ - - - with_tracks - - gpx.studio - - - - - with_segments - Cycling - - - 109.0 - - - 110.8 - - - 110.3 - - - 110.0 - - - 110.3 - - - 109.3 - - - 107.0 - - - 106.0 - - - 108.5 - - - 109.8 - - - 110.8 - - - 112.0 - - - 112.8 - - - 113.5 - - - 114.3 - - - 115.3 - - - 114.8 - - - 114.3 - - - 114.3 - - - 114.5 - - - 115.0 - - - 115.8 - - - 115.8 - - - 116.0 - - - 115.8 - - - 115.5 - - - 114.5 - - - 112.5 - - - 110.8 - - - 109.0 - - - 106.3 - - - 104.3 - - - 104.0 - - - 103.8 - - - 103.3 - - - 103.5 - - - 103.8 - - - 104.8 - - - 105.8 - - - 106.8 - - - 107.8 - - - 108.5 - - - 109.3 - - - 110.0 - - - 110.5 - - - 110.8 - - - 111.8 - - - 112.8 - - - 113.3 - - - - - with_segments - Cycling - - - 115.5 - - - 115.8 - - - 118.5 - - - 119.5 - - - 121.3 - - - 122.0 - - - 122.8 - - - 123.5 - - - 126.3 - - - 128.0 - - - 129.0 - - - 130.0 - - - 130.8 - - - 131.8 - - - 132.3 - - - 132.8 - - - 135.8 - - - 135.5 - - - 132.5 - - - 134.0 - - - 136.8 - - - 137.5 - - - 137.3 - - - 137.5 - - - 137.3 - - - 134.8 - - - 132.3 - - - 129.5 - - - + + + with_tracks + + gpx.studio + + + + + track 1 + Cycling + + + 109.0 + + + 110.8 + + + 110.3 + + + 110.0 + + + 110.3 + + + 109.3 + + + 107.0 + + + 106.0 + + + 108.5 + + + 109.8 + + + 110.8 + + + 112.0 + + + 112.8 + + + 113.5 + + + 114.3 + + + 115.3 + + + 114.8 + + + 114.3 + + + 114.3 + + + 114.5 + + + 115.0 + + + 115.8 + + + 115.8 + + + 116.0 + + + 115.8 + + + 115.5 + + + 114.5 + + + 112.5 + + + 110.8 + + + 109.0 + + + 106.3 + + + 104.3 + + + 104.0 + + + 103.8 + + + 103.3 + + + 103.5 + + + 103.8 + + + 104.8 + + + 105.8 + + + 106.8 + + + 107.8 + + + 108.5 + + + 109.3 + + + 110.0 + + + 110.5 + + + 110.8 + + + 111.8 + + + 112.8 + + + 113.3 + + + + + track 2 + Cycling + + + 115.5 + + + 115.8 + + + 118.5 + + + 119.5 + + + 121.3 + + + 122.0 + + + 122.8 + + + 123.5 + + + 126.3 + + + 128.0 + + + 129.0 + + + 130.0 + + + 130.8 + + + 131.8 + + + 132.3 + + + 132.8 + + + 135.8 + + + 135.5 + + + 132.5 + + + 134.0 + + + 136.8 + + + 137.5 + + + 137.3 + + + 137.5 + + + 137.3 + + + 134.8 + + + 132.3 + + + 129.5 + + + \ No newline at end of file diff --git a/gpx/test/io.test.ts b/gpx/test/io.test.ts index b345e3f1..7bc59b6d 100644 --- a/gpx/test/io.test.ts +++ b/gpx/test/io.test.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import { parseGPX } from '../src/io'; describe("Parsing tests", () => { - it("Simple file", () => { + it("Simple", () => { const path = "test-data/simple.gpx"; const data = fs.readFileSync(path, 'utf8'); const result = parseGPX(data); @@ -29,5 +29,210 @@ describe("Parsing tests", () => { expect(point).toHaveProperty('lon'); expect(point).toHaveProperty('ele'); } + + expect(segment.trkpt[0].lat).toBe(50.790867); + expect(segment.trkpt[0].lon).toBe(4.404968); + expect(segment.trkpt[0].ele).toBe(109.0); + }); + + it("Multiple tracks", () => { + const path = "test-data/with_tracks.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + expect(result.tracks.length).toBe(2); + + const track_1 = result.tracks[0]; + expect(track_1.name).toBe("track 1"); + expect(track_1.trkseg.length).toBe(1); + expect(track_1.trkseg[0].trkpt.length).toBe(49); + + const track_2 = result.tracks[1]; + expect(track_2.name).toBe("track 2"); + expect(track_2.trkseg.length).toBe(1); + expect(track_2.trkseg[0].trkpt.length).toBe(28); + }); + + it("Multiple segments", () => { + const path = "test-data/with_segments.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + expect(result.tracks.length).toBe(1); + + const track = result.tracks[0]; + expect(track.trkseg.length).toBe(2); + expect(track.trkseg[0].trkpt.length).toBe(49); + expect(track.trkseg[1].trkpt.length).toBe(28); + }); + + it("Waypoint", () => { + const path = "test-data/with_waypoint.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + expect(result.waypoints.length).toBe(1); + + const waypoint = result.waypoints[0]; + expect(waypoint.lat).toBe(50.7836710064975); + expect(waypoint.lon).toBe(4.410764082658738); + expect(waypoint.ele).toBe(122.0); + expect(waypoint.name).toBe("Waypoint"); + expect(waypoint.cmt).toBe("Comment"); + expect(waypoint.desc).toBe("Description"); + expect(waypoint.sym).toBe("Bike Trail"); + }); + + it("Time", () => { + const path = "test-data/with_time.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i].time).toBeInstanceOf(Date); + } + + expect(segment.trkpt[0].time).toEqual(new Date("2023-12-31T23:00:00.000Z")); + expect(segment.trkpt[segment.trkpt.length - 1].time).toEqual(new Date("2023-12-31T23:06:40.567Z")); + }); + + it("Heart rate", () => { + const path = "test-data/with_hr.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('hr'); + } + + expect(segment.trkpt[0].extensions.hr).toBe(150); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.hr).toBe(160); + }); + + it("Cadence", () => { + const path = "test-data/with_cad.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('cad'); + } + + expect(segment.trkpt[0].extensions.cad).toBe(80); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.cad).toBe(90); + }); + + it("Temperature", () => { + const path = "test-data/with_temp.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('atemp'); + } + + expect(segment.trkpt[0].extensions.atemp).toBe(21); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.atemp).toBe(22); + }); + + it("Power 1", () => { + const path = "test-data/with_power_1.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('power'); + } + + expect(segment.trkpt[0].extensions.power).toBe(200); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.power).toBe(210); + }); + + it("Power 2", () => { + const path = "test-data/with_power_2.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('power'); + } + + expect(segment.trkpt[0].extensions.power).toBe(200); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.power).toBe(210); + }); + + it("Power 3", () => { + const path = "test-data/with_power_3.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('power'); + } + + expect(segment.trkpt[0].extensions.power).toBe(200); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.power).toBe(210); + }); + + it("Surface", () => { + const path = "test-data/with_surface.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + const segment = track.trkseg[0]; + + for (let i = 0; i < segment.trkpt.length; i++) { + expect(segment.trkpt[i]).toHaveProperty('extensions'); + expect(segment.trkpt[i].extensions).toHaveProperty('surface'); + } + + expect(segment.trkpt[0].extensions.surface).toBe("asphalt"); + expect(segment.trkpt[segment.trkpt.length - 1].extensions.surface).toBe("cobblestone"); + }); + + it("Track style", () => { + const path = "test-data/with_style.gpx"; + const data = fs.readFileSync(path, 'utf8'); + const result = parseGPX(data); + + const track = result.tracks[0]; + + expect(track).toHaveProperty('style'); + + expect(track.style).toHaveProperty('color'); + expect(track.style).toHaveProperty('opacity'); + expect(track.style).toHaveProperty('weight'); + + expect(track.style.color).toBe("2d3ee9"); + expect(track.style.opacity).toBe(0.5); + expect(track.style.weight).toBe(5); }); }); \ No newline at end of file