Merge branch 'dev' into elevation-tool

This commit is contained in:
vcoppe
2024-09-04 15:18:44 +02:00
609 changed files with 76321 additions and 6965 deletions

View File

@@ -1,198 +0,0 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
/** @type {import('jest').Config} */
const config = {
// All imported modules in your tests should be mocked automatically
// automock: false,
// Stop running tests after `n` failures
// bail: 0,
// The directory where Jest should store its cached dependency information
// cacheDirectory: "/private/var/folders/hf/0lj0fwd15m55qqlzd3d29mtw0000gp/T/jest_dy",
// Automatically clear mock calls, instances, contexts and results before every test
// clearMocks: false,
// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// The directory where Jest should output its coverage files
// coverageDirectory: undefined,
// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
// The default configuration for fake timers
// fakeTimers: {
// "enableGlobally": false
// },
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
// globals: {},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "mjs",
// "cjs",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
// Activates notifications for test results
// notify: false,
// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration
preset: "ts-jest",
// Run tests from one or more projects
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
// Automatically reset mock state before every test
// resetMocks: false,
// Reset the module registry before running each individual test
// resetModules: false,
// A path to a custom resolver
// resolver: undefined,
// Automatically restore mock state and implementation before every test
// restoreMocks: false,
// The root directory that Jest should scan for tests and modules within
rootDir: "test",
// A list of paths to directories that Jest should use to search for files in
// roots: [
// "<rootDir>"
// ],
// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
// The test environment that will be used for testing
// testEnvironment: "jest-environment-node",
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
// Adds a location field to test results
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",
// A map from regular expressions to paths to transformers
// transform: undefined,
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "/node_modules/",
// "\\.pnp\\.[^\\/]+$"
// ],
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
// Whether to use watchman for file crawling
// watchman: true,
};
export default config;

3615
gpx/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,8 +2,8 @@
"name": "gpx",
"version": "1.0.0",
"type": "module",
"exports": "./dist/src/index.js",
"types": "dist/src/index.d.ts",
"exports": "./dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/gpxstudio/gpx.studio.git",
@@ -16,15 +16,11 @@
"ts-node": "^10.9.2"
},
"scripts": {
"build": "tsc",
"test": "jest"
"build": "tsc"
},
"devDependencies": {
"@types/geojson": "^7946.0.14",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.6",
"jest": "^29.7.0",
"ts-jest": "^29.1.5",
"typescript": "^5.4.5"
}
}
}

View File

@@ -117,7 +117,15 @@ export class GPXFile extends GPXTreeNode<Track>{
super();
if (gpx) {
this.attributes = gpx.attributes
this.metadata = gpx.metadata;
this.metadata = gpx.metadata ?? {};
this.metadata.author = {
name: 'gpx.studio',
link: {
attributes: {
href: 'https://gpx.studio',
}
}
};
this.wpt = gpx.wpt ? gpx.wpt.map((waypoint) => new Waypoint(waypoint)) : [];
this.trk = gpx.trk ? gpx.trk.map((track) => new Track(track)) : [];
if (gpx.rte && gpx.rte.length > 0) {
@@ -125,6 +133,23 @@ export class GPXFile extends GPXTreeNode<Track>{
}
if (gpx.hasOwnProperty('_data')) {
this._data = gpx._data;
if (!this._data.hasOwnProperty('style')) {
let style = this.getStyle();
let fileStyle = {};
if (style.color.length === 1) {
fileStyle['color'] = style.color[0];
}
if (style.weight.length === 1) {
fileStyle['weight'] = style.weight[0];
}
if (style.opacity.length === 1) {
fileStyle['opacity'] = style.opacity[0];
}
if (Object.keys(fileStyle).length > 0) {
this.setStyle(fileStyle);
}
}
}
} else {
this.attributes = {};
@@ -200,14 +225,32 @@ export class GPXFile extends GPXTreeNode<Track>{
};
}
toGPXFileType(): GPXFileType {
return {
toGPXFileType(exclude: string[] = []): GPXFileType {
let file: GPXFileType = {
attributes: cloneJSON(this.attributes),
metadata: cloneJSON(this.metadata),
wpt: this.wpt,
trk: this.trk.map((track) => track.toTrackType()),
metadata: {},
wpt: this.wpt.map((wpt) => wpt.toWaypointType(exclude)),
trk: this.trk.map((track) => track.toTrackType(exclude)),
rte: [],
};
if (this.metadata) {
if (this.metadata.name) {
file.metadata.name = this.metadata.name;
}
if (this.metadata.desc) {
file.metadata.desc = this.metadata.desc;
}
if (this.metadata.author) {
file.metadata.author = cloneJSON(this.metadata.author);
}
if (this.metadata.link) {
file.metadata.link = cloneJSON(this.metadata.link);
}
if (this.metadata.time && !exclude.includes('time')) {
file.metadata.time = this.metadata.time;
}
}
return file;
}
// Producers
@@ -320,6 +363,15 @@ export class GPXFile extends GPXTreeNode<Track>{
});
}
createArtificialTimestamps(startTime: Date, totalTime: number, trackIndex?: number, segmentIndex?: number) {
let lastPoint = undefined;
this.trk.forEach((track, index) => {
if (trackIndex === undefined || trackIndex === index) {
track.createArtificialTimestamps(startTime, totalTime, lastPoint, segmentIndex);
}
});
}
addElevation(callback: (Coordinates) => number, trackIndices?: number[], segmentIndices?: number[], waypointIndices?: number[]) {
let og = getOriginal(this); // Read as much as possible from the original object because it is faster
this.trk.forEach((track, trackIndex) => {
@@ -348,7 +400,7 @@ export class GPXFile extends GPXTreeNode<Track>{
this._data.style = {};
}
if (style.color) {
this._data.style.color = style.color;
this._data.style.color = style.color.replace('#', '');
}
if (style.opacity) {
this._data.style.opacity = style.opacity;
@@ -412,8 +464,8 @@ export class Track extends GPXTreeNode<TrackSegment> {
src?: string;
link?: Link;
type?: string;
trkseg: TrackSegment[];
extensions?: TrackExtensions;
trkseg: TrackSegment[];
constructor(track?: TrackType & { _data?: any } | Track) {
super();
@@ -446,14 +498,23 @@ export class Track extends GPXTreeNode<TrackSegment> {
src: this.src,
link: cloneJSON(this.link),
type: this.type,
trkseg: this.trkseg.map((seg) => seg.clone()),
extensions: cloneJSON(this.extensions),
trkseg: this.trkseg.map((seg) => seg.clone()),
_data: cloneJSON(this._data),
});
}
getStyle(): LineStyleExtension | undefined {
return this.extensions && this.extensions['gpx_style:line'];
if (this.extensions && this.extensions['gpx_style:line']) {
if (this.extensions["gpx_style:line"].color) {
return {
...this.extensions["gpx_style:line"],
color: `#${this.extensions["gpx_style:line"].color}`
}
}
return this.extensions['gpx_style:line'];
}
return undefined;
}
toGeoJSON(): GeoJSON.Feature[] {
@@ -461,7 +522,7 @@ export class Track extends GPXTreeNode<TrackSegment> {
let geoJSON = child.toGeoJSON();
if (this.extensions && this.extensions['gpx_style:line']) {
if (this.extensions['gpx_style:line'].color) {
geoJSON.properties['color'] = this.extensions['gpx_style:line'].color;
geoJSON.properties['color'] = `#${this.extensions['gpx_style:line'].color}`;
}
if (this.extensions['gpx_style:line'].opacity) {
geoJSON.properties['opacity'] = this.extensions['gpx_style:line'].opacity;
@@ -474,7 +535,7 @@ export class Track extends GPXTreeNode<TrackSegment> {
});
}
toTrackType(): TrackType {
toTrackType(exclude: string[] = []): TrackType {
return {
name: this.name,
cmt: this.cmt,
@@ -482,8 +543,8 @@ export class Track extends GPXTreeNode<TrackSegment> {
src: this.src,
link: this.link,
type: this.type,
trkseg: this.trkseg.map((seg) => seg.toTrackSegmentType()),
extensions: this.extensions,
trkseg: this.trkseg.map((seg) => seg.toTrackSegmentType(exclude)),
};
}
@@ -562,6 +623,17 @@ export class Track extends GPXTreeNode<TrackSegment> {
});
}
createArtificialTimestamps(startTime: Date, totalTime: number, lastPoint: TrackPoint | undefined, segmentIndex?: number) {
this.trkseg.forEach((segment, index) => {
if (segmentIndex === undefined || segmentIndex === index) {
segment.createArtificialTimestamps(startTime, totalTime, lastPoint);
if (segment.trkpt.length > 0) {
lastPoint = segment.trkpt[segment.trkpt.length - 1];
}
}
});
}
setStyle(style: LineStyleExtension, force: boolean = true) {
if (!this.extensions) {
this.extensions = {};
@@ -570,7 +642,7 @@ export class Track extends GPXTreeNode<TrackSegment> {
this.extensions['gpx_style:line'] = {};
}
if (style.color !== undefined && (force || this.extensions['gpx_style:line'].color === undefined)) {
this.extensions['gpx_style:line'].color = style.color;
this.extensions['gpx_style:line'].color = style.color.replace('#', '');
}
if (style.opacity !== undefined && (force || this.extensions['gpx_style:line'].opacity === undefined)) {
this.extensions['gpx_style:line'].opacity = style.opacity;
@@ -663,7 +735,7 @@ export class TrackSegment extends GPXTreeLeaf {
const time = (points[i].time.getTime() - points[i - 1].time.getTime()) / 1000;
speed = dist / (time / 3600);
if (speed >= 0.5) {
if (speed >= 0.5 && speed <= 1500) {
statistics.global.distance.moving += dist;
statistics.global.time.moving += time;
}
@@ -677,6 +749,30 @@ export class TrackSegment extends GPXTreeLeaf {
statistics.global.bounds.southWest.lon = Math.min(statistics.global.bounds.southWest.lon, points[i].attributes.lon);
statistics.global.bounds.northEast.lat = Math.max(statistics.global.bounds.northEast.lat, points[i].attributes.lat);
statistics.global.bounds.northEast.lon = Math.max(statistics.global.bounds.northEast.lon, points[i].attributes.lon);
// extensions
if (points[i].extensions) {
if (points[i].extensions["gpxtpx:TrackPointExtension"] && points[i].extensions["gpxtpx:TrackPointExtension"]["gpxtpx:atemp"]) {
let atemp = points[i].extensions["gpxtpx:TrackPointExtension"]["gpxtpx:atemp"];
statistics.global.atemp.avg = (statistics.global.atemp.count * statistics.global.atemp.avg + atemp) / (statistics.global.atemp.count + 1);
statistics.global.atemp.count++;
}
if (points[i].extensions["gpxtpx:TrackPointExtension"] && points[i].extensions["gpxtpx:TrackPointExtension"]["gpxtpx:hr"]) {
let hr = points[i].extensions["gpxtpx:TrackPointExtension"]["gpxtpx:hr"];
statistics.global.hr.avg = (statistics.global.hr.count * statistics.global.hr.avg + hr) / (statistics.global.hr.count + 1);
statistics.global.hr.count++;
}
if (points[i].extensions["gpxtpx:TrackPointExtension"] && points[i].extensions["gpxtpx:TrackPointExtension"]["gpxtpx:cad"]) {
let cad = points[i].extensions["gpxtpx:TrackPointExtension"]["gpxtpx:cad"];
statistics.global.cad.avg = (statistics.global.cad.count * statistics.global.cad.avg + cad) / (statistics.global.cad.count + 1);
statistics.global.cad.count++;
}
if (points[i].extensions["gpxpx:PowerExtension"] && points[i].extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"]) {
let power = points[i].extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"];
statistics.global.power.avg = (statistics.global.power.count * statistics.global.power.avg + power) / (statistics.global.power.count + 1);
statistics.global.power.count++;
}
}
}
[statistics.local.slope.segment, statistics.local.slope.length] = this._computeSlopeSegments(statistics);
@@ -741,7 +837,7 @@ export class TrackSegment extends GPXTreeLeaf {
let start = simplified[i].point._data.index;
let end = simplified[i + 1].point._data.index;
let dist = statistics.local.distance.total[end] - statistics.local.distance.total[start];
let ele = simplified[i + 1].point.ele - simplified[i].point.ele;
let ele = (simplified[i + 1].point.ele ?? 0) - (simplified[i].point.ele ?? 0);
for (let j = start; j < end + (i + 1 === simplified.length - 1 ? 1 : 0); j++) {
slope.push(0.1 * ele / dist);
@@ -793,9 +889,9 @@ export class TrackSegment extends GPXTreeLeaf {
};
}
toTrackSegmentType(): TrackSegmentType {
toTrackSegmentType(exclude: string[] = []): TrackSegmentType {
return {
trkpt: this.trkpt.map((point) => point.toTrackPointType())
trkpt: this.trkpt.map((point) => point.toTrackPointType(exclude))
};
}
@@ -905,6 +1001,14 @@ export class TrackSegment extends GPXTreeLeaf {
this.trkpt = freeze(trkpt); // Pre-freeze the array, faster as well
}
}
createArtificialTimestamps(startTime: Date, totalTime: number, lastPoint: TrackPoint | undefined) {
let og = getOriginal(this); // Read as much as possible from the original object because it is faster
let slope = og._computeSlope();
let trkpt = withArtificialTimestamps(og.trkpt, totalTime, lastPoint, startTime, slope);
this.trkpt = freeze(trkpt); // Pre-freeze the array, faster as well
}
setHidden(hidden: boolean) {
this._data.hidden = hidden;
}
@@ -945,6 +1049,10 @@ export class TrackPoint {
return this.attributes.lon;
}
getTemperature(): number {
return this.extensions && this.extensions['gpxtpx:TrackPointExtension'] && this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:atemp'] ? this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:atemp'] : undefined;
}
getHeartRate(): number {
return this.extensions && this.extensions['gpxtpx:TrackPointExtension'] && this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:hr'] ? this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:hr'] : undefined;
}
@@ -953,10 +1061,6 @@ export class TrackPoint {
return this.extensions && this.extensions['gpxtpx:TrackPointExtension'] && this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:cad'] ? this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:cad'] : undefined;
}
getTemperature(): number {
return this.extensions && this.extensions['gpxtpx:TrackPointExtension'] && this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:atemp'] ? this.extensions['gpxtpx:TrackPointExtension']['gpxtpx:atemp'] : undefined;
}
getPower(): number {
return this.extensions && this.extensions["gpxpx:PowerExtension"] && this.extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"] ? this.extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"] : undefined;
}
@@ -978,13 +1082,38 @@ export class TrackPoint {
this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:Extensions"]["surface"] = surface;
}
toTrackPointType(): TrackPointType {
return {
toTrackPointType(exclude: string[] = []): TrackPointType {
let trkpt: TrackPointType = {
attributes: this.attributes,
ele: this.ele,
time: this.time,
extensions: this.extensions,
};
if (!exclude.includes('time')) {
trkpt = { ...trkpt, time: this.time };
}
if (this.extensions) {
trkpt = {
...trkpt, extensions: {
"gpxtpx:TrackPointExtension": {},
"gpxpx:PowerExtension": {},
}
};
if (this.extensions["gpxtpx:TrackPointExtension"] && this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:atemp"] && !exclude.includes('atemp')) {
trkpt.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:atemp"] = this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:atemp"];
}
if (this.extensions["gpxtpx:TrackPointExtension"] && this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:hr"] && !exclude.includes('hr')) {
trkpt.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:hr"] = this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:hr"];
}
if (this.extensions["gpxtpx:TrackPointExtension"] && this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:cad"] && !exclude.includes('cad')) {
trkpt.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:cad"] = this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:cad"];
}
if (this.extensions["gpxpx:PowerExtension"] && this.extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"] && !exclude.includes('power')) {
trkpt.extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"] = this.extensions["gpxpx:PowerExtension"]["gpxpx:PowerInWatts"];
}
if (this.extensions["gpxtpx:TrackPointExtension"] && this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:Extensions"] && this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:Extensions"].surface && !exclude.includes('surface')) {
trkpt.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:Extensions"] = { surface: this.extensions["gpxtpx:TrackPointExtension"]["gpxtpx:Extensions"].surface };
}
}
return trkpt;
}
clone(): TrackPoint {
@@ -1043,6 +1172,34 @@ export class Waypoint {
return this.attributes.lon;
}
toWaypointType(exclude: string[] = []): WaypointType {
if (!exclude.includes('time')) {
return {
attributes: this.attributes,
ele: this.ele,
time: this.time,
name: this.name,
cmt: this.cmt,
desc: this.desc,
link: this.link,
sym: this.sym,
type: this.type,
}
} else {
return {
attributes: this.attributes,
ele: this.ele,
name: this.name,
cmt: this.cmt,
desc: this.desc,
link: this.link,
sym: this.sym,
type: this.type,
};
}
}
clone(): Waypoint {
return new Waypoint({
attributes: cloneJSON(this.attributes),
@@ -1087,6 +1244,22 @@ export class GPXStatistics {
southWest: Coordinates,
northEast: Coordinates,
},
atemp: {
avg: number,
count: number,
},
hr: {
avg: number,
count: number,
},
cad: {
avg: number,
count: number,
},
power: {
avg: number,
count: number,
}
};
local: {
points: TrackPoint[],
@@ -1141,6 +1314,22 @@ export class GPXStatistics {
lon: -180,
},
},
atemp: {
avg: 0,
count: 0,
},
hr: {
avg: 0,
count: 0,
},
cad: {
avg: 0,
count: 0,
},
power: {
avg: 0,
count: 0,
}
};
this.local = {
points: [],
@@ -1201,9 +1390,29 @@ export class GPXStatistics {
this.global.bounds.southWest.lon = Math.min(this.global.bounds.southWest.lon, other.global.bounds.southWest.lon);
this.global.bounds.northEast.lat = Math.max(this.global.bounds.northEast.lat, other.global.bounds.northEast.lat);
this.global.bounds.northEast.lon = Math.max(this.global.bounds.northEast.lon, other.global.bounds.northEast.lon);
this.global.atemp.avg = (this.global.atemp.count * this.global.atemp.avg + other.global.atemp.count * other.global.atemp.avg) / Math.max(1, this.global.atemp.count + other.global.atemp.count);
this.global.atemp.count += other.global.atemp.count;
this.global.hr.avg = (this.global.hr.count * this.global.hr.avg + other.global.hr.count * other.global.hr.avg) / Math.max(1, this.global.hr.count + other.global.hr.count);
this.global.hr.count += other.global.hr.count;
this.global.cad.avg = (this.global.cad.count * this.global.cad.avg + other.global.cad.count * other.global.cad.avg) / Math.max(1, this.global.cad.count + other.global.cad.count);
this.global.cad.count += other.global.cad.count;
this.global.power.avg = (this.global.power.count * this.global.power.avg + other.global.power.count * other.global.power.avg) / Math.max(1, this.global.power.count + other.global.power.count);
this.global.power.count += other.global.power.count;
}
slice(start: number, end: number): GPXStatistics {
if (start < 0) {
start = 0;
} else if (start >= this.local.points.length) {
return new GPXStatistics();
}
if (end < start) {
return new GPXStatistics();
} else if (end >= this.local.points.length) {
end = this.local.points.length - 1;
}
let statistics = new GPXStatistics();
statistics.local.points = this.local.points.slice(start, end + 1);
@@ -1228,12 +1437,23 @@ export class GPXStatistics {
statistics.global.bounds.northEast.lat = this.global.bounds.northEast.lat;
statistics.global.bounds.northEast.lon = this.global.bounds.northEast.lon;
statistics.global.atemp = this.global.atemp;
statistics.global.hr = this.global.hr;
statistics.global.cad = this.global.cad;
statistics.global.power = this.global.power;
return statistics;
}
}
const earthRadius = 6371008.8;
export function distance(coord1: Coordinates, coord2: Coordinates): number {
export function distance(coord1: TrackPoint | Coordinates, coord2: TrackPoint | Coordinates): number {
if (coord1 instanceof TrackPoint) {
coord1 = coord1.getCoordinates();
}
if (coord2 instanceof TrackPoint) {
coord2 = coord2.getCoordinates();
}
const rad = Math.PI / 180;
const lat1 = coord1.lat * rad;
const lat2 = coord2.lat * rad;
@@ -1285,9 +1505,39 @@ function withTimestamps(points: TrackPoint[], speed: number, lastPoint: TrackPoi
function withShiftedAndCompressedTimestamps(points: TrackPoint[], speed: number, ratio: number, lastPoint: TrackPoint): TrackPoint[] {
let start = getTimestamp(lastPoint, points[0], speed);
let last = points[0];
return points.map((point) => {
let pt = point.clone();
pt.time = new Date(start.getTime() + ratio * (point.time.getTime() - points[0].time.getTime()));
if (point.time === undefined) {
pt.time = getTimestamp(last, point, speed);
} else {
pt.time = new Date(start.getTime() + ratio * (point.time.getTime() - points[0].time.getTime()));
}
last = pt;
return pt;
});
}
function withArtificialTimestamps(points: TrackPoint[], totalTime: number, lastPoint: TrackPoint | undefined, startTime: Date, slope: number[]): TrackPoint[] {
let weight = [];
let totalWeight = 0;
for (let i = 0; i < points.length - 1; i++) {
let dist = distance(points[i].getCoordinates(), points[i + 1].getCoordinates());
let w = dist * (0.5 + 1 / (1 + Math.exp(- 0.2 * slope[i])));
weight.push(w);
totalWeight += w;
}
let last = lastPoint;
return points.map((point, i) => {
let pt = point.clone();
if (i === 0) {
pt.time = lastPoint?.time ?? startTime;
} else {
pt.time = new Date(last.time.getTime() + totalTime * 1000 * weight[i - 1] / totalWeight);
}
last = pt;
return pt;
});
}
@@ -1318,8 +1568,8 @@ function convertRouteToTrack(route: RouteType): Track {
src: route.src,
link: route.link,
type: route.type,
trkseg: [],
extensions: route.extensions,
trkseg: [],
});
if (route.rtept) {

View File

@@ -23,6 +23,7 @@ export function parseGPX(gpxData: string): GPXFile {
}
return tagName;
},
parseTagValue: false,
tagValueProcessor(tagName, tagValue, jPath, hasAttributes, isLeafNode) {
if (isLeafNode) {
if (tagName === 'ele') {
@@ -33,7 +34,7 @@ export function parseGPX(gpxData: string): GPXFile {
return new Date(tagValue);
}
if (tagName === 'gpxtpx:hr' || tagName === 'gpxtpx:cad' || tagName === 'gpxtpx:atemp' || tagName === 'gpxpx:PowerInWatts' || tagName === 'opacity' || tagName === 'weight') {
if (tagName === 'gpxtpx:atemp' || tagName === 'gpxtpx:hr' || tagName === 'gpxtpx:cad' || tagName === 'gpxpx:PowerInWatts' || tagName === 'opacity' || tagName === 'weight') {
return parseFloat(tagValue);
}
@@ -60,9 +61,8 @@ export function parseGPX(gpxData: string): GPXFile {
return new GPXFile(parsed);
}
export function buildGPX(file: GPXFile): string {
const gpx = file.toGPXFileType();
export function buildGPX(file: GPXFile, exclude: string[]): string {
const gpx = file.toGPXFileType(exclude);
const builder = new XMLBuilder({
format: true,
@@ -87,14 +87,6 @@ export function buildGPX(file: GPXFile): string {
gpx.attributes['xmlns:gpxx'] = 'http://www.garmin.com/xmlschemas/GpxExtensions/v3';
gpx.attributes['xmlns:gpxpx'] = 'http://www.garmin.com/xmlschemas/PowerExtension/v1';
gpx.attributes['xmlns:gpx_style'] = 'http://www.topografix.com/GPX/gpx_style/0/2';
gpx.metadata.author = {
name: 'gpx.studio',
link: {
attributes: {
href: 'https://gpx.studio',
}
}
};
if (gpx.trk.length === 1 && (gpx.trk[0].name === undefined || gpx.trk[0].name === '')) {
gpx.trk[0].name = gpx.metadata.name;

View File

@@ -5,7 +5,7 @@ export type SimplifiedTrackPoint = { point: TrackPoint, distance?: number };
const earthRadius = 6371008.8;
export function ramerDouglasPeucker(points: TrackPoint[], epsilon: number = 50, measure: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => number = computeCrossarc): SimplifiedTrackPoint[] {
export function ramerDouglasPeucker(points: TrackPoint[], epsilon: number = 50, measure: (a: TrackPoint, b: TrackPoint, c: TrackPoint) => number = crossarcDistance): SimplifiedTrackPoint[] {
if (points.length == 0) {
return [];
} else if (points.length == 1) {
@@ -45,8 +45,8 @@ function ramerDouglasPeuckerRecursive(points: TrackPoint[], epsilon: number, mea
}
}
function computeCrossarc(point1: TrackPoint, point2: TrackPoint, point3: TrackPoint): number {
return crossarc(point1.getCoordinates(), point2.getCoordinates(), point3.getCoordinates());
export function crossarcDistance(point1: TrackPoint, point2: TrackPoint, point3: TrackPoint | Coordinates): number {
return crossarc(point1.getCoordinates(), point2.getCoordinates(), point3 instanceof TrackPoint ? point3.getCoordinates() : point3);
}
function crossarc(coord1: Coordinates, coord2: Coordinates, coord3: Coordinates): number {
@@ -101,4 +101,55 @@ function bearing(latA: number, lonA: number, latB: number, lonB: number): number
// Finds the bearing from one lat / lon point to another.
return Math.atan2(Math.sin(lonB - lonA) * Math.cos(latB),
Math.cos(latA) * Math.sin(latB) - Math.sin(latA) * Math.cos(latB) * Math.cos(lonB - lonA));
}
export function projectedPoint(point1: TrackPoint, point2: TrackPoint, point3: TrackPoint | Coordinates): Coordinates {
return projected(point1.getCoordinates(), point2.getCoordinates(), point3 instanceof TrackPoint ? point3.getCoordinates() : point3);
}
function projected(coord1: Coordinates, coord2: Coordinates, coord3: Coordinates): Coordinates {
// Calculates the point on the line defined by p1 and p2
// that is closest to the third point, p3.
// Input lat1,lon1,lat2,lon2,lat3,lon3 in degrees.
const rad = Math.PI / 180;
const lat1 = coord1.lat * rad;
const lat2 = coord2.lat * rad;
const lat3 = coord3.lat * rad;
const lon1 = coord1.lon * rad;
const lon2 = coord2.lon * rad;
const lon3 = coord3.lon * rad;
// Prerequisites for the formulas
const bear12 = bearing(lat1, lon1, lat2, lon2);
const bear13 = bearing(lat1, lon1, lat3, lon3);
let dis13 = distance(lat1, lon1, lat3, lon3);
let diff = Math.abs(bear13 - bear12);
if (diff > Math.PI) {
diff = 2 * Math.PI - diff;
}
// Is relative bearing obtuse?
if (diff > (Math.PI / 2)) {
return coord1;
}
// Find the cross-track distance.
let dxt = Math.asin(Math.sin(dis13 / earthRadius) * Math.sin(bear13 - bear12)) * earthRadius;
// Is p4 beyond the arc?
let dis12 = distance(lat1, lon1, lat2, lon2);
let dis14 = Math.acos(Math.cos(dis13 / earthRadius) / Math.cos(dxt / earthRadius)) * earthRadius;
if (dis14 > dis12) {
return coord2;
} else {
// Determine the closest point (p4) on the great circle
const f = dis14 / earthRadius;
const lat4 = Math.asin(Math.sin(lat1) * Math.cos(f) + Math.cos(lat1) * Math.sin(f) * Math.cos(bear12));
const lon4 = lon1 + Math.atan2(Math.sin(bear12) * Math.sin(f) * Math.cos(lat1), Math.cos(f) - Math.sin(lat1) * Math.sin(lat4));
return { lat: lat4 / rad, lon: lon4 / rad };
}
}

View File

@@ -58,8 +58,8 @@ export type TrackType = {
src?: string;
link?: Link;
type?: string;
trkseg: TrackSegmentType[];
extensions?: TrackExtensions;
trkseg: TrackSegmentType[];
};
export type TrackExtensions = {
@@ -89,9 +89,9 @@ export type TrackPointExtensions = {
};
export type TrackPointExtension = {
'gpxtpx:atemp'?: number;
'gpxtpx:hr'?: number;
'gpxtpx:cad'?: number;
'gpxtpx:atemp'?: number;
'gpxtpx:Extensions'?: {
surface?: string;
};

View File

@@ -1,59 +0,0 @@
import * as fs from 'fs';
import { parseGPX, buildGPX } from '../src/io';
describe('GPX operations', () => {
it('Clone', () => {
const path = "test-data/with_tracks_and_segments.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const cloned = original.clone();
expect(cloned).not.toBe(original);
const originalString = buildGPX(original);
const clonedString = buildGPX(cloned);
expect(clonedString).toBe(originalString);
});
it('Reverse', () => {
const path = "test-data/with_tracks_and_segments.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
let reversed = original.clone();
reversed.reverse();
expect(original.getStartTimestamp().getTime()).toBe(reversed.getStartTimestamp().getTime());
expect(original.getEndTimestamp().getTime()).toBe(reversed.getEndTimestamp().getTime());
expect(reversed.trk.length).toBe(original.trk.length);
for (let i = 0; i < original.trk.length; i++) {
const originalTrack = original.trk[i];
const reversedTrack = reversed.trk[original.trk.length - i - 1];
expect(reversedTrack.trkseg.length).toBe(originalTrack.trkseg.length);
for (let j = 0; j < originalTrack.trkseg.length; j++) {
const originalSegment = originalTrack.trkseg[j];
const reversedSegment = reversedTrack.trkseg[originalTrack.trkseg.length - j - 1];
expect(reversedSegment.trkpt.length).toBe(originalSegment.trkpt.length);
for (let k = 0; k < originalSegment.trkpt.length; k++) {
const originalPoint = originalSegment.trkpt[k];
const reversedPoint = reversedSegment.trkpt[originalSegment.trkpt.length - k - 1];
expect(reversedPoint.attributes.lat).toBe(originalPoint.attributes.lat);
expect(reversedPoint.attributes.lon).toBe(originalPoint.attributes.lon);
expect(reversedPoint.ele).toBe(originalPoint.ele);
expect(reversed.getEndTimestamp().getTime() - reversedPoint.time.getTime()).toBe(originalPoint.time.getTime() - original.getStartTimestamp().getTime());
}
}
}
});
});

View File

@@ -1,364 +0,0 @@
import * as fs from 'fs';
import { parseGPX, buildGPX } from '../src/io';
describe("Parsing", () => {
it("Simple", () => {
const path = "test-data/simple.gpx";
const data = fs.readFileSync(path, 'utf8');
const result = parseGPX(data);
expect(result.attributes.creator).toBe("https://gpx.studio");
expect(result.metadata.name).toBe("simple");
expect(result.metadata.author.name).toBe("gpx.studio");
expect(result.metadata.author.link.attributes.href).toBe("https://gpx.studio");
expect(result.trk.length).toBe(1);
const track = result.trk[0];
expect(track.name).toBe("simple");
expect(track.type).toBe("Cycling");
expect(track.trkseg.length).toBe(1);
const segment = track.trkseg[0];
expect(segment.trkpt.length).toBe(80);
for (let i = 0; i < segment.trkpt.length; i++) {
const point = segment.trkpt[i];
expect(point).toHaveProperty('attributes');
expect(point.attributes).toHaveProperty('lat');
expect(point.attributes).toHaveProperty('lon');
expect(point).toHaveProperty('ele');
}
expect(segment.trkpt[0].attributes.lat).toBe(50.790867);
expect(segment.trkpt[0].attributes.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.trk.length).toBe(2);
const track_1 = result.trk[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.trk[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.trk.length).toBe(1);
const track = result.trk[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.wpt.length).toBe(1);
const waypoint = result.wpt[0];
expect(waypoint.attributes.lat).toBe(50.7836710064975);
expect(waypoint.attributes.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.trk[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.trk[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('gpxtpx:TrackPointExtension');
expect(segment.trkpt[i].extensions['gpxtpx:TrackPointExtension']).toHaveProperty('gpxtpx:hr');
}
expect(segment.trkpt[0].extensions['gpxtpx:TrackPointExtension']['gpxtpx:hr']).toBe(150);
expect(segment.trkpt[segment.trkpt.length - 1].extensions['gpxtpx:TrackPointExtension']['gpxtpx: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.trk[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('gpxtpx:TrackPointExtension');
expect(segment.trkpt[i].extensions['gpxtpx:TrackPointExtension']).toHaveProperty('gpxtpx:cad');
}
expect(segment.trkpt[0].extensions['gpxtpx:TrackPointExtension']['gpxtpx:cad']).toBe(80);
expect(segment.trkpt[segment.trkpt.length - 1].extensions['gpxtpx:TrackPointExtension']['gpxtpx: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.trk[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('gpxtpx:TrackPointExtension');
expect(segment.trkpt[i].extensions['gpxtpx:TrackPointExtension']).toHaveProperty('gpxtpx:atemp');
}
expect(segment.trkpt[0].extensions['gpxtpx:TrackPointExtension']['gpxtpx:atemp']).toBe(21);
expect(segment.trkpt[segment.trkpt.length - 1].extensions['gpxtpx:TrackPointExtension']['gpxtpx: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.trk[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('gpxpx:PowerExtension');
expect(segment.trkpt[i].extensions['gpxpx:PowerExtension']).toHaveProperty('gpxpx:PowerInWatts');
}
expect(segment.trkpt[0].extensions['gpxpx:PowerExtension']['gpxpx:PowerInWatts']).toBe(200);
expect(segment.trkpt[segment.trkpt.length - 1].extensions['gpxpx:PowerExtension']['gpxpx:PowerInWatts']).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.trk[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('gpxpx:PowerExtension');
expect(segment.trkpt[i].extensions['gpxpx:PowerExtension']).toHaveProperty('gpxpx:PowerInWatts');
}
expect(segment.trkpt[0].extensions['gpxpx:PowerExtension']['gpxpx:PowerInWatts']).toBe(200);
expect(segment.trkpt[segment.trkpt.length - 1].extensions['gpxpx:PowerExtension']['gpxpx:PowerInWatts']).toBe(210);
});
it("Surface", () => {
const path = "test-data/with_surface.gpx";
const data = fs.readFileSync(path, 'utf8');
const result = parseGPX(data);
const track = result.trk[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('gpxtpx:TrackPointExtension');
expect(segment.trkpt[i].extensions['gpxtpx:TrackPointExtension']).toHaveProperty('gpxtpx:Extensions');
expect(segment.trkpt[i].extensions['gpxtpx:TrackPointExtension']['gpxtpx:Extensions']).toHaveProperty('surface');
}
expect(segment.trkpt[0].extensions['gpxtpx:TrackPointExtension']['gpxtpx:Extensions'].surface).toBe("asphalt");
expect(segment.trkpt[segment.trkpt.length - 1].extensions['gpxtpx:TrackPointExtension']['gpxtpx: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.trk[0];
expect(track).toHaveProperty('extensions');
expect(track.extensions).toHaveProperty('gpx_style:line');
expect(track.extensions['gpx_style:line']).toHaveProperty('color');
expect(track.extensions['gpx_style:line']).toHaveProperty('opacity');
expect(track.extensions['gpx_style:line']).toHaveProperty('weight');
expect(track.extensions['gpx_style:line'].color).toBe("#2d3ee9");
expect(track.extensions['gpx_style:line'].opacity).toBe(0.5);
expect(track.extensions['gpx_style:line'].weight).toBe(6);
});
});
describe("Building", () => {
it("Simple", () => {
const path = "test-data/simple.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Multiple tracks", () => {
const path = "test-data/with_tracks.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Multiple segments", () => {
const path = "test-data/with_segments.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Waypoint", () => {
const path = "test-data/with_waypoint.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Time", () => {
const path = "test-data/with_time.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Heart rate", () => {
const path = "test-data/with_hr.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Cadence", () => {
const path = "test-data/with_cad.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Temperature", () => {
const path = "test-data/with_temp.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Power 1", () => {
const path = "test-data/with_power_1.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Power 2", () => {
const path = "test-data/with_power_2.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Surface", () => {
const path = "test-data/with_surface.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
it("Track style", () => {
const path = "test-data/with_style.gpx";
const data = fs.readFileSync(path, 'utf8');
const original = parseGPX(data);
const built = buildGPX(original);
const rebuilt = parseGPX(built);
expect(rebuilt).toEqual(original);
});
});

View File

@@ -7,7 +7,6 @@
"moduleResolution": "node",
},
"include": [
"src",
"test"
"src"
],
}