@hexide-digital/geoportal.lt
Version:
Set of libs for geoportal.lt
76 lines (75 loc) • 2.92 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 axios from 'axios';
import { merge } from 'lodash';
export default class Solver {
constructor(url, options = {}) {
this.url = 'https://www.geoportal.lt/map/proxy/routing/Route/solve';
this.options = {
returnDirections: true,
returnRoutes: true,
returnZ: true,
returnStops: true,
returnBarriers: false,
returnPolygonBarriers: false,
returnPolylineBarriers: false,
outSR: 4326,
outputLines: 'esriNAOutputLineTrueShape',
impedanceAttributeName: 'minutes',
directionsLanguage: 'en',
travelMode: null,
};
if (url) {
this.url = url;
}
this.options = merge({}, this.options, options);
}
buildUrl(points) {
if (points.length < 2) {
throw new Error('Points array must contain at least 2 points');
}
const query = Object.entries(this.options)
.map(([key, val]) => encodeURIComponent(key) + '=' + encodeURIComponent(val))
.join('&');
let url = this.url + '?f=json&' + query;
const wp = {
type: 'features',
features: Array(),
doNotLocateOnRestrictedElements: true,
};
const name = points[0].label + ' - ' + points[points.length - 1].label;
points.forEach((point, index) => {
wp.features.push({
geometry: {
x: point.lng,
y: point.lat,
spatialReference: {
wkid: this.options.outSR,
},
},
attributes: {
Name: point.label,
RouteName: name,
},
});
});
url += '&stops=' + encodeURIComponent(JSON.stringify(wp));
return url;
}
solve(points) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield axios.get(this.buildUrl(points));
if (response.data) {
return response.data;
}
throw new Error('No route has been returned');
});
}
}