Skip to content

Commit

Permalink
feat(trail): Compute cumulative positiv elevation and ITRA distance
Browse files Browse the repository at this point in the history
  • Loading branch information
annelhote committed Apr 1, 2024
1 parent c273cb8 commit ee5476e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/components/overview/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Button, Grid, Stack, TextField } from '@mui/material';
import { useEffect, useState } from 'react';

import { downloadGpx, getITRADistance } from '../../utils';
import { downloadGpx } from '../../utils';

const Overview = ({ gpx, meta, setMeta }) => {
const [distance, setDistance] = useState(0);
Expand All @@ -12,15 +12,12 @@ const Overview = ({ gpx, meta, setMeta }) => {
const [itraDistance, setItraDistance] = useState(0);

useEffect(() => {
setDistance(Math.floor(gpx.tracks[0].distance.total / 1000));
setDistance(Math.round(gpx.tracks[0].distance.total / 1000));
setItraDistance(Math.round(gpx.tracks[0].distance.totalItra / 1000))
setDuration(Math.ceil(distance / meta.kmPerDay));
setElevation(gpx.calcElevation(gpx.tracks[0].points));
}, [distance, gpx, meta]);

useEffect(() => {
setItraDistance(getITRADistance({ distance, elevation: elevation.pos }));
}, [distance, elevation])

return (
<Grid className='overview' container item>
<Grid item>
Expand Down
23 changes: 23 additions & 0 deletions src/pages/trail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ const Trail = () => {
.then((xml) => {
const newGpx = new gpxParser();
newGpx.parse(xml);
// Compute cumulative positiv elevation and ITRA distance at each point of the Route/Track
const cumulItra = [];
const cumulElevation = [];
let itraValue = 0;
let elevationValue = 0;
const points = newGpx.tracks[0].points;
for (let i = 0; i < points.length - 1; i++) {
const pointFrom = points[i];
const pointTo = points[i + 1];
let distance = newGpx.calcDistanceBetween(pointFrom, pointTo);
const elevation = pointTo.ele - pointFrom.ele;
if (elevation > 0) {
distance += elevation * 10;
elevationValue += elevation;
}
itraValue += distance;
cumulItra.push(itraValue);
cumulElevation.push(elevationValue);
};
newGpx.tracks[0].distance.cumulItra = cumulItra;
newGpx.tracks[0].distance.totalItra = itraValue;
newGpx.tracks[0].distance.cumulElevation = cumulElevation;
newGpx.tracks[0].distance.totalElevation = elevationValue;
setGpx(newGpx);
})
.catch((e) => console.error(e));
Expand Down

0 comments on commit ee5476e

Please sign in to comment.