@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) • 4.39 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 { RoutingApiError } from "../../../../../models";
/**
* Helper class for Routing API calls to calculate consecutive travel times.
*/
export class RoutingHelper {
constructor(options, routingOptions) {
this.baseUrl = options.baseUrl || 'https://api.geoapify.com';
this.apiKey = options.apiKey;
this.routingOptions = routingOptions;
}
calculateConsecutiveTravelTimes(locations) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
if (locations.length < 2)
return [];
const waypoints = locations.map(loc => `lonlat:${loc[0]},${loc[1]}`).join('|');
const url = this.constructRoutingUrl(waypoints);
const response = yield fetch(url);
if (!response.ok) {
throw new RoutingApiError(`Routing API failed: ${response.statusText}`, response.status, response.statusText);
}
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 || !((_b = feature.properties) === null || _b === void 0 ? void 0 : _b.legs)) {
// No route found, return zeros as fallback
return new Array(locations.length - 1).fill(0);
}
return feature.properties.legs.map((leg) => leg.time);
});
}
calculateLegData(locations) {
return __awaiter(this, void 0, void 0, function* () {
const routeData = yield this.calculateRouteData(locations);
return routeData.legs;
});
}
calculateRouteData(locations) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
if (locations.length < 2) {
return { legs: [], waypoints: [] };
}
const waypoints = locations.map(loc => `lonlat:${loc[0]},${loc[1]}`).join('|');
const url = this.constructRoutingUrl(waypoints);
const response = yield fetch(url);
if (!response.ok) {
throw new RoutingApiError(`Routing API failed: ${response.statusText}`, response.status, response.statusText);
}
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 || !((_b = feature.properties) === null || _b === void 0 ? void 0 : _b.legs)) {
return { legs: [], waypoints: [] };
}
return {
legs: feature.properties.legs || [],
waypoints: feature.properties.waypoints || []
};
});
}
constructRoutingUrl(waypoints) {
let url = `${this.baseUrl}/v1/routing?waypoints=${waypoints}&apiKey=${this.apiKey}`;
if (this.routingOptions.mode) {
url += `&mode=${this.routingOptions.mode}`;
}
if (this.routingOptions.type) {
url += `&type=${this.routingOptions.type}`;
}
if (this.routingOptions.units) {
url += `&units=${this.routingOptions.units}`;
}
if (this.routingOptions.avoid && this.routingOptions.avoid.length > 0) {
url += `&avoid=${this.routingOptions.avoid.map((a) => a.type).join('|')}`;
}
if (this.routingOptions.traffic) {
url += `&traffic=${this.routingOptions.traffic}`;
}
if (this.routingOptions.max_speed) {
url += `&max_speed=${this.routingOptions.max_speed}`;
}
return url;
}
}