@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
92 lines (91 loc) • 3.66 kB
JavaScript
import { Utils } from "./utils";
export class RoutePlannerResultConverter {
static generateRoutePlannerResultData(response) {
var _a, _b, _c;
let clonedResponse = Utils.cloneObject(response);
return {
agents: this.generateAgents(clonedResponse),
inputData: clonedResponse.properties.params,
unassignedAgents: (_a = clonedResponse.properties.issues) === null || _a === void 0 ? void 0 : _a.unassigned_agents,
unassignedJobs: (_b = clonedResponse.properties.issues) === null || _b === void 0 ? void 0 : _b.unassigned_jobs,
unassignedShipments: (_c = clonedResponse.properties.issues) === null || _c === void 0 ? void 0 : _c.unassigned_shipments
};
}
static generateAgents(response) {
let result = [];
response.features.forEach((feature) => {
let properties = feature.properties;
result.push({
agentIndex: properties.agent_index,
agentId: properties.agent_id,
time: properties.time,
start_time: properties.start_time,
end_time: properties.end_time,
distance: properties.distance,
mode: properties.mode,
legs: this.generateRouteLegs(properties.legs),
actions: this.generateActions(properties.actions),
waypoints: this.generateWaypoints(properties.waypoints)
});
});
return result;
}
static generateRouteLegs(response) {
if (response === undefined) {
return [];
}
else {
return response.map((leg) => {
return {
time: leg.time,
distance: leg.distance,
steps: this.generateRouteLegSteps(leg.steps),
from_waypoint_index: leg.from_waypoint_index,
to_waypoint_index: leg.to_waypoint_index,
};
});
}
}
static generateRouteLegSteps(response) {
return response.map((legStep) => {
return {
distance: legStep.distance,
time: legStep.distance,
from_index: legStep.from_index,
to_index: legStep.to_index
};
});
}
static generateActions(response) {
return response.map((action) => {
return {
type: action.type,
start_time: action.start_time,
duration: action.duration,
shipment_index: action.shipment_index,
shipment_id: action.shipment_id,
location_index: action.location_index,
location_id: action.location_id,
job_index: action.job_index,
job_id: action.job_id,
index: action.index,
waypoint_index: action.waypoint_index
};
});
}
static generateWaypoints(response) {
return response.map((waypoint) => {
return {
original_location: waypoint.original_location,
original_location_index: waypoint.original_location_index,
original_location_id: waypoint.original_location_id,
location: waypoint.location,
start_time: waypoint.start_time,
duration: waypoint.duration,
actions: this.generateActions(waypoint.actions),
prev_leg_index: waypoint.prev_leg_index,
next_leg_index: waypoint.next_leg_index
};
});
}
}