Skip to content

Commit

Permalink
add bbox filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhorner committed Sep 15, 2024
1 parent 05a8394 commit 269d846
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/tracks/track-import.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class TrackImportProcessor extends WorkerHost {
try {
const gpxPath = await this.convertToGpx(job.data.filePath)
gpxData = await this.processGpxData(gpxPath)
} catch {
throw new UnrecoverableError("Failed to process GPX data")
} catch (e: any) {
throw new UnrecoverableError(`Failed to process GPX data: ${e.message}`)
}

await job.updateProgress({
Expand Down Expand Up @@ -98,11 +98,14 @@ export class TrackImportProcessor extends WorkerHost {
const smoothedSegment = smoothTrackSegment(segment)

const simplifiedPoints = ramerDouglasPeucker(smoothedSegment.trkpt, 1)

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

if (points.length < 5) {
throw new Error("Cleaned GPX yielded insignificant data")
}

return {
type: "Feature",
properties: {},
Expand Down
3 changes: 2 additions & 1 deletion src/tracks/tracks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ export class TracksController {
async list(
@Query("start") start?: string,
@Query("end") end?: string,
@Query("bbox") bbox?: string,
@Query("format") format?: string,
) {
const tracks = await this.tracksService.list({ start, end })
const tracks = await this.tracksService.list({ start, end, bbox })

if (format === "geojson") {
return this.tracksService.toGeoJSON(tracks)
Expand Down
12 changes: 12 additions & 0 deletions src/tracks/tracks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as path from "node:path"
export interface TrackFilters {
start?: string
end?: string
bbox?: string
}

@Injectable()
Expand Down Expand Up @@ -45,6 +46,17 @@ export class TracksService {
})
}

if (filters.bbox) {
const [minLon, minLat, maxLon, maxLat] = filters.bbox
.split(",")
.map(Number)

query.andWhere(
"ST_Intersects(track.geometry, ST_MakeEnvelope(:minLon, :minLat, :maxLon, :maxLat, 4326))",
{ minLon, minLat, maxLon, maxLat },
)
}

return query.getMany()
}

Expand Down

0 comments on commit 269d846

Please sign in to comment.