UNPKG

mobility-toolbox-js

Version:

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

216 lines (215 loc) 8.83 kB
import { Feature } from 'ol'; import Control from 'ol/control/Control'; import { GeoJSON } from 'ol/format'; import { LineString, Point } from 'ol/geom'; import { Modify } from 'ol/interaction'; import VectorLayer from 'ol/layer/Vector'; import VectorSource from 'ol/source/Vector'; import { RoutingAPI } from '../../api'; import type { Map, MapBrowserEvent } from 'ol'; import type { Options } from 'ol/control/Control'; import type { Coordinate } from 'ol/coordinate'; import type { EventsKey } from 'ol/events'; import type { Geometry } from 'ol/geom'; import type { ModifyEvent } from 'ol/interaction/Modify'; import type { StyleLike } from 'ol/style/Style'; import type { RoutingGraph, RoutingMot, RoutingParameters, RoutingViaPoint } from '../../types'; export type AbortControllersByGraph = Record<string, AbortController>; export type RoutingControlOptions = { active?: boolean; apiKey?: string; apiParams?: RoutingParameters; graphs?: RoutingGraph[]; modify?: boolean; mot?: RoutingMot; onRouteError?: () => void; routingLayer?: VectorLayer<VectorSource>; snapToClosestStation?: boolean; stopsApiKey?: string; stopsApiUrl?: string; style?: StyleLike; useRawViaPoints?: boolean; } & Options; /** * This OpenLayers control allows the user to add and modifiy via points to * a map and request a route from the [geOps Routing API](https://developer.geops.io/apis/routing/). * * @example * import { Map } from 'ol'; * import { RoutingControl } from 'mobility-toolbox-js/ol'; * * const map = new Map({ * target: 'map' * }); * * const control = new RoutingControl(); * * map.addControl(control); * * @classproperty {string} mot - Mean of transport to be used for routing. * @classproperty {VectorLayer} routingLayer - Layer for adding route features. * @classproperty {boolean} loading - True if the control is requesting the backend. * * @see <a href="/example/ol-routing">Openlayers routing example</a> * * @extends {ol/control/Control~Control} * * @public */ declare class RoutingControl extends Control { abortControllers: Record<string, AbortController>; api?: RoutingAPI; apiKey?: string; apiParams?: RoutingParameters; cacheStationData: Record<string, Coordinate>; format: GeoJSON; graphs: RoutingGraph[]; graphsResolutions?: [number, number][]; initialRouteDrag?: { oldRoute?: Feature<LineString>; segmentIndex?: number; viaPoint?: Feature<Point>; }; map?: Map; modifyInteraction?: Modify; onMapClickKey?: EventsKey; onRouteError: (error?: Error, control?: RoutingControl) => void; routingLayer?: VectorLayer<VectorSource>; segments: Feature<LineString>[]; snapToClosestStation: boolean; stopsApiKey?: string; stopsApiUrl?: string; useRawViaPoints: boolean; viaPoints: RoutingViaPoint[]; get active(): boolean; set active(newValue: boolean); get loading(): boolean; set loading(newValue: boolean); get modify(): boolean; set modify(newValue: boolean); get mot(): RoutingMot; set mot(newValue: RoutingMot); /** * Constructor. * * @param {Object} options * @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/). * @param {Object} options.apiParams Request parameters. See [geOps Routing API documentation](https://developer.geops.io/apis/routing/). * @param {Array.<Array<graph="osm", minZoom=0, maxZoom=99>>} [options.graphs=[['osm', 0, 99]]] - Array of routing graphs and min/max zoom levels. If you use the control in combination with the [geOps Maps API](https://developer.geops.io/apis/maps/), you may want to use the optimal level of generalizations: "[['gen4', 0, 8], ['gen3', 8, 9], ['gen2', 9, 11], ['gen1', 11, 13], ['osm', 13, 99]]". * @param {string} [options.mot="bus"] Mean of transport to be used for routing. * @param {function} options.onRouteError Callback on request errors. * @param {VectorLayer} [options.routingLayer=new VectorLayer()] Vector layer for adding route features. * @param {boolean} [options.snapToClosestStation=false] If true, the routing will snap the coordinate to the closest station. Default to false. * @param {StyleLike} options.style Style of the default vector layer. * @param {boolean} [options.useRawViaPoints=fale] Experimental property. If true, it allows the user to add via points using different kind of string. See "via" parameter defined by the [geOps Routing API](https://developer.geops.io/apis/routing/). Default to false, only array of coordinates and station's id are supported as via points. * @param {string} [options.url='https://api.geops.io/routing/v1/'] [geOps Realtime API](https://developer.geops.io/apis/realtime/) url. * @public */ constructor(options?: RoutingControlOptions); /** * Calculate at which resolutions corresponds each generalizations. * * @private */ static getGraphsResolutions(graphs: RoutingGraph[], map: Map): [number, number][]; /** * Aborts viapoint and route requests * @private */ abortRequests(): void; activate(): void; /** * Add click listener to map. * @private */ addListeners(): void; /** * Adds/Replaces a viaPoint to the viaPoints array and redraws route: * Adds a viaPoint at end of array by default. * If an index is passed a viaPoint is added at the specified index. * If an index is passed and overwrite x is > 0, x viaPoints at the specified * index are replaced with a single new viaPoint. * @param {string | Coordinate} coordinatesOrString Array of coordinates or a string representing a station * @param {number} [index=-1] Integer representing the index of the added viaPoint. If not specified, the viaPoint is added at the end of the array. * @param {number} [overwrite=0] Marks the number of viaPoints that are removed at the specified index on add. * @public */ addViaPoint(coordinatesOrString: RoutingViaPoint, index?: number, overwrite?: number): void; /** * Define a default element. * * @private */ createDefaultElement(): void; /** * Create the interaction used to modify vertexes of features. * @private */ createModifyInteraction(): void; deactivate(): void; /** * Draws route on map using an array of coordinates: * If a single coordinate is passed a single point feature is added to map. * If two or more coordinates are passed a request to the RoutingAPI fetches * the route using the passed coordinates and the current mot. * @private */ drawRoute(): Promise<void | Feature<Geometry>> | Promise<Feature<Geometry> | null> | Promise<(void | never[])[]> | null; /** * Draw a via point. This function can parse all the possibilitiies * * @private */ drawViaPoint(viaPoint: RoutingViaPoint, idx: number, abortController: AbortController): Promise<void | Feature<Geometry>> | Promise<Feature<Geometry> | null>; /** * Activet7deactivate the control when activ eproperty changes * @private */ onActiveChange(): void; /** * Used on click on map while control is active: * By default adds a viaPoint to the end of array. * If an existing viaPoint is clicked removes the clicked viaPoint. * @private */ onMapClick(evt: MapBrowserEvent<PointerEvent>): void; /** * Used on end of the modify interaction. Resolves feature modification: * Line drag creates new viaPoint at the final coordinate of drag. * Point drag replaces old viaPoint. * @private */ onModifyEnd(evt: ModifyEvent): void | Promise<never>; /** * Used on start of the modify interaction. Stores relevant data * in this.initialRouteDrag object * @private */ onModifyStart(evt: ModifyEvent): void; /** * Remove click listener from map. * @private */ removeListeners(): void; /** * Removes a viaPoint at the passed array index and redraws route * By default the last viaPoint is removed. * @param {number} index Integer representing the index of the viaPoint to delete. * @public */ removeViaPoint(index?: number): void; render(): void; /** * Removes all viaPoints, clears the source and triggers a change event * @public */ reset(): void; setMap(map: Map): void; /** * Replaces the current viaPoints with a new coordinate array. * @param {Array<Array<number>>} coordinateArray Array of nested coordinates * @public */ setViaPoints(coordinateArray: Coordinate[]): void; } export default RoutingControl;