-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import InputLabel from '@mui/material/InputLabel'; | ||
import { OverridableStringUnion } from '@mui/types'; | ||
import Button, { ButtonPropsColorOverrides } from '@mui/material/Button'; | ||
import { ChangeEvent, ReactNode, useRef } from 'react'; | ||
|
||
export default function ImportFileButton({ | ||
children, | ||
onFile, | ||
color, | ||
}: { | ||
children?: ReactNode | ReactNode[]; | ||
onFile: (file: File) => void; | ||
color?: OverridableStringUnion< | ||
'inherit' | 'primary' | 'secondary' | 'success' | 'error' | 'info' | 'warning', | ||
ButtonPropsColorOverrides | ||
>; | ||
}) { | ||
const uploadInputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
const onChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
onFile(e.target.files[0]); | ||
}; | ||
|
||
return ( | ||
<InputLabel htmlFor="import-file" hidden> | ||
<input ref={uploadInputRef} id="import-file" name="import-file" type="file" onChange={onChange} hidden /> | ||
<Button color={color || 'secondary'} variant="contained" component="span"> | ||
{children} | ||
</Button> | ||
</InputLabel> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { Polyline } from 'react-leaflet'; | ||
|
||
const mintrackpointdelta = 0.0001; | ||
|
||
export default function MapTrack({ map, track }) { | ||
const { trackpoints } = track?.tracks[0]?.segments[0] || []; | ||
const [polyline, setPolyline] = useState([]); | ||
|
||
useEffect(() => { | ||
const pointArray = []; | ||
|
||
if (trackpoints.length > 0) { | ||
let lastlon = parseFloat(trackpoints[0].lon); | ||
let lastlat = parseFloat(trackpoints[0].lat); | ||
|
||
pointArray.push([lastlon, lastlat]); | ||
|
||
for (let i = 1; i < trackpoints.length; i++) { | ||
const lon = parseFloat(trackpoints[i].lon); | ||
const lat = parseFloat(trackpoints[i].lat); | ||
|
||
const latdiff = lat - lastlat; | ||
const londiff = lon - lastlon; | ||
if (Math.sqrt(latdiff * latdiff + londiff * londiff) > mintrackpointdelta) { | ||
lastlon = lon; | ||
lastlat = lat; | ||
pointArray.push([lat, lon]); | ||
} | ||
} | ||
} | ||
|
||
setPolyline(pointArray); | ||
}, [trackpoints]); | ||
|
||
return <Polyline pathOptions={{ color: 'black' }} positions={polyline} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
export async function parseGpxFile2Document(file: File): Promise<Document> { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = (e) => { | ||
const parser = new DOMParser(); | ||
const xmlDoc = parser.parseFromString(e.target.result as string, 'text/xml'); | ||
const errorNode = xmlDoc.querySelector('parsererror'); | ||
if (errorNode) { | ||
reject(new Error('Failed to parse the GPX file')); | ||
} else { | ||
resolve(xmlDoc); | ||
} | ||
}; | ||
reader.readAsText(file); | ||
}); | ||
} | ||
|
||
function* elIter(el: HTMLCollectionOf<Element>, callback: (el: Element) => any) { | ||
for (let i = 0; i < el.length; i++) { | ||
yield callback(el[i]); | ||
} | ||
} | ||
|
||
function getElValue(el: HTMLCollectionOf<Element>) { | ||
return el[0].childNodes[0].nodeValue; | ||
} | ||
|
||
function parseTrackpoints(trackpoints: HTMLCollectionOf<Element>) { | ||
return [ | ||
...elIter(trackpoints, (trackpoint) => ({ | ||
lon: parseFloat(trackpoint.getAttribute('lon')), | ||
lat: parseFloat(trackpoint.getAttribute('lat')), | ||
ele: getElValue(trackpoint.getElementsByTagName('ele')), | ||
})), | ||
]; | ||
} | ||
|
||
function parseSegments(segments: HTMLCollectionOf<Element>) { | ||
return [ | ||
...elIter(segments, (segment) => ({ | ||
trackpoints: parseTrackpoints(segment.getElementsByTagName('trkpt')), | ||
})), | ||
]; | ||
} | ||
|
||
function parseTracks(tracks: HTMLCollectionOf<Element>) { | ||
return [ | ||
...elIter(tracks, (track) => ({ | ||
name: getElValue(track.getElementsByTagName('name')), | ||
segments: parseSegments(track.getElementsByTagName('trkseg')), | ||
})), | ||
]; | ||
} | ||
|
||
export function gpxDocument2obj(doc: Document) { | ||
return { tracks: parseTracks(doc.documentElement.getElementsByTagName('trk')) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"target": "es2022", | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
|
bb5c394
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
bfree – ./
bfree-olliv.vercel.app
bfree.vercel.app
bfree-git-master-olliv.vercel.app