Skip to content

Commit

Permalink
simplify gpx tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhorner committed Sep 14, 2024
1 parent f4b7251 commit 05a8394
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib/gpx-smooth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TrackSegment, TrackPoint } from "../vendor/gpx" // or the appropriate m
* @returns A new TrackSegment with smoothed TrackPoints.
*/
export function smoothTrackSegment(trackSegment: TrackSegment): TrackSegment {
const maxSpeedThreshold = 18 // meters per second (adjust based on expected activity speed)
const maxSpeedThreshold = 17.8 // meters per second (adjust based on expected activity speed)

const originalTrackPoints = trackSegment.trkpt
const cleanedTrackPoints: TrackPoint[] = []
Expand Down
21 changes: 19 additions & 2 deletions src/tracks/track-import.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { TracksService } from "./tracks.service"
import { Job, UnrecoverableError } from "bullmq"
import { Feature, LineString } from "typeorm"
import { execFile } from "child_process"
import { parseGPX } from "src/vendor/gpx"
import { parseGPX, ramerDouglasPeucker } from "src/vendor/gpx"
import { createReadStream } from "fs"
import * as crypto from "crypto"
import * as fs from "fs/promises"
Expand Down Expand Up @@ -93,10 +93,27 @@ export class TrackImportProcessor extends WorkerHost {
const gpxFile = await fs.readFile(gpxPath, "utf-8")

const gpxData = parseGPX(gpxFile)

const segment = gpxData.getSegment(0, 0)
const smoothedSegment = smoothTrackSegment(segment)

return smoothedSegment.toGeoJSON()
const simplifiedPoints = ramerDouglasPeucker(smoothedSegment.trkpt, 1)

const points = simplifiedPoints.filter(
(point) => point.distance === undefined || point.distance >= 1,
)

return {
type: "Feature",
properties: {},
geometry: {
type: "LineString",
coordinates: points.map((point) => [
point.point.getLongitude(),
point.point.getLatitude(),
]),
},
}
}

private async getCaptureDate(filePath: string): Promise<Date> {
Expand Down

0 comments on commit 05a8394

Please sign in to comment.