@geospatial-sdk/openlayers 0.0.5-dev.38 → 0.0.5-dev.40

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 +1 @@
1
- {"version":3,"file":"register-events.d.ts","sourceRoot":"","sources":["../../lib/map/register-events.ts"],"names":[],"mappings":"AAAA,OAAO,GAA4B,MAAM,QAAQ,CAAC;AAClD,OAAO,EAIL,eAAe,EAEhB,MAAM,sBAAsB,CAAC;AAM9B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,OAAO,MAAM,mBAAmB,CAAC;AACxC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,SAAS,CAAC;AAM1D,wBAAgB,4BAA4B,CAC1C,KAAK,EAAE,GAAG,EACV,KAAK,EAAE,KAAK,GACX,OAAO,EAAE,CAOX;AAED,wBAAgB,SAAS,CACvB,MAAM,EAAE,OAAO,GAAG,QAAQ,EAC1B,GAAG,EAAE,GAAG,EACR,UAAU,EAAE,UAAU,GACrB,MAAM,GAAG,IAAI,CAWf;AAED,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,GAAG,EACV,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,OAAO,EAAE,CAAC,CA2BpB;AAyCD,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,eAAe,EACpD,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAAI,QAmE9C"}
1
+ {"version":3,"file":"register-events.d.ts","sourceRoot":"","sources":["../../lib/map/register-events.ts"],"names":[],"mappings":"AAAA,OAAO,GAA4B,MAAM,QAAQ,CAAC;AAClD,OAAO,EAKL,eAAe,EAEhB,MAAM,sBAAsB,CAAC;AAM9B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,OAAO,MAAM,mBAAmB,CAAC;AACxC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAE1C,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,SAAS,CAAC;AAO1D,wBAAgB,4BAA4B,CAC1C,KAAK,EAAE,GAAG,EACV,KAAK,EAAE,KAAK,GACX,OAAO,EAAE,CAOX;AAED,wBAAgB,SAAS,CACvB,MAAM,EAAE,OAAO,GAAG,QAAQ,EAC1B,GAAG,EAAE,GAAG,EACR,UAAU,EAAE,UAAU,GACrB,MAAM,GAAG,IAAI,CAWf;AAED,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,GAAG,EACV,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,OAAO,EAAE,CAAC,CA2BpB;AA8ED,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,eAAe,EACpD,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAAI,QA0E9C"}
@@ -7,13 +7,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { FeaturesClickEventType, FeaturesHoverEventType, MapClickEventType, SourceLoadErrorType, } from "@geospatial-sdk/core";
11
- import { toLonLat } from "ol/proj";
10
+ import { FeaturesClickEventType, FeaturesHoverEventType, MapClickEventType, MapExtentChangeEventType, SourceLoadErrorType, } from "@geospatial-sdk/core";
11
+ import { toLonLat, transformExtent } from "ol/proj";
12
12
  import GeoJSON from "ol/format/GeoJSON";
13
13
  import TileWMS from "ol/source/TileWMS";
14
14
  import ImageWMS from "ol/source/ImageWMS";
15
15
  import Layer from "ol/layer/Layer";
16
16
  import throttle from "lodash.throttle";
17
+ import { equals } from "ol/extent";
17
18
  const GEOJSON = new GeoJSON();
18
19
  export function getFeaturesFromVectorSources(olMap, pixel) {
19
20
  const olFeatures = olMap.getFeaturesAtPixel(pixel);
@@ -83,6 +84,28 @@ function registerFeatureHoverEvent(map) {
83
84
  }));
84
85
  map.set(FeaturesHoverEventType, true);
85
86
  }
87
+ function registerMapExtentChangeEvent(map) {
88
+ if (map.get(MapExtentChangeEventType))
89
+ return;
90
+ let lastExtent = null;
91
+ const handleExtentChange = () => {
92
+ const extent = map.getView().calculateExtent(map.getSize());
93
+ const reprojectedExtent = transformExtent(extent, map.getView().getProjection(), "EPSG:4326");
94
+ if (lastExtent && equals(lastExtent, reprojectedExtent)) {
95
+ return;
96
+ }
97
+ lastExtent = reprojectedExtent;
98
+ map.dispatchEvent({
99
+ type: MapExtentChangeEventType,
100
+ extent: reprojectedExtent,
101
+ });
102
+ };
103
+ map.getView().on("change:center", handleExtentChange);
104
+ map.getView().on("change:resolution", handleExtentChange);
105
+ map.getView().on("change:rotation", handleExtentChange);
106
+ map.on("change:size", handleExtentChange);
107
+ map.set(MapExtentChangeEventType, true);
108
+ }
86
109
  export function listen(map, eventType, callback) {
87
110
  switch (eventType) {
88
111
  case FeaturesClickEventType:
@@ -108,6 +131,13 @@ export function listen(map, eventType, callback) {
108
131
  });
109
132
  });
110
133
  break;
134
+ case MapExtentChangeEventType:
135
+ registerMapExtentChangeEvent(map);
136
+ // see comment above
137
+ map.on(eventType, (event) => {
138
+ callback(event);
139
+ });
140
+ break;
111
141
  case SourceLoadErrorType: {
112
142
  const errorCallback = (event) => {
113
143
  callback(event);
@@ -16,6 +16,11 @@ import Point from "ol/geom/Point";
16
16
  import TileLayer from "ol/layer/Tile";
17
17
  import OlFeature from "ol/Feature";
18
18
 
19
+ const EXPECTED_MAP_EXTENT_EPSG4326 = [
20
+ -0.0035932611364780857, -0.0026949458513598756, 0.0035932611364780857,
21
+ 0.0026949458513740865,
22
+ ];
23
+
19
24
  const gfiResult = {
20
25
  type: "Feature",
21
26
  properties: {
@@ -57,6 +62,7 @@ function createMap(): Map {
57
62
  getEventPixel: { value: vi.fn(() => [10, 10]) },
58
63
  getCoordinateFromPixel: { value: vi.fn(() => [123, 123]) },
59
64
  getFeaturesAtPixel: { value: vi.fn(() => [feature]) },
65
+ getSize: { value: vi.fn(() => [800, 600]) },
60
66
  });
61
67
  return map;
62
68
  }
@@ -196,4 +202,106 @@ describe("event registration", () => {
196
202
  });
197
203
  });
198
204
  });
205
+ describe("map extent change event", () => {
206
+ let callback: Mock;
207
+
208
+ beforeEach(() => {
209
+ callback = vi.fn();
210
+ listen(map, "map-extent-change", callback);
211
+ });
212
+
213
+ it("should registers the event on the map when center changed", () => {
214
+ map.getView().dispatchEvent(createMapEvent(map, "change:center"));
215
+
216
+ expect(callback).toHaveBeenCalledOnce();
217
+ expect(callback).toHaveBeenCalledWith({
218
+ type: "map-extent-change",
219
+ extent: EXPECTED_MAP_EXTENT_EPSG4326,
220
+ target: expect.anything(),
221
+ });
222
+ });
223
+
224
+ it("should registers the event on the map when resolution changed", () => {
225
+ map.getView().dispatchEvent(createMapEvent(map, "change:resolution"));
226
+
227
+ expect(callback).toHaveBeenCalledOnce();
228
+ expect(callback).toHaveBeenCalledWith({
229
+ type: "map-extent-change",
230
+ extent: EXPECTED_MAP_EXTENT_EPSG4326,
231
+ target: expect.anything(),
232
+ });
233
+ });
234
+
235
+ it("should registers the event on the map when rotation changed", () => {
236
+ map.getView().dispatchEvent(createMapEvent(map, "change:rotation"));
237
+
238
+ expect(callback).toHaveBeenCalledOnce();
239
+ expect(callback).toHaveBeenCalledWith({
240
+ type: "map-extent-change",
241
+ extent: EXPECTED_MAP_EXTENT_EPSG4326,
242
+ target: expect.anything(),
243
+ });
244
+ });
245
+
246
+ it("should registers the event on the map when size changed", () => {
247
+ map.dispatchEvent(createMapEvent(map, "change:size"));
248
+
249
+ expect(callback).toHaveBeenCalledOnce();
250
+ expect(callback).toHaveBeenCalledWith({
251
+ type: "map-extent-change",
252
+ extent: EXPECTED_MAP_EXTENT_EPSG4326,
253
+ target: expect.anything(),
254
+ });
255
+ });
256
+
257
+ it("should send map-extent-change only once when multiple events occur with same extent", () => {
258
+ map.getView().dispatchEvent(createMapEvent(map, "change:center"));
259
+ map.getView().dispatchEvent(createMapEvent(map, "change:resolution"));
260
+ map.getView().dispatchEvent(createMapEvent(map, "change:rotation"));
261
+ map.dispatchEvent(createMapEvent(map, "change:size"));
262
+
263
+ expect(callback).toHaveBeenCalledOnce();
264
+ expect(callback).toHaveBeenCalledWith({
265
+ type: "map-extent-change",
266
+ extent: EXPECTED_MAP_EXTENT_EPSG4326,
267
+ target: expect.anything(),
268
+ });
269
+ });
270
+
271
+ it("should send map-extent-change twice when view properties actually change", () => {
272
+ map.getView().setCenter([1000, 1000]);
273
+ map.getView().dispatchEvent(createMapEvent(map, "change:center"));
274
+
275
+ map.getView().setResolution(2);
276
+ map.getView().dispatchEvent(createMapEvent(map, "change:resolution"));
277
+
278
+ expect(callback).toHaveBeenCalledTimes(2);
279
+ expect(callback).toHaveBeenCalledWith(
280
+ expect.objectContaining({
281
+ type: "map-extent-change",
282
+ extent: expect.any(Array),
283
+ }),
284
+ );
285
+
286
+ const firstCall = callback.mock.calls[0][0];
287
+ const lastCall = callback.mock.calls[1][0];
288
+ expect(firstCall.extent).not.toEqual(lastCall.extent);
289
+ });
290
+
291
+ it("should send map-extent-change only once when same view property is set multiple times to same value", () => {
292
+ map.getView().setCenter([1000, 1000]);
293
+ map.getView().dispatchEvent(createMapEvent(map, "change:center"));
294
+
295
+ map.getView().setCenter([1000, 1000]);
296
+ map.getView().dispatchEvent(createMapEvent(map, "change:center"));
297
+
298
+ expect(callback).toHaveBeenCalledTimes(1);
299
+ expect(callback).toHaveBeenCalledWith(
300
+ expect.objectContaining({
301
+ type: "map-extent-change",
302
+ extent: expect.any(Array),
303
+ }),
304
+ );
305
+ });
306
+ });
199
307
  });
@@ -3,10 +3,11 @@ import {
3
3
  FeaturesClickEventType,
4
4
  FeaturesHoverEventType,
5
5
  MapClickEventType,
6
+ MapExtentChangeEventType,
6
7
  MapEventsByType,
7
8
  SourceLoadErrorType,
8
9
  } from "@geospatial-sdk/core";
9
- import { toLonLat } from "ol/proj";
10
+ import { toLonLat, transformExtent } from "ol/proj";
10
11
  import GeoJSON from "ol/format/GeoJSON";
11
12
  import OlFeature from "ol/Feature";
12
13
  import BaseEvent from "ol/events/Event";
@@ -19,6 +20,7 @@ import { Pixel } from "ol/pixel";
19
20
  import type { Feature, FeatureCollection } from "geojson";
20
21
  import throttle from "lodash.throttle";
21
22
  import { BaseLayerObjectEventTypes } from "ol/layer/Base";
23
+ import { equals } from "ol/extent";
22
24
 
23
25
  const GEOJSON = new GeoJSON();
24
26
 
@@ -100,6 +102,7 @@ async function readFeaturesAtPixel(
100
102
 
101
103
  function registerFeatureClickEvent(map: Map) {
102
104
  if (map.get(FeaturesClickEventType)) return;
105
+
103
106
  map.on("click", async (event) => {
104
107
  const features = await readFeaturesAtPixel(map, event);
105
108
  map.dispatchEvent({
@@ -107,11 +110,13 @@ function registerFeatureClickEvent(map: Map) {
107
110
  features,
108
111
  } as unknown as BaseEvent);
109
112
  });
113
+
110
114
  map.set(FeaturesClickEventType, true);
111
115
  }
112
116
 
113
117
  function registerFeatureHoverEvent(map: Map) {
114
118
  if (map.get(FeaturesHoverEventType)) return;
119
+
115
120
  map.on("pointermove", async (event) => {
116
121
  const features = await readFeaturesAtPixel(map, event);
117
122
  map.dispatchEvent({
@@ -119,9 +124,43 @@ function registerFeatureHoverEvent(map: Map) {
119
124
  features,
120
125
  } as unknown as BaseEvent);
121
126
  });
127
+
122
128
  map.set(FeaturesHoverEventType, true);
123
129
  }
124
130
 
131
+ function registerMapExtentChangeEvent(map: Map) {
132
+ if (map.get(MapExtentChangeEventType)) return;
133
+
134
+ let lastExtent: number[] | null = null;
135
+
136
+ const handleExtentChange = () => {
137
+ const extent = map.getView().calculateExtent(map.getSize());
138
+ const reprojectedExtent = transformExtent(
139
+ extent,
140
+ map.getView().getProjection(),
141
+ "EPSG:4326",
142
+ );
143
+
144
+ if (lastExtent && equals(lastExtent, reprojectedExtent)) {
145
+ return;
146
+ }
147
+
148
+ lastExtent = reprojectedExtent;
149
+
150
+ map.dispatchEvent({
151
+ type: MapExtentChangeEventType,
152
+ extent: reprojectedExtent,
153
+ } as unknown as BaseEvent);
154
+ };
155
+
156
+ map.getView().on("change:center", handleExtentChange);
157
+ map.getView().on("change:resolution", handleExtentChange);
158
+ map.getView().on("change:rotation", handleExtentChange);
159
+ map.on("change:size", handleExtentChange);
160
+
161
+ map.set(MapExtentChangeEventType, true);
162
+ }
163
+
125
164
  export function listen<T extends keyof MapEventsByType>(
126
165
  map: Map,
127
166
  eventType: T,
@@ -154,6 +193,13 @@ export function listen<T extends keyof MapEventsByType>(
154
193
  });
155
194
  });
156
195
  break;
196
+ case MapExtentChangeEventType:
197
+ registerMapExtentChangeEvent(map);
198
+ // see comment above
199
+ map.on(eventType as unknown as MapObjectEventTypes, (event) => {
200
+ (callback as (event: unknown) => void)(event);
201
+ });
202
+ break;
157
203
  case SourceLoadErrorType: {
158
204
  const errorCallback = (event: BaseEvent) => {
159
205
  (callback as (event: unknown) => void)(event);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geospatial-sdk/openlayers",
3
- "version": "0.0.5-dev.38+fe1fda8",
3
+ "version": "0.0.5-dev.40+17bad88",
4
4
  "description": "OpenLayers-related utilities",
5
5
  "keywords": [
6
6
  "ol",
@@ -37,10 +37,10 @@
37
37
  "ol": ">6.x"
38
38
  },
39
39
  "dependencies": {
40
- "@geospatial-sdk/core": "^0.0.5-dev.38+fe1fda8",
40
+ "@geospatial-sdk/core": "^0.0.5-dev.40+17bad88",
41
41
  "chroma-js": "^2.4.2",
42
42
  "lodash.throttle": "^4.1.1",
43
43
  "ol-mapbox-style": "12.4.0"
44
44
  },
45
- "gitHead": "fe1fda8882b1113ac9a52059a992836dd87cf23e"
45
+ "gitHead": "17bad885319e693911c2219422bfe3fca4437fa2"
46
46
  }