Skip to content

Commit

Permalink
added batch simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
derGraph committed Sep 1, 2024
1 parent 7d7c2f8 commit 16c89e8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
84 changes: 49 additions & 35 deletions src/lib/server/simplifyGps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "%");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/Datapoints/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 16c89e8

Please sign in to comment.