@geoapify/route-planner-sdk
Version:
TypeScript SDK for the Geoapify Route Planner API. Supports route optimization, delivery planning, and timeline visualization in browser and Node.js
62 lines (61 loc) • 3.21 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { LegRecalculator } from "./leg-recalculator";
export class AgentPlanRecalculator {
static recalculate(context, agentIndex) {
return __awaiter(this, void 0, void 0, function* () {
const agentFeature = context.getAgentFeature(agentIndex);
const waypoints = agentFeature.properties.waypoints;
const legs = agentFeature.properties.legs || [];
yield LegRecalculator.fillMissingLegData(context, agentFeature);
// we start at currect agent time
let time = agentFeature.properties.start_time;
const actions = [];
for (let i = 0; i < waypoints.length; i++) {
const fromPrevWaypointTravelTime = i > 0 ? legs[i - 1].time : 0;
time += fromPrevWaypointTravelTime;
waypoints[i].start_time = time;
// update leg indexes
if (i === 0)
delete waypoints[i].prev_leg_index;
else
waypoints[i].prev_leg_index = i - 1;
if (i === waypoints.length - 1)
delete waypoints[i].next_leg_index;
else
waypoints[i].next_leg_index = i;
// update actions
waypoints[i].actions.forEach(action => {
action.waypoint_index = i;
action.index = actions.length;
action.start_time = time;
time += action.duration || 0;
actions.push(action);
});
waypoints[i].duration = time - waypoints[i].start_time;
// update locations
if (i < waypoints.length - 1) {
const correspondingGeometry = agentFeature.geometry.coordinates[i];
waypoints[i].location = correspondingGeometry[0];
}
else {
const correspondingGeometry = agentFeature.geometry.coordinates[i - 1];
waypoints[i].location = correspondingGeometry[correspondingGeometry.length - 1];
}
}
agentFeature.properties.end_time = time;
agentFeature.properties.time = agentFeature.properties.end_time - agentFeature.properties.start_time;
agentFeature.properties.actions = actions;
agentFeature.properties.distance = legs.reduce((sum, leg) => {
return sum + leg.distance;
}, 0);
});
}
}