UNPKG

@geoapify/route-planner-sdk

Version:

A TypeScript SDK for the Geoapify Route Planner API that simplifies route optimization requests, and helps visualize and edit resulting routes.

96 lines (95 loc) 3.96 kB
import { RoutePlannerResult } from "../models/entities/route-planner-result"; export class RoutePlannerResultConverter { static convert(options, inputData, response) { let routePlannerResultData = this.generateRoutePlannerResultData(inputData, response); return new RoutePlannerResult(options, routePlannerResultData); } static generateRoutePlannerResultData(inputData, response) { var _a, _b, _c; return { agents: this.generateAgents(response), // NOTE: we can generate the inputData according to RoutePlannerResultResponseData, but it will be the same object inputData: inputData, unassignedAgents: (_a = response.properties.issues) === null || _a === void 0 ? void 0 : _a.unassigned_agents, unassignedJobs: (_b = response.properties.issues) === null || _b === void 0 ? void 0 : _b.unassigned_jobs, unassignedShipments: (_c = response.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 }; }); } }