@geospatial-sdk/openlayers 0.0.3-alpha.0
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.
- package/LICENSE +28 -0
- package/README.md +11 -0
- package/index.ts +2 -0
- package/lib/map/create-map.test.ts +311 -0
- package/lib/map/create-map.ts +148 -0
- package/lib/map/index.ts +2 -0
- package/lib/map/styles.test.ts +217 -0
- package/lib/map/styles.ts +103 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Camptocamp
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
package/index.ts
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { FeatureCollection } from "geojson";
|
|
2
|
+
import TileLayer from "ol/layer/Tile";
|
|
3
|
+
import VectorLayer from "ol/layer/Vector";
|
|
4
|
+
import Map from "ol/Map";
|
|
5
|
+
import TileWMS from "ol/source/TileWMS";
|
|
6
|
+
import VectorSource from "ol/source/Vector";
|
|
7
|
+
import XYZ from "ol/source/XYZ";
|
|
8
|
+
import View from "ol/View";
|
|
9
|
+
import GeoJSON from "ol/format/GeoJSON";
|
|
10
|
+
import {
|
|
11
|
+
MAP_CTX_EXTENT_FIXTURE,
|
|
12
|
+
MAP_CTX_FIXTURE,
|
|
13
|
+
MAP_CTX_LAYER_GEOJSON_FIXTURE,
|
|
14
|
+
MAP_CTX_LAYER_GEOJSON_REMOTE_FIXTURE,
|
|
15
|
+
MAP_CTX_LAYER_WFS_FIXTURE,
|
|
16
|
+
MAP_CTX_LAYER_WMS_FIXTURE,
|
|
17
|
+
MAP_CTX_LAYER_XYZ_FIXTURE,
|
|
18
|
+
} from "@geospatial-sdk/core/fixtures/map-context.fixtures";
|
|
19
|
+
import {
|
|
20
|
+
MapContext,
|
|
21
|
+
MapContextLayer,
|
|
22
|
+
MapContextLayerGeojson,
|
|
23
|
+
MapContextLayerWms,
|
|
24
|
+
} from "@geospatial-sdk/core";
|
|
25
|
+
import Layer from "ol/layer/Layer";
|
|
26
|
+
import {
|
|
27
|
+
createLayer,
|
|
28
|
+
createMapFromContext,
|
|
29
|
+
createView,
|
|
30
|
+
resetMapFromContext,
|
|
31
|
+
} from "./create-map";
|
|
32
|
+
|
|
33
|
+
describe("MapContextService", () => {
|
|
34
|
+
describe("#createLayer", () => {
|
|
35
|
+
let layerModel: MapContextLayer, layer: Layer;
|
|
36
|
+
|
|
37
|
+
describe("XYZ", () => {
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
layerModel = MAP_CTX_LAYER_XYZ_FIXTURE;
|
|
40
|
+
layer = createLayer(layerModel);
|
|
41
|
+
});
|
|
42
|
+
it("create a tile layer", () => {
|
|
43
|
+
expect(layer).toBeTruthy();
|
|
44
|
+
expect(layer).toBeInstanceOf(TileLayer);
|
|
45
|
+
});
|
|
46
|
+
it("create a XYZ source", () => {
|
|
47
|
+
const source = layer.getSource();
|
|
48
|
+
expect(source).toBeInstanceOf(XYZ);
|
|
49
|
+
});
|
|
50
|
+
it("set correct urls", () => {
|
|
51
|
+
const source = layer.getSource() as XYZ;
|
|
52
|
+
const urls = source.getUrls() ?? [];
|
|
53
|
+
expect(urls.length).toBe(3);
|
|
54
|
+
expect(urls[0]).toEqual(
|
|
55
|
+
"https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("WMS", () => {
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
(layerModel = MAP_CTX_LAYER_WMS_FIXTURE),
|
|
63
|
+
(layer = createLayer(layerModel));
|
|
64
|
+
});
|
|
65
|
+
it("create a tile layer", () => {
|
|
66
|
+
expect(layer).toBeTruthy();
|
|
67
|
+
expect(layer).toBeInstanceOf(TileLayer);
|
|
68
|
+
});
|
|
69
|
+
it("create a TileWMS source", () => {
|
|
70
|
+
const source = layer.getSource();
|
|
71
|
+
expect(source).toBeInstanceOf(TileWMS);
|
|
72
|
+
});
|
|
73
|
+
it("set correct WMS params", () => {
|
|
74
|
+
const source = layer.getSource() as TileWMS;
|
|
75
|
+
const params = source.getParams();
|
|
76
|
+
expect(params.LAYERS).toBe((layerModel as MapContextLayerWms).name);
|
|
77
|
+
});
|
|
78
|
+
it("set correct url without existing REQUEST and SERVICE params", () => {
|
|
79
|
+
const source = layer.getSource() as TileWMS;
|
|
80
|
+
const urls = source.getUrls() || [];
|
|
81
|
+
expect(urls.length).toBe(1);
|
|
82
|
+
expect(urls[0]).toBe(
|
|
83
|
+
"https://www.geograndest.fr/geoserver/region-grand-est/ows",
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
it("set WMS gutter of 20px", () => {
|
|
87
|
+
const source = layer.getSource() as TileWMS;
|
|
88
|
+
const gutter = source["gutter_"];
|
|
89
|
+
expect(gutter).toBe(20);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("WFS", () => {
|
|
94
|
+
beforeEach(() => {
|
|
95
|
+
(layerModel = MAP_CTX_LAYER_WFS_FIXTURE),
|
|
96
|
+
(layer = createLayer(layerModel));
|
|
97
|
+
});
|
|
98
|
+
it("create a vector layer", () => {
|
|
99
|
+
expect(layer).toBeTruthy();
|
|
100
|
+
expect(layer).toBeInstanceOf(VectorLayer);
|
|
101
|
+
});
|
|
102
|
+
it("create a Vector source", () => {
|
|
103
|
+
const source = layer.getSource();
|
|
104
|
+
expect(source).toBeInstanceOf(VectorSource);
|
|
105
|
+
});
|
|
106
|
+
it("set correct url load function", () => {
|
|
107
|
+
const source = layer.getSource() as VectorSource;
|
|
108
|
+
const urlLoader = source.getUrl() as Function;
|
|
109
|
+
expect(urlLoader([10, 20, 30, 40])).toBe(
|
|
110
|
+
"https://www.geograndest.fr/geoserver/region-grand-est/ows?service=WFS&version=1.1.0&request=GetFeature&outputFormat=application%2Fjson&typename=ms%3Acommune_actuelle_3857&srsname=EPSG%3A3857&bbox=10%2C20%2C30%2C40%2CEPSG%3A3857",
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("GEOJSON", () => {
|
|
116
|
+
describe("with inline data", () => {
|
|
117
|
+
beforeEach(() => {
|
|
118
|
+
layerModel = MAP_CTX_LAYER_GEOJSON_FIXTURE;
|
|
119
|
+
layer = createLayer(layerModel);
|
|
120
|
+
});
|
|
121
|
+
it("create a VectorLayer", () => {
|
|
122
|
+
expect(layer).toBeTruthy();
|
|
123
|
+
expect(layer).toBeInstanceOf(VectorLayer);
|
|
124
|
+
});
|
|
125
|
+
it("create a VectorSource source", () => {
|
|
126
|
+
const source = layer.getSource();
|
|
127
|
+
expect(source).toBeInstanceOf(VectorSource);
|
|
128
|
+
});
|
|
129
|
+
it("add features", () => {
|
|
130
|
+
const source = layer.getSource() as VectorSource;
|
|
131
|
+
const features = source.getFeatures();
|
|
132
|
+
const data = (layerModel as MapContextLayerGeojson)
|
|
133
|
+
.data as FeatureCollection;
|
|
134
|
+
expect(features.length).toBe(data.features.length);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe("with inline data as string", () => {
|
|
138
|
+
beforeEach(() => {
|
|
139
|
+
layerModel = { ...MAP_CTX_LAYER_GEOJSON_FIXTURE };
|
|
140
|
+
layerModel.data = JSON.stringify(layerModel.data);
|
|
141
|
+
layer = createLayer(layerModel);
|
|
142
|
+
});
|
|
143
|
+
it("create a VectorLayer", () => {
|
|
144
|
+
expect(layer).toBeTruthy();
|
|
145
|
+
expect(layer).toBeInstanceOf(VectorLayer);
|
|
146
|
+
});
|
|
147
|
+
it("create a VectorSource source", () => {
|
|
148
|
+
const source = layer.getSource();
|
|
149
|
+
expect(source).toBeInstanceOf(VectorSource);
|
|
150
|
+
});
|
|
151
|
+
it("add features", () => {
|
|
152
|
+
const source = layer.getSource() as VectorSource;
|
|
153
|
+
const features = source.getFeatures();
|
|
154
|
+
expect(features.length).toBe(
|
|
155
|
+
(MAP_CTX_LAYER_GEOJSON_FIXTURE.data as FeatureCollection).features
|
|
156
|
+
.length,
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
describe("with invalid inline data as string", () => {
|
|
161
|
+
beforeEach(() => {
|
|
162
|
+
const spy = vi.spyOn(window.console, "warn");
|
|
163
|
+
spy.mockClear();
|
|
164
|
+
layerModel = {
|
|
165
|
+
...MAP_CTX_LAYER_GEOJSON_FIXTURE,
|
|
166
|
+
url: undefined,
|
|
167
|
+
data: "blargz",
|
|
168
|
+
};
|
|
169
|
+
layer = createLayer(layerModel);
|
|
170
|
+
});
|
|
171
|
+
it("create a VectorLayer", () => {
|
|
172
|
+
expect(layer).toBeTruthy();
|
|
173
|
+
expect(layer).toBeInstanceOf(VectorLayer);
|
|
174
|
+
});
|
|
175
|
+
it("outputs error in the console", () => {
|
|
176
|
+
expect(window.console.warn).toHaveBeenCalled();
|
|
177
|
+
});
|
|
178
|
+
it("create an empty VectorSource source", () => {
|
|
179
|
+
const source = layer.getSource() as VectorSource;
|
|
180
|
+
expect(source).toBeInstanceOf(VectorSource);
|
|
181
|
+
expect(source.getFeatures().length).toBe(0);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
describe("with remote file url", () => {
|
|
185
|
+
beforeEach(() => {
|
|
186
|
+
layerModel = MAP_CTX_LAYER_GEOJSON_REMOTE_FIXTURE;
|
|
187
|
+
layer = createLayer(layerModel);
|
|
188
|
+
});
|
|
189
|
+
it("create a VectorLayer", () => {
|
|
190
|
+
expect(layer).toBeTruthy();
|
|
191
|
+
expect(layer).toBeInstanceOf(VectorLayer);
|
|
192
|
+
});
|
|
193
|
+
it("create a VectorSource source", () => {
|
|
194
|
+
const source = layer.getSource();
|
|
195
|
+
expect(source).toBeInstanceOf(VectorSource);
|
|
196
|
+
});
|
|
197
|
+
it("sets the format as GeoJSON", () => {
|
|
198
|
+
const source = layer.getSource() as VectorSource;
|
|
199
|
+
expect(source.getFormat()).toBeInstanceOf(GeoJSON);
|
|
200
|
+
});
|
|
201
|
+
it("set the url to point to the file", () => {
|
|
202
|
+
const source = layer.getSource() as VectorSource;
|
|
203
|
+
expect(source.getUrl()).toBe(layerModel.url);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe("#createView", () => {
|
|
210
|
+
let view: View;
|
|
211
|
+
let map: Map;
|
|
212
|
+
describe("from center and zoom", () => {
|
|
213
|
+
const contextModel = MAP_CTX_FIXTURE;
|
|
214
|
+
beforeEach(() => {
|
|
215
|
+
map = createMapFromContext(contextModel);
|
|
216
|
+
view = createView(contextModel.view, map);
|
|
217
|
+
});
|
|
218
|
+
it("create a view", () => {
|
|
219
|
+
expect(view).toBeTruthy();
|
|
220
|
+
expect(view).toBeInstanceOf(View);
|
|
221
|
+
});
|
|
222
|
+
it("set center", () => {
|
|
223
|
+
const center = view.getCenter();
|
|
224
|
+
expect(center).toEqual([862726.0536478702, 6207260.308175252]);
|
|
225
|
+
});
|
|
226
|
+
it("set zoom", () => {
|
|
227
|
+
const zoom = view.getZoom();
|
|
228
|
+
expect(zoom).toEqual(contextModel.view.zoom);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
describe("from extent", () => {
|
|
232
|
+
const contextModel = {
|
|
233
|
+
...MAP_CTX_FIXTURE,
|
|
234
|
+
view: { ...MAP_CTX_FIXTURE.view, extent: MAP_CTX_EXTENT_FIXTURE },
|
|
235
|
+
};
|
|
236
|
+
const map = new Map({});
|
|
237
|
+
map.setSize([100, 100]);
|
|
238
|
+
beforeEach(() => {
|
|
239
|
+
view = createView(contextModel.view, map);
|
|
240
|
+
});
|
|
241
|
+
it("create a view", () => {
|
|
242
|
+
expect(view).toBeTruthy();
|
|
243
|
+
expect(view).toBeInstanceOf(View);
|
|
244
|
+
});
|
|
245
|
+
it("set center", () => {
|
|
246
|
+
const center = view.getCenter();
|
|
247
|
+
expect(center).toEqual([324027.04834895337, 6438563.654151043]);
|
|
248
|
+
});
|
|
249
|
+
it("set zoom", () => {
|
|
250
|
+
const zoom = view.getZoom();
|
|
251
|
+
expect(zoom).toEqual(5);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
describe("#resetMapFromContext", () => {
|
|
256
|
+
const map = new Map({});
|
|
257
|
+
const mapContext = MAP_CTX_FIXTURE;
|
|
258
|
+
beforeEach(() => {
|
|
259
|
+
resetMapFromContext(map, mapContext);
|
|
260
|
+
});
|
|
261
|
+
it("create a map", () => {
|
|
262
|
+
expect(map).toBeTruthy();
|
|
263
|
+
expect(map).toBeInstanceOf(Map);
|
|
264
|
+
});
|
|
265
|
+
it("add layers", () => {
|
|
266
|
+
const layers = map.getLayers().getArray();
|
|
267
|
+
expect(layers.length).toEqual(3);
|
|
268
|
+
});
|
|
269
|
+
it("set view", () => {
|
|
270
|
+
const view = map.getView();
|
|
271
|
+
expect(view).toBeTruthy();
|
|
272
|
+
expect(view).toBeInstanceOf(View);
|
|
273
|
+
});
|
|
274
|
+
describe("uses default fallback view", () => {
|
|
275
|
+
let view: View;
|
|
276
|
+
const map = new Map({});
|
|
277
|
+
const mapContext: MapContext = {
|
|
278
|
+
view: {},
|
|
279
|
+
layers: [
|
|
280
|
+
MAP_CTX_LAYER_XYZ_FIXTURE,
|
|
281
|
+
MAP_CTX_LAYER_WMS_FIXTURE,
|
|
282
|
+
MAP_CTX_LAYER_GEOJSON_FIXTURE,
|
|
283
|
+
],
|
|
284
|
+
};
|
|
285
|
+
beforeEach(() => {
|
|
286
|
+
resetMapFromContext(map, mapContext);
|
|
287
|
+
});
|
|
288
|
+
it("create a map", () => {
|
|
289
|
+
expect(map).toBeTruthy();
|
|
290
|
+
expect(map).toBeInstanceOf(Map);
|
|
291
|
+
});
|
|
292
|
+
it("add layers", () => {
|
|
293
|
+
const layers = map.getLayers().getArray();
|
|
294
|
+
expect(layers.length).toEqual(3);
|
|
295
|
+
});
|
|
296
|
+
it("set view", () => {
|
|
297
|
+
view = map.getView();
|
|
298
|
+
expect(view).toBeTruthy();
|
|
299
|
+
expect(view).toBeInstanceOf(View);
|
|
300
|
+
});
|
|
301
|
+
it("set center", () => {
|
|
302
|
+
const center = view.getCenter();
|
|
303
|
+
expect(center).toEqual([0, 0]);
|
|
304
|
+
});
|
|
305
|
+
it("set zoom", () => {
|
|
306
|
+
const zoom = view.getZoom();
|
|
307
|
+
expect(zoom).toEqual(0);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MapContext,
|
|
3
|
+
MapContextLayer,
|
|
4
|
+
MapContextView,
|
|
5
|
+
} from "@geospatial-sdk/core/lib/model";
|
|
6
|
+
import Map from "ol/Map";
|
|
7
|
+
import View from "ol/View";
|
|
8
|
+
import Layer from "ol/layer/Layer";
|
|
9
|
+
import TileLayer from "ol/layer/Tile";
|
|
10
|
+
import XYZ from "ol/source/XYZ";
|
|
11
|
+
import TileWMS from "ol/source/TileWMS";
|
|
12
|
+
import VectorLayer from "ol/layer/Vector";
|
|
13
|
+
import VectorSource from "ol/source/Vector";
|
|
14
|
+
import GeoJSON from "ol/format/GeoJSON";
|
|
15
|
+
import Feature from "ol/Feature";
|
|
16
|
+
import Geometry from "ol/geom/Geometry";
|
|
17
|
+
import { fromLonLat } from "ol/proj";
|
|
18
|
+
import { bbox as bboxStrategy } from "ol/loadingstrategy";
|
|
19
|
+
import { removeSearchParams } from "@geospatial-sdk/core/lib/utils";
|
|
20
|
+
import { defaultStyle } from "./styles";
|
|
21
|
+
|
|
22
|
+
const geosjonFormat = new GeoJSON();
|
|
23
|
+
|
|
24
|
+
export function createLayer(layerModel: MapContextLayer): Layer {
|
|
25
|
+
const { type } = layerModel;
|
|
26
|
+
const style = defaultStyle;
|
|
27
|
+
switch (type) {
|
|
28
|
+
case "xyz":
|
|
29
|
+
return new TileLayer({
|
|
30
|
+
source: new XYZ({
|
|
31
|
+
url: layerModel.url,
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
case "wms":
|
|
35
|
+
return new TileLayer({
|
|
36
|
+
source: new TileWMS({
|
|
37
|
+
url: removeSearchParams(layerModel.url, ["request", "service"]),
|
|
38
|
+
params: { LAYERS: layerModel.name },
|
|
39
|
+
gutter: 20,
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
// TODO: implement when ogc-client can handle wmts
|
|
43
|
+
// case 'wmts':
|
|
44
|
+
// return new TileLayer({
|
|
45
|
+
// source: new WMTS(layerModel.options),
|
|
46
|
+
// })
|
|
47
|
+
case "wfs":
|
|
48
|
+
return new VectorLayer({
|
|
49
|
+
source: new VectorSource({
|
|
50
|
+
format: new GeoJSON(),
|
|
51
|
+
url: function (extent) {
|
|
52
|
+
const urlObj = new URL(
|
|
53
|
+
removeSearchParams(layerModel.url, [
|
|
54
|
+
"service",
|
|
55
|
+
"version",
|
|
56
|
+
"request",
|
|
57
|
+
]),
|
|
58
|
+
);
|
|
59
|
+
urlObj.searchParams.set("service", "WFS");
|
|
60
|
+
urlObj.searchParams.set("version", "1.1.0");
|
|
61
|
+
urlObj.searchParams.set("request", "GetFeature");
|
|
62
|
+
urlObj.searchParams.set("outputFormat", "application/json");
|
|
63
|
+
urlObj.searchParams.set("typename", layerModel.featureType);
|
|
64
|
+
urlObj.searchParams.set("srsname", "EPSG:3857");
|
|
65
|
+
urlObj.searchParams.set("bbox", `${extent.join(",")},EPSG:3857`);
|
|
66
|
+
return urlObj.toString();
|
|
67
|
+
},
|
|
68
|
+
strategy: bboxStrategy,
|
|
69
|
+
}),
|
|
70
|
+
style,
|
|
71
|
+
});
|
|
72
|
+
case "geojson": {
|
|
73
|
+
if (layerModel.url !== undefined) {
|
|
74
|
+
return new VectorLayer({
|
|
75
|
+
source: new VectorSource({
|
|
76
|
+
format: new GeoJSON(),
|
|
77
|
+
url: layerModel.url,
|
|
78
|
+
}),
|
|
79
|
+
style,
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
let geojson = layerModel.data;
|
|
83
|
+
if (typeof geojson === "string") {
|
|
84
|
+
try {
|
|
85
|
+
geojson = JSON.parse(geojson);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
console.warn("A layer could not be created", layerModel, e);
|
|
88
|
+
geojson = { type: "FeatureCollection", features: [] };
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const features = geosjonFormat.readFeatures(geojson, {
|
|
92
|
+
featureProjection: "EPSG:3857",
|
|
93
|
+
dataProjection: "EPSG:4326",
|
|
94
|
+
}) as Feature<Geometry>[];
|
|
95
|
+
return new VectorLayer({
|
|
96
|
+
source: new VectorSource({
|
|
97
|
+
features,
|
|
98
|
+
}),
|
|
99
|
+
style,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
default:
|
|
104
|
+
throw new Error(`Unrecognized layer type: ${layerModel.type}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function createView(viewModel: MapContextView, map: Map): View {
|
|
109
|
+
const { center: centerInViewProj, zoom, maxZoom, maxExtent } = viewModel;
|
|
110
|
+
const center = centerInViewProj
|
|
111
|
+
? fromLonLat(centerInViewProj, "EPSG:3857")
|
|
112
|
+
: [0, 0];
|
|
113
|
+
const view = new View({
|
|
114
|
+
center,
|
|
115
|
+
zoom: zoom ?? 0,
|
|
116
|
+
maxZoom,
|
|
117
|
+
extent: maxExtent,
|
|
118
|
+
multiWorld: false,
|
|
119
|
+
constrainResolution: true,
|
|
120
|
+
});
|
|
121
|
+
if (viewModel.extent) {
|
|
122
|
+
view.fit(viewModel.extent, {
|
|
123
|
+
size: map.getSize(),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
return view;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Create an OpenLayers map from a context
|
|
131
|
+
* @param context
|
|
132
|
+
*/
|
|
133
|
+
export function createMapFromContext(context: MapContext): Map {
|
|
134
|
+
const map = new Map({});
|
|
135
|
+
return resetMapFromContext(map, context);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Resets an OpenLayers map from a context; existing content will be cleared
|
|
140
|
+
* @param map
|
|
141
|
+
* @param context
|
|
142
|
+
*/
|
|
143
|
+
export function resetMapFromContext(map: Map, context: MapContext): Map {
|
|
144
|
+
map.setView(createView(context.view, map));
|
|
145
|
+
map.getLayers().clear();
|
|
146
|
+
context.layers.forEach((layer) => map.addLayer(createLayer(layer)));
|
|
147
|
+
return map;
|
|
148
|
+
}
|
package/lib/map/index.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import chroma from "chroma-js";
|
|
2
|
+
import Style from "ol/style/Style";
|
|
3
|
+
import Feature from "ol/Feature";
|
|
4
|
+
import { LineString, Point, Polygon } from "ol/geom";
|
|
5
|
+
import {
|
|
6
|
+
createGeometryStyles,
|
|
7
|
+
createStyleFunction,
|
|
8
|
+
defaultHighlightStyle,
|
|
9
|
+
defaultStyle,
|
|
10
|
+
StyleByGeometryType,
|
|
11
|
+
} from "./styles";
|
|
12
|
+
import CircleStyle from "ol/style/Circle";
|
|
13
|
+
|
|
14
|
+
describe("MapStyleService", () => {
|
|
15
|
+
describe("#createGeometryStyles", () => {
|
|
16
|
+
let styles: StyleByGeometryType;
|
|
17
|
+
let pointStyle: Style;
|
|
18
|
+
let lineStyle: Style[];
|
|
19
|
+
let polygonStyle: Style;
|
|
20
|
+
|
|
21
|
+
describe("unfocused style", () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
const options = {
|
|
24
|
+
color: "orange",
|
|
25
|
+
};
|
|
26
|
+
styles = createGeometryStyles(options);
|
|
27
|
+
pointStyle = styles.point as Style;
|
|
28
|
+
lineStyle = styles.line as Style[];
|
|
29
|
+
polygonStyle = styles.polygon as Style;
|
|
30
|
+
});
|
|
31
|
+
describe("point style", () => {
|
|
32
|
+
it("has 1 style", () => {
|
|
33
|
+
expect(pointStyle).toBeInstanceOf(Style);
|
|
34
|
+
});
|
|
35
|
+
it("has correct radius", () => {
|
|
36
|
+
expect((pointStyle.getImage() as CircleStyle)?.getRadius()).toBe(7);
|
|
37
|
+
});
|
|
38
|
+
it("has correct fill color", () => {
|
|
39
|
+
expect(
|
|
40
|
+
(pointStyle.getImage() as CircleStyle)?.getFill()?.getColor(),
|
|
41
|
+
).toBe("orange");
|
|
42
|
+
});
|
|
43
|
+
it("has correct stroke color and width", () => {
|
|
44
|
+
expect(
|
|
45
|
+
(pointStyle.getImage() as CircleStyle)?.getStroke()?.getColor(),
|
|
46
|
+
).toBe("white");
|
|
47
|
+
expect(
|
|
48
|
+
(pointStyle.getImage() as CircleStyle)?.getStroke()?.getWidth(),
|
|
49
|
+
).toBe(2);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe("polygon style", () => {
|
|
53
|
+
it("has 1 style", () => {
|
|
54
|
+
expect(polygonStyle).toBeInstanceOf(Style);
|
|
55
|
+
});
|
|
56
|
+
it("has correct fill color", () => {
|
|
57
|
+
expect(polygonStyle.getFill()?.getColor()).toBe(
|
|
58
|
+
chroma("orange").alpha(0.25).css(),
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
it("has correct stroke color and width", () => {
|
|
62
|
+
expect(polygonStyle.getStroke()?.getColor()).toBe("white");
|
|
63
|
+
expect(polygonStyle.getStroke()?.getWidth()).toBe(2);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
describe("line style", () => {
|
|
67
|
+
it("has 2 styles", () => {
|
|
68
|
+
expect(lineStyle).toEqual([expect.any(Style), expect.any(Style)]);
|
|
69
|
+
});
|
|
70
|
+
it("has correct color (back stroke)", () => {
|
|
71
|
+
expect(lineStyle[0].getStroke()?.getColor()).toBe("white");
|
|
72
|
+
});
|
|
73
|
+
it("has correct width (back stroke)", () => {
|
|
74
|
+
expect(lineStyle[0].getStroke()?.getWidth()).toBe(6);
|
|
75
|
+
});
|
|
76
|
+
it("has correct color (front stroke)", () => {
|
|
77
|
+
expect(lineStyle[1].getStroke()?.getColor()).toBe("orange");
|
|
78
|
+
});
|
|
79
|
+
it("has correct width (front stroke)", () => {
|
|
80
|
+
expect(lineStyle[1].getStroke()?.getWidth()).toBe(2);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe("focused style", () => {
|
|
85
|
+
beforeEach(() => {
|
|
86
|
+
const options = {
|
|
87
|
+
color: "pink",
|
|
88
|
+
isFocused: true,
|
|
89
|
+
};
|
|
90
|
+
styles = createGeometryStyles(options);
|
|
91
|
+
pointStyle = styles.point as Style;
|
|
92
|
+
lineStyle = styles.line as Style[];
|
|
93
|
+
polygonStyle = styles.polygon as Style;
|
|
94
|
+
});
|
|
95
|
+
describe("point style", () => {
|
|
96
|
+
it("has correct radius", () => {
|
|
97
|
+
expect((pointStyle.getImage() as CircleStyle)?.getRadius()).toBe(8);
|
|
98
|
+
});
|
|
99
|
+
it("has correct fill color", () => {
|
|
100
|
+
expect(
|
|
101
|
+
(pointStyle.getImage() as CircleStyle)?.getFill()?.getColor(),
|
|
102
|
+
).toBe("pink");
|
|
103
|
+
});
|
|
104
|
+
it("has correct stroke color and width", () => {
|
|
105
|
+
expect(
|
|
106
|
+
(pointStyle.getImage() as CircleStyle)?.getStroke()?.getColor(),
|
|
107
|
+
).toBe("white");
|
|
108
|
+
expect(
|
|
109
|
+
(pointStyle.getImage() as CircleStyle)?.getStroke()?.getWidth(),
|
|
110
|
+
).toBe(3);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
describe("polygon style", () => {
|
|
114
|
+
it("has correct fill color", () => {
|
|
115
|
+
expect(polygonStyle.getFill()?.getColor()).toBe(
|
|
116
|
+
chroma("pink").alpha(0.25).css(),
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
it("has correct stroke color and width", () => {
|
|
120
|
+
expect(polygonStyle.getStroke()?.getColor()).toBe("white");
|
|
121
|
+
expect(polygonStyle.getStroke()?.getWidth()).toBe(2);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
describe("line style", () => {
|
|
125
|
+
it("has correct color (back stroke)", () => {
|
|
126
|
+
expect(lineStyle[0].getStroke()?.getColor()).toBe("white");
|
|
127
|
+
});
|
|
128
|
+
it("has correct width (back stroke)", () => {
|
|
129
|
+
expect(lineStyle[0].getStroke()?.getWidth()).toBe(8);
|
|
130
|
+
});
|
|
131
|
+
it("has correct color (front stroke)", () => {
|
|
132
|
+
expect(lineStyle[1].getStroke()?.getColor()).toBe("pink");
|
|
133
|
+
});
|
|
134
|
+
it("has correct width (front stroke)", () => {
|
|
135
|
+
expect(lineStyle[1].getStroke()?.getWidth()).toBe(3);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe("#createStyleFunction", () => {
|
|
142
|
+
let styleFn: Function;
|
|
143
|
+
let feature: Feature;
|
|
144
|
+
it("returns a function", () => {
|
|
145
|
+
styleFn = createStyleFunction(
|
|
146
|
+
createGeometryStyles({
|
|
147
|
+
color: "blue",
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
feature = new Feature();
|
|
151
|
+
});
|
|
152
|
+
describe("with linestring geometry", () => {
|
|
153
|
+
beforeEach(() => {
|
|
154
|
+
feature.setGeometry(new LineString([]));
|
|
155
|
+
});
|
|
156
|
+
it("resolves to a double style with stroke", () => {
|
|
157
|
+
const style = styleFn(feature, 1);
|
|
158
|
+
expect(style).toEqual([expect.any(Style), expect.any(Style)]);
|
|
159
|
+
expect(style[0].getStroke()).toBeTruthy();
|
|
160
|
+
expect(style[0].getFill()).toBeFalsy();
|
|
161
|
+
expect(style[0].getImage()).toBeFalsy();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
describe("with point geometry", () => {
|
|
165
|
+
beforeEach(() => {
|
|
166
|
+
feature.setGeometry(new Point([]));
|
|
167
|
+
});
|
|
168
|
+
it("resolves to a style with image", () => {
|
|
169
|
+
const style = styleFn(feature, 1);
|
|
170
|
+
expect(style.getImage()).toBeTruthy();
|
|
171
|
+
expect(style.getFill()).toBeFalsy();
|
|
172
|
+
expect(style.getStroke()).toBeFalsy();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
describe("with polygon geometry", () => {
|
|
176
|
+
beforeEach(() => {
|
|
177
|
+
feature.setGeometry(new Polygon([]));
|
|
178
|
+
});
|
|
179
|
+
it("resolves to a style with fill and stroke", () => {
|
|
180
|
+
const style = styleFn(feature, 1);
|
|
181
|
+
expect(style.getFill()).toBeTruthy();
|
|
182
|
+
expect(style.getStroke()).toBeTruthy();
|
|
183
|
+
expect(style.getImage()).toBeFalsy();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("built-in styles", () => {
|
|
189
|
+
let pointFeature: Feature;
|
|
190
|
+
let pointStyle: Style;
|
|
191
|
+
beforeEach(() => {
|
|
192
|
+
pointFeature = new Feature(new Point([]));
|
|
193
|
+
});
|
|
194
|
+
describe("default style", () => {
|
|
195
|
+
beforeEach(() => {
|
|
196
|
+
const styleFn = defaultStyle;
|
|
197
|
+
pointStyle = styleFn(pointFeature, 1) as Style;
|
|
198
|
+
});
|
|
199
|
+
it("uses the primary theme color", () => {
|
|
200
|
+
expect(
|
|
201
|
+
(pointStyle.getImage() as CircleStyle)?.getFill()?.getColor(),
|
|
202
|
+
).toEqual("blue");
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
describe("default highlight style", () => {
|
|
206
|
+
beforeEach(() => {
|
|
207
|
+
const styleFn = defaultHighlightStyle;
|
|
208
|
+
pointStyle = styleFn(pointFeature, 1) as Style;
|
|
209
|
+
});
|
|
210
|
+
it("uses the secondary theme color", () => {
|
|
211
|
+
expect(
|
|
212
|
+
(pointStyle.getImage() as CircleStyle)?.getFill()?.getColor(),
|
|
213
|
+
).toEqual("red");
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Circle, Fill, Stroke, Style } from "ol/style";
|
|
2
|
+
import { StyleFunction } from "ol/style/Style";
|
|
3
|
+
import { FeatureLike } from "ol/Feature";
|
|
4
|
+
import chroma from "chroma-js";
|
|
5
|
+
|
|
6
|
+
export interface CreateStyleOptions {
|
|
7
|
+
color: string;
|
|
8
|
+
isFocused?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface StyleByGeometryType {
|
|
12
|
+
line: Style | Style[];
|
|
13
|
+
polygon: Style | Style[];
|
|
14
|
+
point: Style | Style[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function createGeometryStyles(
|
|
18
|
+
options: CreateStyleOptions,
|
|
19
|
+
): StyleByGeometryType {
|
|
20
|
+
const { color, isFocused } = options;
|
|
21
|
+
const zIndex = isFocused ? 10 : undefined;
|
|
22
|
+
return {
|
|
23
|
+
polygon: new Style({
|
|
24
|
+
fill: new Fill({
|
|
25
|
+
color: computeTransparentFillColor(color),
|
|
26
|
+
}),
|
|
27
|
+
stroke: new Stroke({
|
|
28
|
+
color: "white",
|
|
29
|
+
width: 2,
|
|
30
|
+
}),
|
|
31
|
+
zIndex,
|
|
32
|
+
}),
|
|
33
|
+
point: new Style({
|
|
34
|
+
image: new Circle({
|
|
35
|
+
fill: new Fill({
|
|
36
|
+
color,
|
|
37
|
+
}),
|
|
38
|
+
stroke: new Stroke({
|
|
39
|
+
color: "white",
|
|
40
|
+
width: isFocused ? 3 : 2,
|
|
41
|
+
}),
|
|
42
|
+
radius: isFocused ? 8 : 7,
|
|
43
|
+
}),
|
|
44
|
+
zIndex,
|
|
45
|
+
}),
|
|
46
|
+
line: [
|
|
47
|
+
new Style({
|
|
48
|
+
stroke: new Stroke({
|
|
49
|
+
color: "white",
|
|
50
|
+
width: isFocused ? 8 : 6,
|
|
51
|
+
}),
|
|
52
|
+
zIndex,
|
|
53
|
+
}),
|
|
54
|
+
new Style({
|
|
55
|
+
stroke: new Stroke({
|
|
56
|
+
color,
|
|
57
|
+
width: isFocused ? 3 : 2,
|
|
58
|
+
}),
|
|
59
|
+
zIndex,
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function createStyleFunction(
|
|
66
|
+
styleByGeometryType: StyleByGeometryType,
|
|
67
|
+
): StyleFunction {
|
|
68
|
+
return (feature: FeatureLike): Style | Style[] => {
|
|
69
|
+
const geometryType = feature?.getGeometry()?.getType();
|
|
70
|
+
switch (geometryType) {
|
|
71
|
+
case "LinearRing":
|
|
72
|
+
case "LineString":
|
|
73
|
+
case "MultiLineString":
|
|
74
|
+
return styleByGeometryType.line;
|
|
75
|
+
case "Point":
|
|
76
|
+
case "MultiPoint":
|
|
77
|
+
return styleByGeometryType.point;
|
|
78
|
+
case "Circle":
|
|
79
|
+
case "Polygon":
|
|
80
|
+
case "MultiPolygon":
|
|
81
|
+
return styleByGeometryType.polygon;
|
|
82
|
+
default:
|
|
83
|
+
return styleByGeometryType.point;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function computeTransparentFillColor(color: string, alpha = 0.25): string {
|
|
89
|
+
return chroma(color).alpha(alpha).css();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export const defaultStyle = createStyleFunction(
|
|
93
|
+
createGeometryStyles({
|
|
94
|
+
color: "blue",
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
export const defaultHighlightStyle = createStyleFunction(
|
|
99
|
+
createGeometryStyles({
|
|
100
|
+
color: "red",
|
|
101
|
+
isFocused: true,
|
|
102
|
+
}),
|
|
103
|
+
);
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@geospatial-sdk/openlayers",
|
|
3
|
+
"version": "0.0.3-alpha.0+d226ce9",
|
|
4
|
+
"description": "OpenLayers-related utilities",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ol",
|
|
7
|
+
"openlayers",
|
|
8
|
+
"map"
|
|
9
|
+
],
|
|
10
|
+
"author": "Olivia <olivia.guyot@camptocamp.com>",
|
|
11
|
+
"homepage": "",
|
|
12
|
+
"license": "BSD-3-Clause",
|
|
13
|
+
"main": "index.ts",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"directories": {
|
|
19
|
+
"lib": "lib"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"lib"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "vitest"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/chroma-js": "^2.4.3",
|
|
29
|
+
"ol": "^8.2.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"ol": ">6.x"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"chroma-js": "^2.4.2"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "d226ce97fd7d50287c694ce4ed7182755cdfbb4e"
|
|
38
|
+
}
|