UNPKG

mobility-toolbox-js

Version:

Toolbox for JavaScript applications in the domains of mobility and logistics.

265 lines (264 loc) 10.8 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()); }); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _MapsetLayer_abortController; import VectorLayer from 'ol/layer/Vector'; import { unByKey } from 'ol/Observable'; import { transformExtent } from 'ol/proj'; import { Vector } from 'ol/source'; import MapsetAPI from '../../api/MapsetApi'; import MapsetKmlFormat from '../utils/MapsetKmlFormat'; const kmlFormatter = new MapsetKmlFormat(); /** * An OpenLayers layer able to display plan data from [mapset](https://geops.com/de/solution/mapset). * * @example * import { MapsetLayer } from 'mobility-toolbox-js/ol'; * * const layer = new MapsetLayer({ * apiKey: 'yourApiKey', * // tags: ['test'], * // tenants: ['geopstest'], * // url: 'https://editor.mapset.io/api/v1', * }); * * @see <a href="/doc/class/build/api/MapsetApi%20js~MapsetAPI%20html-offset-anchor">MapsetAPI</a> * @see <a href="/example/ol-mapset-layer">OpenLayers Mapset layer example</a> * * * @extends {ol/layer/VectorLayer~VectorLayer} * * @public */ class MapsetLayer extends VectorLayer { get api() { return this.get('api'); } set api(value) { this.set('api', value); void this.fetchPlans(); } get apiKey() { return this.api.apiKey; } set apiKey(value) { if (this.api.apiKey !== value) { this.api.apiKey = value; void this.fetchPlans(); } } get doNotRevert32pxScaling() { return this.get('doNotRevert32pxScaling'); } set doNotRevert32pxScaling(value) { this.set('doNotRevert32pxScaling', value); this.updateFeatures(); } get planId() { return this.get('planId'); } set planId(value) { if (this.planId !== value) { this.set('planId', value); void this.fetchPlanById(value); } } get plans() { return this.get('plans'); } set plans(value) { this.set('plans', value); this.updateFeatures(); } get tags() { return this.api.tags; } set tags(value) { var _a; if (((_a = this.api.tags) === null || _a === void 0 ? void 0 : _a.toString()) !== (value === null || value === void 0 ? void 0 : value.toString())) { this.api.tags = value; void this.fetchPlans(); } } get tenants() { return this.api.tenants; } set tenants(value) { var _a; if (((_a = this.api.tenants) === null || _a === void 0 ? void 0 : _a.toString()) !== (value === null || value === void 0 ? void 0 : value.toString())) { this.api.tenants = value; void this.fetchPlans(); } } get timestamp() { return this.get('timestamp'); } set timestamp(value) { if (this.timestamp !== value) { this.set('timestamp', value); void this.fetchPlans(); } } get url() { var _a; return (_a = this.api) === null || _a === void 0 ? void 0 : _a.url; } set url(value) { var _a; if (this.api && ((_a = this.api) === null || _a === void 0 ? void 0 : _a.url) !== value) { this.api.url = value; void this.fetchPlans(); } } /** * Constructor. * * @param {Object} options * @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/). * @param {string[]} [options.tags] The tags of the required plans. * @param {string} [options.timestamp] The timestamp of the required plans. * @param {string[]} [options.tenants] The tenants of the required plans. * @param {string} [options.url] The URL of the [geOps Mapset API](https://geops.com/de/solution/mapset). * @public */ constructor(options) { var _a, _b; super(Object.assign({ source: (_a = options.source) !== null && _a !== void 0 ? _a : new Vector() }, (options || {}))); this.loadAll = true; this.olEventsKeys = []; _MapsetLayer_abortController.set(this, void 0); this.api = (_b = options.api) !== null && _b !== void 0 ? _b : new MapsetAPI({ apiKey: options.apiKey, tags: options.tags, tenants: options.tenants, url: options.url, }); __classPrivateFieldSet(this, _MapsetLayer_abortController, new AbortController(), "f"); if (options.loadAll === false) { this.loadAll = options.loadAll; } } fetchPlanById(planId) { return __awaiter(this, void 0, void 0, function* () { var _a; (_a = __classPrivateFieldGet(this, _MapsetLayer_abortController, "f")) === null || _a === void 0 ? void 0 : _a.abort(); __classPrivateFieldSet(this, _MapsetLayer_abortController, new AbortController(), "f"); if (!planId) { this.plans = []; return; } let planById; try { this.dispatchEvent('featuresloadstart'); planById = yield this.api.getPlanById(planId, { signal: __classPrivateFieldGet(this, _MapsetLayer_abortController, "f").signal, }); this.plans = [planById]; this.dispatchEvent('featuresloadend'); } catch (e) { // @ts-expect-error Abort errors are OK if ((e === null || e === void 0 ? void 0 : e.name).includes('AbortError')) { // Ignore abort error return; } // eslint-disable-next-line no-console console.error('MapsetLayer: Error fetching plan by ID...', e); this.plans = []; this.dispatchEvent('featuresloaderror'); throw e; } return; }); } fetchPlans() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!this.getVisible()) { return; } const view = (_a = this.getMapInternal()) === null || _a === void 0 ? void 0 : _a.getView(); if (!view) { return; } const extent = transformExtent(view.calculateExtent(), view.getProjection(), 'EPSG:4326'); const zoom = view.getZoom(); if (!zoom || !extent) { this.plans = []; return; } (_b = __classPrivateFieldGet(this, _MapsetLayer_abortController, "f")) === null || _b === void 0 ? void 0 : _b.abort(); __classPrivateFieldSet(this, _MapsetLayer_abortController, new AbortController(), "f"); let plans = []; try { this.dispatchEvent('featuresloadstart'); plans = yield this.api.getPlans({ bbox: extent === null || extent === void 0 ? void 0 : extent.toString(), timestamp: (_c = this.timestamp) !== null && _c !== void 0 ? _c : new Date().toISOString(), zoom, }, { signal: __classPrivateFieldGet(this, _MapsetLayer_abortController, "f").signal }); this.plans = plans; this.dispatchEvent('featuresloadend'); } catch (e) { // @ts-expect-error Abort errors are OK if ((e === null || e === void 0 ? void 0 : e.name).includes('AbortError')) { // Ignore abort error return []; } console.error('MapsetLayer: Error fetching plans...', e); this.dispatchEvent('featuresloaderror'); throw e; } }); } setMapInternal(map) { super.setMapInternal(map); if (map && this.loadAll) { void this.fetchPlans(); this.olEventsKeys.push(map.on('moveend', () => { void this.fetchPlans(); }), this.on('change:visible', () => { void this.fetchPlans(); })); } else { unByKey(this.olEventsKeys); } if (map && this.planId) { void this.fetchPlanById(this.planId); } } updateFeatures() { var _a, _b, _c, _d, _e; (_a = this.getSource()) === null || _a === void 0 ? void 0 : _a.clear(); const map = this.getMapInternal(); if (map && ((_b = this.plans) === null || _b === void 0 ? void 0 : _b.length) !== 0) { const features = (_d = (_c = this.plans) === null || _c === void 0 ? void 0 : _c.flatMap((plan) => { return kmlFormatter.readFeatures(plan.data, map.getView().getProjection(), this.doNotRevert32pxScaling); })) !== null && _d !== void 0 ? _d : []; (_e = this.getSource()) === null || _e === void 0 ? void 0 : _e.addFeatures(features); } this.dispatchEvent('updatefeatures'); } } _MapsetLayer_abortController = new WeakMap(); export default MapsetLayer;