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

145 lines (144 loc) 5.52 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 { RouteLeg } from "./route-leg"; import { RouteAction } from "./route-action"; import { Waypoint } from "./waypoint"; export class AgentPlan { constructor(raw, agentInputData, routingOptions, callOptions, violations) { this.raw = raw; this.agentInputData = agentInputData; this.routingOptions = routingOptions; this.callOptions = callOptions; this.violations = violations; if (!raw) { throw new Error("AgentSolutionData is undefined"); } } getRaw() { return this.raw; } getAgentIndex() { return this.raw.agent_index; } getAgentId() { return this.raw.agent_id; } getTime() { return this.raw.time; } getStartTime() { return this.raw.start_time; } getEndTime() { return this.raw.end_time; } getDistance() { return this.raw.distance; } getMode() { return this.raw.mode; } getLegs() { return this.raw.legs.map((leg) => new RouteLeg(leg)); } getActions() { return this.raw.actions.map((action) => new RouteAction(action)); } getDelays() { return this.getActions().filter((action) => action.getType() === 'delay'); } getWaypoints() { return this.raw.waypoints.map((waypoint) => new Waypoint(waypoint)); } getPlannedShipments() { return [...new Set(this.raw.actions.filter(action => typeof action.shipment_index !== 'undefined').map(action => { return action.shipment_index; }))]; } getPlannedJobs() { return [...new Set(this.raw.actions.filter(action => typeof action.job_index !== 'undefined').map(action => { return action.job_index; }))]; } getAgentInputData() { return this.agentInputData; } containsShipment(shipmentIdOrIndex) { return this.getActions().some(action => action.getShipmentIndex() === shipmentIdOrIndex || action.getShipmentId() === shipmentIdOrIndex); } containsJob(jonIdOrIndex) { return this.getActions().some(action => action.getJobIndex() === jonIdOrIndex || action.getJobId() === jonIdOrIndex); } getViolations() { return this.violations; } /** * Retrieves the route for a specific agent. * @param agentIdOrIndex - The ID or index of the agent. * @param options - The routing options. */ getRoute(routingOptions) { var _a; return __awaiter(this, void 0, void 0, function* () { const waypointLocations = this.getWaypoints().map((waypoint) => waypoint.getLocation()); const waypoints = waypointLocations.map((location) => "lonlat:" + location).join("|"); if (waypoints.length === 0) return undefined; const response = yield fetch(this.constructRoutingRequest(waypoints, routingOptions || this.routingOptions, this.callOptions)); const result = yield response.json(); const feature = (_a = result === null || result === void 0 ? void 0 : result.features) === null || _a === void 0 ? void 0 : _a[0]; if (!feature) { return { type: "Feature", geometry: { type: "LineString", coordinates: waypointLocations }, properties: { agent_index: this.getAgentIndex(), agent_id: this.getAgentId() } }; } feature.properties = Object.assign(Object.assign({}, (feature.properties || {})), { agent_index: this.getAgentIndex(), agent_id: this.getAgentId() }); return feature; }); } constructRoutingRequest(waypoints, options, callOptions) { let url = `${callOptions.baseUrl}/v1/routing?waypoints=${waypoints}&apiKey=${callOptions.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.map(a => a.type).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; } }