UNPKG

@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

388 lines (387 loc) 13.3 kB
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 { AgentSolution } from "./nested/result/agent-solution"; import { RouteActionInfo } from "./nested/result/route-action-info"; import { RoutePlannerResultConverter } from "../../tools/route-planner-result-converter"; import { JobSolution } from "./nested/result/job-solution"; import { ShipmentSolution } from "./nested/result/shipment-solution"; /** * Provides convenient methods for reading Route Planner API results. */ export class RoutePlannerResult { constructor(options, rawData) { this.rawData = rawData; this.options = options; } /** * Returns the data returned by the Route Planner API. */ getData() { return RoutePlannerResultConverter.generateRoutePlannerResultData(this.rawData); } /** * Returns the raw data returned by the Route Planner API. */ getRawData() { return this.rawData; } /** * Returns a list of all assigned agent solutions. */ getAgentSolutions() { return this.getData().agents.map(agent => new AgentSolution(agent)); } /** * Returns a list of all agent solutions by index. (if it's not assigned, then it will be undefined) */ getAgentSolutionsByIndex() { let data = this.getData(); let result = Array(data.inputData.agents.length); this.getData().agents.forEach(agent => { let agentSolution = new AgentSolution(agent); result[agentSolution.getAgentIndex()] = agentSolution; }); return result; } /** * Finds an agent's solution by their ID. */ getAgentSolution(agentId) { let agentFound = this.getData().agents.find(agent => agent.agentId === agentId); if (agentFound === undefined) { return undefined; } else { return new AgentSolution(agentFound); } } /** * Finds an agent's solution by their index. */ getAgentSolutionByIndex(agentIndex) { let agentFound = this.getData().agents.find(agent => agent.agentIndex === agentIndex); if (agentFound === undefined) { return undefined; } else { return new AgentSolution(agentFound); } } /** * Retrieves all waypoints of a specific agent. */ getAgentWaypoints(agentId) { const agent = this.getAgentSolution(agentId); return agent ? agent.getWaypoints() : []; } /** * Retrieves all route actions of a specific agent. */ getAgentRouteActions(agentId) { const agent = this.getAgentSolution(agentId); return agent ? agent.getActions() : []; } /** * Retrieves all route legs of a specific agent. */ getAgentRouteLegs(agentId) { const agent = this.getAgentSolution(agentId); return agent ? agent.getLegs() : []; } /** * Retrieves the options used to generate the result. */ getOptions() { return this.options; } /** * Retrieves all jobs assigned to a specific agent. */ getAgentJobs(agentId) { const agent = this.getAgentSolution(agentId); if (agent === undefined) { return []; } return agent.getActions() .filter(action => action.getJobIndex() !== undefined) .map(action => action.getJobIndex()); } /** * Retrieves all shipments assigned to a specific agent. */ getAgentShipments(agentId) { const agent = this.getAgentSolution(agentId); if (agent === undefined) { return []; } return Array.from(new Set(agent.getActions() .filter(action => action.getShipmentIndex() !== undefined) .map(action => action.getShipmentIndex()))).sort((a, b) => a - b); } /** * Retrieves unassigned agents. */ getUnassignedAgents() { let data = this.getData(); if (!data.unassignedAgents || data.unassignedAgents.length == 0) { return []; } else { return data.unassignedAgents.map(index => { return data.inputData.agents[index]; }); } } /** * Retrieves unassigned jobs. */ getUnassignedJobs() { let data = this.getData(); if (!data.unassignedJobs || data.unassignedJobs.length == 0) { return []; } else { return data.unassignedJobs.map(index => { return data.inputData.jobs[index]; }); } } /** * Retrieves unassigned shipments. */ getUnassignedShipments() { let data = this.getData(); if (!data.unassignedShipments || data.unassignedShipments.length == 0) { return []; } else { return data.unassignedShipments.map(index => { return data.inputData.shipments[index]; }); } } /** * Returns a list of all assigned jobs */ getJobSolutions() { let result = []; let agents = this.getAgentSolutions(); agents.forEach(agent => { let agentActions = agent.getActions(); let actionsByJob = {}; agentActions.forEach(action => { const jobIndex = action.getJobIndex(); if (jobIndex !== undefined) { if (!actionsByJob[jobIndex]) { actionsByJob[jobIndex] = []; } actionsByJob[jobIndex].push(action.getRaw()); } }); for (const jobIndex in actionsByJob) { const actions = actionsByJob[+jobIndex]; let jobSolution = { agentId: agent.getAgentId(), actions: actions, agent: agent.getRaw(), job: this.rawData.properties.params.jobs[actions[0].job_index] }; result.push(new JobSolution(jobSolution)); } }); return result; } /** * Finds job solution by their ID. */ getJobSolution(jobId) { for (let jobSolution of this.getJobSolutions()) { if (jobSolution.getJob().getRaw().id === jobId) { return jobSolution; } } return undefined; } /** * Returns a list of all assigned shipments */ getShipmentSolutions() { let result = []; let agents = this.getAgentSolutions(); agents.forEach(agent => { let agentActions = agent.getActions(); let actionsByShipment = {}; agentActions.forEach(action => { const jobIndex = action.getShipmentIndex(); if (jobIndex !== undefined) { if (!actionsByShipment[jobIndex]) { actionsByShipment[jobIndex] = []; } actionsByShipment[jobIndex].push(action.getRaw()); } }); for (const jobIndex in actionsByShipment) { const actions = actionsByShipment[+jobIndex]; let shipment = { agentId: agent.getAgentId(), actions: actions, agent: agent.getRaw(), shipment: this.rawData.properties.params.shipments[actions[0].shipment_index] }; result.push(new ShipmentSolution(shipment)); } }); return result; } /** * Finds shipment solution by their ID. */ getShipmentSolution(jobId) { for (let shipmentSolution of this.getShipmentSolutions()) { if (shipmentSolution.getShipment().getRaw().id === jobId) { return shipmentSolution; } } return undefined; } /** * Retrieves detailed information about a specific job. */ getJobInfo(jobId) { if (!jobId) { return undefined; } let actions = []; let agentFound; for (const agent of this.getAgentSolutions()) { for (const action of agent.getActions()) { if (action.getJobId() === jobId) { actions.push(action); agentFound = agent; } } } if (actions.length !== 0 && agentFound) { return new RouteActionInfo({ agentId: agentFound.getAgentId(), actions: actions, agent: agentFound }); } return undefined; // Job not found } /** * Retrieves detailed information about a specific shipment. */ getShipmentInfo(shipmentId) { if (!shipmentId) { return undefined; } let actions = []; let agentFound; for (const agent of this.getAgentSolutions()) { for (const action of agent.getActions()) { if (action.getShipmentId() === shipmentId) { actions.push(action); agentFound = agent; } } } if (actions.length !== 0 && agentFound) { return new RouteActionInfo({ agentId: agentFound.getAgentId(), actions: actions, agent: agentFound }); } return undefined; // Shipment not found } /** * Retrieves detailed information about a specific job. */ getJobInfoByIndex(jobIndex) { if (jobIndex < 0) { return undefined; } let actions = []; let agentFound; for (const agent of this.getAgentSolutions()) { for (const action of agent.getActions()) { if (action.getJobIndex() === jobIndex) { actions.push(action); agentFound = agent; } } } if (actions.length !== 0 && agentFound) { return new RouteActionInfo({ agentId: agentFound.getAgentId(), actions: actions, agent: agentFound }); } return undefined; // Job not found } /** * Retrieves detailed information about a specific shipment. */ getShipmentInfoByIndex(shipmentIndex) { if (shipmentIndex < 0) { return undefined; } let actions = []; let agentFound; for (const agent of this.getAgentSolutions()) { for (const action of agent.getActions()) { if (action.getShipmentIndex() === shipmentIndex) { actions.push(action); agentFound = agent; } } } if (actions.length !== 0 && agentFound) { return new RouteActionInfo({ agentId: agentFound.getAgentId(), actions: actions, agent: agentFound }); } return undefined; // Shipment not found } /** * Retrieves the route for a specific agent. * @param agentId - The ID of the agent. * @param options - The routing options. */ getAgentRoute(agentId, options) { return __awaiter(this, void 0, void 0, function* () { const agent = this.getAgentSolution(agentId); if (!agent) return undefined; let waypoints = agent.getWaypoints().map(waypoint => "lonlat:" + waypoint.getLocation()).join('|'); if (waypoints.length == 0) return undefined; const response = yield fetch(this.constructRoutingRequest(waypoints, options)); return yield response.json(); }); } constructRoutingRequest(waypoints, options) { let url = `${this.getOptions().baseUrl}/v1/routing?waypoints=${waypoints}&apiKey=${this.getOptions().apiKey}`; if (options.mode) { url += `&mode=${options.mode}`; } if (options.type) { url += `&type=${options.type}`; } if (options.units) { url += `&units=${options.units}`; } if (options.lang) { url += `&lang=${options.lang}`; } if (options.avoid && options.avoid.length > 0) { url += `&avoid=${options.avoid.join('|')}`; } if (options.details && options.details.length > 0) { url += `&details=${options.details.join(',')}`; } if (options.traffic) { url += `&traffic=${options.traffic}`; } if (options.max_speed) { url += `&max_speed=${options.max_speed}`; } return url; } }