mobility-toolbox-js 3.0.0-beta.13 → 3.0.0-beta.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +1,56 @@
1
1
  import { getUid } from 'ol';
2
+ import debounce from 'lodash.debounce';
2
3
  import getLayersAsFlatArray from '../../common/utils/getLayersAsFlatArray';
4
+ const deprecated = debounce((message) => {
5
+ // eslint-disable-next-line no-console
6
+ console.warn(message);
7
+ }, 1000);
3
8
  /**
4
9
  * This mixin adds some properties to access ol custom properties easily.
5
10
  */
6
11
  function PropertiesLayerMixin(Base) {
7
12
  return class PropertiesLayer extends Base {
13
+ /** @deprecated */
8
14
  get children() {
15
+ deprecated("Layer.children is deprecated. Use the Layer.get('children') method instead.");
9
16
  return this.get('children') || [];
10
17
  }
18
+ /** @deprecated */
11
19
  set children(newValue) {
20
+ deprecated("Layer.children is deprecated. Use the Layer.set('children', children) method instead.");
12
21
  this.set('children', newValue || []);
13
22
  }
23
+ /** @deprecated */
14
24
  get copyrights() {
15
- // eslint-disable-next-line no-console
16
- console.warn('Deprecated. Use the source object to get the attributions');
25
+ deprecated('Layer.copyrights is deprecated. Get the attributions from the source object');
17
26
  return this.get('copyrights');
18
27
  }
28
+ /** @deprecated */
19
29
  set copyrights(newCopyrights) {
20
- // eslint-disable-next-line no-console
21
- console.warn('Deprecated. Use the source object to set the attributions');
30
+ deprecated('Layer.copyrights is deprecated. Set the attributions to the source object.');
22
31
  const arrValue = newCopyrights && !Array.isArray(newCopyrights)
23
32
  ? [newCopyrights]
24
33
  : newCopyrights;
25
34
  this.set('copyrights', arrValue || []);
26
35
  }
36
+ /** @deprecated */
27
37
  get disabled() {
38
+ deprecated("Layer.disabled is deprecated. Use the Layer.get('disabled') method instead.");
28
39
  return this.get('disabled');
29
40
  }
41
+ /** @deprecated */
30
42
  set disabled(newValue) {
43
+ deprecated("Layer.disabled is deprecated. Use the Layer.set('disabled', newValue) method instead.");
31
44
  this.set('disabled', newValue);
32
45
  }
46
+ /** @deprecated */
33
47
  get group() {
48
+ deprecated("Layer.group is deprecated. Use the Layer.get('group') method instead.");
34
49
  return this.get('group');
35
50
  }
51
+ /** @deprecated */
36
52
  get hitTolerance() {
37
- // eslint-disable-next-line no-console
38
- console.warn('Deprecated. Pass the pixelTolerance when you request the features.');
53
+ deprecated('Layer.hitTolerance is deprecated.Pass the hitTolerance when you request the features.');
39
54
  return this.get('hitTolerance') || 5;
40
55
  }
41
56
  get key() {
@@ -44,29 +59,39 @@ function PropertiesLayerMixin(Base) {
44
59
  get map() {
45
60
  return this.getMapInternal();
46
61
  }
62
+ /** @deprecated */
47
63
  get name() {
64
+ deprecated("Layer.name is deprecated. Use the Layer.get('name') method instead.");
48
65
  return this.get('name');
49
66
  }
67
+ /** @deprecated */
50
68
  get olLayer() {
51
- // eslint-disable-next-line no-console
52
- console.warn("Deprecated property: mobility-toolbox-js/ol layers inherits now from ol/layer/Layer class. This getter is only a redirect to the current 'this' object.");
69
+ deprecated("Layer.olLayer is deprecated. mobility-toolbox-js/ol layers inherits now from ol/layer/Layer class. This getter is only a redirect to the current 'this' object.");
53
70
  return this;
54
71
  }
72
+ /** @deprecated */
55
73
  // eslint-disable-next-line class-methods-use-this
56
74
  set olLayer(newValue) {
57
- // eslint-disable-next-line no-console
58
- console.log('Deprecated property: mobility-toolbox-js/ol layers inherits now from ol/layer/Layer class. This setter has no effect.');
75
+ deprecated('Layer.olLayer is deprecated. mobility-toolbox-js/ol layers inherits now from ol/layer/Layer class. This setter has no effect.');
59
76
  }
77
+ /** @deprecated */
60
78
  get parent() {
79
+ deprecated("Layer.parent is deprecated. Use the Layer.get('parent') method instead.");
61
80
  return this.get('parent');
62
81
  }
82
+ /** @deprecated */
63
83
  set parent(newValue) {
84
+ deprecated("Layer.parent is deprecated. Use the Layer.set('parent', parent) method instead.");
64
85
  this.set('parent', newValue);
65
86
  }
87
+ /** @deprecated */
66
88
  get visible() {
89
+ deprecated('Layer.visible is deprecated. Use the Layer.getVisible() method instead.');
67
90
  return this.getVisible();
68
91
  }
92
+ /** @deprecated */
69
93
  set visible(newValue) {
94
+ deprecated('Layer.visible is deprecated. Use the Layer.setVisible(newValue) method instead.');
70
95
  this.setVisible(newValue);
71
96
  }
72
97
  constructor(...args) {
@@ -74,13 +99,12 @@ function PropertiesLayerMixin(Base) {
74
99
  const options = args[0];
75
100
  super(options);
76
101
  this.options = {};
77
- this.olListenersKeys = [];
102
+ this.olEventsKeys = [];
78
103
  if (options.properties) {
79
- // eslint-disable-next-line no-console
80
- console.warn("Deprecated. Don't use properties options. Pass the values directly in options object.");
104
+ deprecated("Deprecated. Don't use properties options. Pass the values directly in options object.");
81
105
  this.setProperties(options.properties);
82
106
  }
83
- (_a = this.olListenersKeys) === null || _a === void 0 ? void 0 : _a.push(
107
+ (_a = this.olEventsKeys) === null || _a === void 0 ? void 0 : _a.push(
84
108
  // Update parent property
85
109
  this.on('propertychange', (evt) => {
86
110
  if (evt.key === 'children') {
@@ -134,8 +158,10 @@ function PropertiesLayerMixin(Base) {
134
158
  }
135
159
  /**
136
160
  * Return the an array containing all the descendants of the layer in a flat array. Including the current layer.
161
+ * @deprecated
137
162
  */
138
163
  flat() {
164
+ deprecated('Layer.flat is deprecated. Use getLayersAsFlatArray utils method instead.');
139
165
  return getLayersAsFlatArray(this);
140
166
  }
141
167
  };
@@ -1,20 +0,0 @@
1
- import { FrameState } from 'ol/Map';
2
- import LayerRenderer from 'ol/renderer/Layer';
3
- import { Coordinate } from 'ol/coordinate';
4
- import { FeatureCallback } from 'ol/renderer/vector';
5
- import { Feature } from 'ol';
6
- import { Geometry } from 'ol/geom';
7
- import { Pixel } from 'ol/pixel';
8
- import type MaplibreLayer from '../layers/MaplibreLayer';
9
- /**
10
- * This class is a renderer for Maplibre Layer to be able to use the native ol
11
- * functionnalities like map.getFeaturesAtPixel or map.hasFeatureAtPixel.
12
- * @private
13
- */
14
- export default class MaplibreLayerRenderer extends LayerRenderer<MaplibreLayer> {
15
- getFeaturesAtCoordinate(coordinate: Coordinate | undefined, hitTolerance?: number): Feature<Geometry>[];
16
- prepareFrame(): boolean;
17
- renderFrame(frameState: FrameState): HTMLElement | null;
18
- getFeatures(pixel: Pixel): Promise<Feature<Geometry>[]>;
19
- forEachFeatureAtCoordinate<Feature>(coordinate: Coordinate, frameState: FrameState, hitTolerance: number, callback: FeatureCallback<Feature>): Feature | undefined;
20
- }
@@ -1,114 +1,142 @@
1
- import { toDegrees } from 'ol/math';
2
- import { toLonLat } from 'ol/proj';
3
- import LayerRenderer from 'ol/renderer/Layer';
4
- import GeoJSON from 'ol/format/GeoJSON';
5
- import { VECTOR_TILE_FEATURE_PROPERTY } from '../../common';
6
- /**
7
- * @private
8
- */
9
- const formats = {
10
- 'EPSG:3857': new GeoJSON({
11
- featureProjection: 'EPSG:3857',
12
- }),
13
- };
14
- /**
15
- * This class is a renderer for Maplibre Layer to be able to use the native ol
16
- * functionnalities like map.getFeaturesAtPixel or map.hasFeatureAtPixel.
17
- * @private
18
- */
19
- // @ts-ignore
20
- export default class MaplibreLayerRenderer extends LayerRenderer {
21
- getFeaturesAtCoordinate(coordinate, hitTolerance = 5) {
22
- var _a, _b;
23
- if (!coordinate) {
24
- return [];
25
- }
26
- const layer = this.getLayer();
27
- const map = layer.getMapInternal();
28
- const { mbMap } = layer;
29
- const projection = ((_b = (_a = map === null || map === void 0 ? void 0 : map.getView()) === null || _a === void 0 ? void 0 : _a.getProjection()) === null || _b === void 0 ? void 0 : _b.getCode()) || 'EPSG:3857';
30
- let features = [];
31
- if (!formats[projection]) {
32
- formats[projection] = new GeoJSON({
33
- featureProjection: projection,
34
- });
35
- }
36
- if (mbMap === null || mbMap === void 0 ? void 0 : mbMap.isStyleLoaded()) {
37
- const pixel = coordinate && mbMap.project(toLonLat(coordinate));
38
- if ((pixel === null || pixel === void 0 ? void 0 : pixel.x) && (pixel === null || pixel === void 0 ? void 0 : pixel.y)) {
39
- let pixels = [
40
- pixel.x,
41
- pixel.y,
42
- ];
43
- if (hitTolerance) {
44
- const [x, y] = pixels;
45
- pixels = [
46
- [x - hitTolerance, y - hitTolerance],
47
- [x + hitTolerance, y + hitTolerance],
48
- ];
49
- }
50
- // At this point we get GeoJSON Maplibre feature, we transform it to an OpenLayers
51
- // feature to be consistent with other layers.
52
- features = mbMap
53
- .queryRenderedFeatures(pixels, layer.queryRenderedFeaturesOptions || {})
54
- .map((feature) => {
55
- const olFeature = formats[projection].readFeature(feature);
56
- if (olFeature) {
57
- // We save the original Maplibre feature to avoid losing informations
58
- // potentially needed for other functionnality like highlighting
59
- // (id, layer id, source, sourceLayer ...)
60
- // @ts-ignore
61
- olFeature.set(VECTOR_TILE_FEATURE_PROPERTY, feature);
62
- }
63
- return olFeature;
64
- });
65
- }
66
- }
67
- return features;
68
- }
69
- // eslint-disable-next-line class-methods-use-this
70
- prepareFrame() {
71
- return true;
72
- }
73
- renderFrame(frameState) {
74
- const layer = this.getLayer();
75
- const { map, mbMap } = layer;
76
- if (!layer || !map || !mbMap) {
77
- return null;
78
- }
79
- const canvas = mbMap.getCanvas();
80
- const { viewState } = frameState;
81
- const opacity = layer.getOpacity() || 1;
82
- canvas.style.opacity = `${opacity}`;
83
- // adjust view parameters in Maplibre
84
- mbMap.jumpTo({
85
- center: toLonLat(viewState.center),
86
- zoom: viewState.zoom - 1,
87
- bearing: toDegrees(-viewState.rotation),
88
- });
89
- if (!canvas.isConnected) {
90
- // The canvas is not connected to the DOM, request a map rendering at the next animation frame
91
- // to set the canvas size.
92
- map.render();
93
- }
94
- else if (canvas.width !== frameState.size[0] ||
95
- canvas.height !== frameState.size[1]) {
96
- mbMap.resize();
97
- }
98
- mbMap.redraw();
99
- return mbMap.getContainer();
100
- }
101
- getFeatures(pixel) {
102
- var _a, _b;
103
- const coordinate = (_b = (_a = this.getLayer()) === null || _a === void 0 ? void 0 : _a.getMapInternal()) === null || _b === void 0 ? void 0 : _b.getCoordinateFromPixel(pixel);
104
- return Promise.resolve(this.getFeaturesAtCoordinate(coordinate));
105
- }
106
- forEachFeatureAtCoordinate(coordinate, frameState, hitTolerance, callback) {
107
- const features = this.getFeaturesAtCoordinate(coordinate, hitTolerance);
108
- features.forEach((feature) => {
109
- // @ts-ignore
110
- callback(feature, this.layer_, feature.getGeometry());
111
- });
112
- return features === null || features === void 0 ? void 0 : features[0];
113
- }
114
- }
1
+ "use strict";
2
+ // /* eslint-disable no-underscore-dangle */
3
+ // import { FrameState } from 'ol/Map';
4
+ // import { toDegrees } from 'ol/math';
5
+ // import { toLonLat } from 'ol/proj';
6
+ // import LayerRenderer from 'ol/renderer/Layer';
7
+ // import GeoJSON from 'ol/format/GeoJSON';
8
+ // import { Coordinate } from 'ol/coordinate';
9
+ // import { FeatureCallback } from 'ol/renderer/vector';
10
+ // import { Feature } from 'ol';
11
+ // import { Geometry } from 'ol/geom';
12
+ // import { Pixel } from 'ol/pixel';
13
+ // import { Map } from 'maplibre-gl';
14
+ // import { VECTOR_TILE_FEATURE_PROPERTY } from '../../common';
15
+ // import type MaplibreLayer from '../layers/MaplibreLayer';
16
+ // /**
17
+ // * @private
18
+ // */
19
+ // const formats: {
20
+ // [key: string]: GeoJSON;
21
+ // } = {
22
+ // 'EPSG:3857': new GeoJSON({
23
+ // featureProjection: 'EPSG:3857',
24
+ // }),
25
+ // };
26
+ // /**
27
+ // * This class is a renderer for Maplibre Layer to be able to use the native ol
28
+ // * functionnalities like map.getFeaturesAtPixel or map.hasFeatureAtPixel.
29
+ // * @private
30
+ // */
31
+ // // @ts-ignore
32
+ // export default class MaplibreLayerRenderer extends LayerRenderer<MaplibreLayer> {
33
+ // getFeaturesAtCoordinate(
34
+ // coordinate: Coordinate | undefined,
35
+ // hitTolerance: number = 5,
36
+ // ): Feature<Geometry>[] {
37
+ // if (!coordinate) {
38
+ // return [];
39
+ // }
40
+ // const layer = this.getLayer();
41
+ // const map = layer.getMapInternal();
42
+ // const { mbMap } = layer;
43
+ // const projection =
44
+ // map?.getView()?.getProjection()?.getCode() || 'EPSG:3857';
45
+ // let features: Feature[] = [];
46
+ // if (!formats[projection]) {
47
+ // formats[projection] = new GeoJSON({
48
+ // featureProjection: projection,
49
+ // });
50
+ // }
51
+ // if (mbMap?.isStyleLoaded()) {
52
+ // const pixel =
53
+ // coordinate && mbMap.project(toLonLat(coordinate) as [number, number]);
54
+ // if (pixel?.x && pixel?.y) {
55
+ // let pixels: [[number, number], [number, number]] | [number, number] = [
56
+ // pixel.x,
57
+ // pixel.y,
58
+ // ];
59
+ // if (hitTolerance) {
60
+ // const [x, y] = pixels as [number, number];
61
+ // pixels = [
62
+ // [x - hitTolerance, y - hitTolerance],
63
+ // [x + hitTolerance, y + hitTolerance],
64
+ // ];
65
+ // }
66
+ // // At this point we get GeoJSON Maplibre feature, we transform it to an OpenLayers
67
+ // // feature to be consistent with other layers.
68
+ // features = (mbMap as Map)
69
+ // .queryRenderedFeatures(
70
+ // pixels,
71
+ // layer.queryRenderedFeaturesOptions || {},
72
+ // )
73
+ // .map((feature) => {
74
+ // const olFeature = formats[projection].readFeature(
75
+ // feature,
76
+ // ) as Feature;
77
+ // if (olFeature) {
78
+ // // We save the original Maplibre feature to avoid losing informations
79
+ // // potentially needed for other functionnality like highlighting
80
+ // // (id, layer id, source, sourceLayer ...)
81
+ // // @ts-ignore
82
+ // olFeature.set(VECTOR_TILE_FEATURE_PROPERTY, feature);
83
+ // }
84
+ // return olFeature;
85
+ // });
86
+ // }
87
+ // }
88
+ // return features;
89
+ // }
90
+ // // eslint-disable-next-line class-methods-use-this
91
+ // override prepareFrame() {
92
+ // return true;
93
+ // }
94
+ // override renderFrame(frameState: FrameState) {
95
+ // const layer = this.getLayer();
96
+ // const { map, mbMap } = layer;
97
+ // if (!layer || !map || !mbMap) {
98
+ // return null;
99
+ // }
100
+ // const canvas = mbMap.getCanvas();
101
+ // const { viewState } = frameState;
102
+ // const opacity = layer.getOpacity() || 1;
103
+ // canvas.style.opacity = `${opacity}`;
104
+ // // adjust view parameters in Maplibre
105
+ // mbMap.jumpTo({
106
+ // center: toLonLat(viewState.center) as [number, number],
107
+ // zoom: viewState.zoom - 1,
108
+ // bearing: toDegrees(-viewState.rotation),
109
+ // });
110
+ // if (!canvas.isConnected) {
111
+ // // The canvas is not connected to the DOM, request a map rendering at the next animation frame
112
+ // // to set the canvas size.
113
+ // map.render();
114
+ // } else if (
115
+ // canvas.width !== frameState.size[0] ||
116
+ // canvas.height !== frameState.size[1]
117
+ // ) {
118
+ // mbMap.resize();
119
+ // }
120
+ // mbMap.redraw();
121
+ // return mbMap.getContainer();
122
+ // }
123
+ // override getFeatures(pixel: Pixel) {
124
+ // const coordinate = this.getLayer()
125
+ // ?.getMapInternal()
126
+ // ?.getCoordinateFromPixel(pixel);
127
+ // return Promise.resolve(this.getFeaturesAtCoordinate(coordinate));
128
+ // }
129
+ // override forEachFeatureAtCoordinate<Feature>(
130
+ // coordinate: Coordinate,
131
+ // frameState: FrameState,
132
+ // hitTolerance: number,
133
+ // callback: FeatureCallback<Feature>,
134
+ // ): Feature | undefined {
135
+ // const features = this.getFeaturesAtCoordinate(coordinate, hitTolerance);
136
+ // features.forEach((feature) => {
137
+ // // @ts-ignore
138
+ // callback(feature, this.layer_, feature.getGeometry());
139
+ // });
140
+ // return features?.[0] as Feature;
141
+ // }
142
+ // }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "mobility-toolbox-js",
3
3
  "license": "MIT",
4
4
  "description": "Toolbox for JavaScript applications in the domains of mobility and logistics.",
5
- "version": "3.0.0-beta.13",
5
+ "version": "3.0.0-beta.15",
6
6
  "homepage": "https://mobility-toolbox-js.geops.io/",
7
7
  "exports": {
8
8
  ".": "./index.js",
@@ -12,6 +12,7 @@
12
12
  "./types": "./types/index.d.ts"
13
13
  },
14
14
  "dependencies": {
15
+ "@geoblocks/ol-maplibre-layer": "^1.0.0",
15
16
  "@turf/helpers": "6.5.0",
16
17
  "@turf/transform-rotate": "6.5.0",
17
18
  "lodash.debounce": "4.0.8",