diff --git a/src/lib/server/simplifyGps.ts b/src/lib/server/simplifyGps.ts index f407582..1d60f87 100644 --- a/src/lib/server/simplifyGps.ts +++ b/src/lib/server/simplifyGps.ts @@ -3,46 +3,60 @@ import type { Decimal } from '@prisma/client/runtime/library'; import { getDistanceFromLine } from 'geolib'; export async function simplifyGps(trip: string, amount: number) { - let inputData = await prisma.datapoint.findMany({ - where: { - tripId: trip, - optimized: 0 - }, - take: amount - }); + let totalAmount = 0; + while(totalAmount < amount){ + let take = amount; - let lastPoint: Datapoint = inputData[0]; - - for (let i = 1; i < inputData.length - 1; i++) { - let crosstrackError = getDistanceFromLine( - { lat: Number(inputData[i].lat), lng: Number(inputData[i].long) }, - { lat: Number(lastPoint.lat), lng: Number(lastPoint.long) }, - { lat: Number(inputData[i + 1].lat), lng: Number(inputData[i + 1].long) } - ); + if(amount>500){ + take = 500; + } - let turnRate = 0; - if ((inputData[i].heading != null, inputData[i - 1].heading != null)) { - turnRate = Number(inputData[i - 1].heading) - Number(inputData[i].heading); + let inputData = await prisma.datapoint.findMany({ + where: { + tripId: trip, + optimized: 0 + }, + take: take + }); + if(inputData.length <= 2){ + return; } - if (crosstrackError < 10 && Math.abs(turnRate) < 30) { - // Delete Datapoint - await prisma.datapoint.update({ - where: { id: inputData[i].id }, - data: { - optimized: 1 - } - }); - } else { - // Change Datapoint to optimized - lastPoint = inputData[i]; - await prisma.datapoint.update({ - where: { id: inputData[i].id }, - data: { - optimized: 2 - } - }); + let lastPoint: Datapoint = inputData[0]; + + for (let i = 1; i < inputData.length - 1; i++) { + let crosstrackError = getDistanceFromLine( + { lat: Number(inputData[i].lat), lng: Number(inputData[i].long) }, + { lat: Number(lastPoint.lat), lng: Number(lastPoint.long) }, + { lat: Number(inputData[i + 1].lat), lng: Number(inputData[i + 1].long) } + ); + + let turnRate = 0; + if ((inputData[i].heading != null, inputData[i - 1].heading != null)) { + turnRate = Number(inputData[i - 1].heading) - Number(inputData[i].heading); + } + + if (crosstrackError < 10 && Math.abs(turnRate) < 30) { + // Delete Datapoint + await prisma.datapoint.update({ + where: { id: inputData[i].id }, + data: { + optimized: 1 + } + }); + } else { + // Change Datapoint to optimized + lastPoint = inputData[i]; + await prisma.datapoint.update({ + where: { id: inputData[i].id }, + data: { + optimized: 2 + } + }); + } } + totalAmount += take; + console.log("Simplified " + totalAmount + " of " + amount + " " + totalAmount*100/amount + "%"); } } diff --git a/src/routes/api/Datapoints/+server.ts b/src/routes/api/Datapoints/+server.ts index 4521e16..f3a6a78 100644 --- a/src/routes/api/Datapoints/+server.ts +++ b/src/routes/api/Datapoints/+server.ts @@ -375,7 +375,7 @@ export async function GET(event) { datapoints.forEach((datapoint) => { responseData[datapoint.id] = { ...datapoint, id: undefined }; }); - simplifyGps(requestedTrip, 500); + simplifyGps(requestedTrip, 100000); return new Response(JSON.stringify(responseData)); } catch (error_message) { if (error_message instanceof Error) {