@geolonia/embed 5.2.2 → 6.0.0-pre.2
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/.github/workflows/build.yml +25 -1
- package/CHANGELOG.md +9 -0
- package/README.md +24 -1
- package/dist/embed +1 -1
- package/dist/embed-chunks/4ada77b78cc0cc5ccef9.js +1 -0
- package/dist/embed-chunks/8176630945512b29f294.js +2 -0
- package/dist/embed-chunks/c783fffcd28083f7d5cb.js +2 -0
- package/dist/embed-chunks/f2124097fda51d0079ee.js +1 -0
- package/dist/embed-core.d.ts +6 -7
- package/dist/embed-core.js +1 -1
- package/dist/embed.d.ts +2 -4
- package/dist/embed.js +1 -1
- package/dist/lib/parse-atts.d.ts +19 -0
- package/dist/lib/render.d.ts +1 -1
- package/dist/lib/util.d.ts +14 -55
- package/dist/types.d.ts +155 -1
- package/dist/version.d.ts +1 -1
- package/e2e/auto-render.spec.ts +44 -0
- package/e2e/basic.spec.ts +10 -74
- package/e2e/cleanup.spec.ts +43 -0
- package/e2e/data-attributes.spec.ts +56 -0
- package/e2e/globals.spec.ts +51 -0
- package/e2e/helper.ts +170 -4
- package/e2e/iframe.spec.ts +35 -0
- package/e2e/lazy-loading.spec.ts +32 -0
- package/e2e/legacy-constructor.spec.ts +102 -0
- package/e2e/plugin.spec.ts +25 -0
- package/e2e/pmtiles.spec.ts +13 -0
- package/package.json +8 -11
- package/playwright.config.ts +6 -32
- package/src/embed-core.ts +12 -14
- package/src/embed.ts +2 -4
- package/src/lib/parse-atts.test.ts +186 -18
- package/src/lib/parse-atts.ts +151 -5
- package/src/lib/render.ts +50 -12
- package/src/lib/util.ts +37 -350
- package/src/types.ts +155 -1
- package/src/version.ts +1 -1
- package/typedoc.json +13 -0
- package/vitest.config.ts +7 -0
- package/dist/embed-chunks/12cbd0d1cb4cf744c32f.js +0 -2
- package/dist/embed-chunks/8a521e8312a5b4da123d.js +0 -2
- package/dist/lib/CustomAttributionControl.d.ts +0 -47
- package/dist/lib/controls/geolonia-control.d.ts +0 -7
- package/dist/lib/geolonia-map.d.ts +0 -30
- package/dist/lib/geolonia-marker.d.ts +0 -9
- package/dist/lib/keyring.d.ts +0 -45
- package/dist/lib/maplibre-util.d.ts +0 -25
- package/dist/lib/simplestyle-vector.d.ts +0 -26
- package/dist/lib/simplestyle.d.ts +0 -31
- package/e2e/external-style.spec.ts +0 -25
- package/e2e/map.spec.ts +0 -69
- package/src/lib/CustomAttributionControl.ts +0 -394
- package/src/lib/controls/geolonia-control.ts +0 -38
- package/src/lib/geolonia-map.ts +0 -429
- package/src/lib/geolonia-marker.ts +0 -53
- package/src/lib/keyring.test.ts +0 -127
- package/src/lib/keyring.ts +0 -136
- package/src/lib/maplibre-util.ts +0 -137
- package/src/lib/marker.svg +0 -1
- package/src/lib/simplestyle-vector.ts +0 -257
- package/src/lib/simplestyle.test.ts +0 -289
- package/src/lib/simplestyle.ts +0 -435
- package/src/lib/util.test.ts +0 -364
- /package/dist/embed-chunks/{12cbd0d1cb4cf744c32f.js.LICENSE.txt → 8176630945512b29f294.js.LICENSE.txt} +0 -0
- /package/dist/embed-chunks/{8a521e8312a5b4da123d.js.LICENSE.txt → c783fffcd28083f7d5cb.js.LICENSE.txt} +0 -0
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-loss-of-precision */
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
|
|
5
|
-
import { random } from './util';
|
|
6
|
-
|
|
7
|
-
const originalRequestAnimationFrame = window.requestAnimationFrame;
|
|
8
|
-
const originalCreateObjectURL = window.URL.createObjectURL;
|
|
9
|
-
|
|
10
|
-
beforeAll(() => {
|
|
11
|
-
window.URL.createObjectURL ||= (_: Blob | MediaSource) => 'dummy';
|
|
12
|
-
window.requestAnimationFrame = (cb) => {
|
|
13
|
-
cb(performance.now());
|
|
14
|
-
return random(999999);
|
|
15
|
-
};
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
afterAll(() => {
|
|
19
|
-
window.requestAnimationFrame = originalRequestAnimationFrame;
|
|
20
|
-
if (originalCreateObjectURL) {
|
|
21
|
-
window.URL.createObjectURL = originalCreateObjectURL;
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
const sampleRemoteGeoJSON = {
|
|
26
|
-
type: 'FeatureCollection',
|
|
27
|
-
features: [
|
|
28
|
-
{
|
|
29
|
-
type: 'Feature',
|
|
30
|
-
properties: {},
|
|
31
|
-
geometry: {
|
|
32
|
-
type: 'LineString',
|
|
33
|
-
coordinates: [
|
|
34
|
-
[139.6870422363281, 35.73425097869431],
|
|
35
|
-
[139.76943969726562, 35.73425097869431],
|
|
36
|
-
[139.73922729492188, 35.66399091134812],
|
|
37
|
-
[139.70352172851562, 35.698571062054015],
|
|
38
|
-
],
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
],
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
class Map {
|
|
45
|
-
public bounds = false;
|
|
46
|
-
public layers = [];
|
|
47
|
-
public sources = {};
|
|
48
|
-
|
|
49
|
-
constructor(
|
|
50
|
-
private json?,
|
|
51
|
-
private options?,
|
|
52
|
-
) {}
|
|
53
|
-
|
|
54
|
-
addSource(id, source) {
|
|
55
|
-
this.sources[id] = source;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
addLayer(layer) {
|
|
59
|
-
this.layers.push(layer);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
on() {}
|
|
63
|
-
|
|
64
|
-
getSource(id) {
|
|
65
|
-
class getSource {
|
|
66
|
-
constructor(
|
|
67
|
-
private id,
|
|
68
|
-
private sources,
|
|
69
|
-
) {}
|
|
70
|
-
setData(geojson) {
|
|
71
|
-
this.sources[this.id] = {
|
|
72
|
-
type: 'geojson',
|
|
73
|
-
data: geojson,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return new getSource(id, this.sources);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
getContainer() {
|
|
82
|
-
return { dataset: true };
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
fitBounds() {
|
|
86
|
-
this.bounds = true;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const geojson = {
|
|
91
|
-
type: 'FeatureCollection',
|
|
92
|
-
features: [
|
|
93
|
-
{
|
|
94
|
-
type: 'Feature',
|
|
95
|
-
properties: {},
|
|
96
|
-
geometry: {
|
|
97
|
-
type: 'Point',
|
|
98
|
-
coordinates: [139.77012634277344, 35.68518697509636],
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
],
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
describe('Tests for simpleStyle()', () => {
|
|
105
|
-
it('should has sources and layers as expected', async () => {
|
|
106
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
107
|
-
|
|
108
|
-
const map = new Map();
|
|
109
|
-
new SimpleStyle(geojson).addTo(map).fitBounds();
|
|
110
|
-
|
|
111
|
-
expect(Object.keys(map.sources)).toEqual(
|
|
112
|
-
['geolonia-simple-style', 'geolonia-simple-style-points'],
|
|
113
|
-
);
|
|
114
|
-
expect(map.layers.length).toEqual(8);
|
|
115
|
-
expect(map.bounds).toEqual(true);
|
|
116
|
-
expect(
|
|
117
|
-
map.layers.find(
|
|
118
|
-
(layer) => layer.id === 'geolonia-simple-style-symbol-points',
|
|
119
|
-
).layout['icon-allow-overlap'],
|
|
120
|
-
).toEqual(true);
|
|
121
|
-
expect(
|
|
122
|
-
map.layers.find(
|
|
123
|
-
(layer) => layer.id === 'geolonia-simple-style-symbol-points',
|
|
124
|
-
).layout['text-allow-overlap'],
|
|
125
|
-
).toEqual(true);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should has sources and layers as expected with custom IDs', async () => {
|
|
129
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
130
|
-
|
|
131
|
-
const map = new Map();
|
|
132
|
-
new SimpleStyle(geojson, { id: 'hello-world' }).addTo(map).fitBounds();
|
|
133
|
-
|
|
134
|
-
expect(Object.keys(map.sources)).toEqual(
|
|
135
|
-
['hello-world', 'hello-world-points'],
|
|
136
|
-
);
|
|
137
|
-
expect(map.layers.length).toEqual(8);
|
|
138
|
-
expect(map.bounds).toEqual(true);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('should has sources and layers as expected with empty GeoJSON', async () => {
|
|
142
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
143
|
-
|
|
144
|
-
const map = new Map();
|
|
145
|
-
|
|
146
|
-
const empty = {
|
|
147
|
-
type: 'FeatureCollection',
|
|
148
|
-
features: [],
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
new SimpleStyle(empty, { id: 'hello-world' }).addTo(map).fitBounds();
|
|
152
|
-
|
|
153
|
-
expect(Object.keys(map.sources)).toEqual(
|
|
154
|
-
['hello-world', 'hello-world-points'],
|
|
155
|
-
);
|
|
156
|
-
expect(map.layers.length).toEqual(8);
|
|
157
|
-
expect(map.bounds).toEqual(false);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('should update GeoJSON', async () => {
|
|
161
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
162
|
-
|
|
163
|
-
const map = new Map();
|
|
164
|
-
|
|
165
|
-
const empty = {
|
|
166
|
-
type: 'FeatureCollection',
|
|
167
|
-
features: [],
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const ss = new SimpleStyle(empty).addTo(map).fitBounds();
|
|
171
|
-
|
|
172
|
-
expect(Object.keys(map.sources)).toEqual(
|
|
173
|
-
['geolonia-simple-style', 'geolonia-simple-style-points'],
|
|
174
|
-
);
|
|
175
|
-
expect(map.layers.length).toEqual(8);
|
|
176
|
-
expect(map.bounds).toEqual(false);
|
|
177
|
-
expect(
|
|
178
|
-
map.sources['geolonia-simple-style-points'].data.features.length,
|
|
179
|
-
).toEqual(0);
|
|
180
|
-
|
|
181
|
-
ss.updateData(geojson);
|
|
182
|
-
expect(map.bounds).toEqual(false);
|
|
183
|
-
expect(
|
|
184
|
-
map.sources['geolonia-simple-style-points'].data.features.length,
|
|
185
|
-
).toEqual(1);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it('should load GeoJSON from url', async () => {
|
|
189
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
190
|
-
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
|
191
|
-
new Response(JSON.stringify(sampleRemoteGeoJSON), {
|
|
192
|
-
status: 200,
|
|
193
|
-
headers: { 'content-type': 'application/json' },
|
|
194
|
-
}),
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
const map = new Map();
|
|
198
|
-
const geojsonUrl = 'https://example.com/test.geojson';
|
|
199
|
-
const ss = new SimpleStyle(geojsonUrl);
|
|
200
|
-
ss.addTo(map).fitBounds();
|
|
201
|
-
|
|
202
|
-
await ss._loadingPromise;
|
|
203
|
-
|
|
204
|
-
const geometry =
|
|
205
|
-
map.sources['geolonia-simple-style'].data.features[0].geometry;
|
|
206
|
-
const coordinates = geometry.coordinates;
|
|
207
|
-
const type = geometry.type;
|
|
208
|
-
|
|
209
|
-
const expectCoordinates = [
|
|
210
|
-
[139.6870422363281, 35.73425097869431],
|
|
211
|
-
[139.76943969726562, 35.73425097869431],
|
|
212
|
-
[139.73922729492188, 35.66399091134812],
|
|
213
|
-
[139.70352172851562, 35.698571062054015],
|
|
214
|
-
];
|
|
215
|
-
|
|
216
|
-
expect(coordinates).toEqual(expectCoordinates);
|
|
217
|
-
expect(type).toEqual('LineString');
|
|
218
|
-
expect(map.bounds).toEqual(true);
|
|
219
|
-
|
|
220
|
-
fetchSpy.mockRestore();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
it('should load empty GeoJSON when failed to fetch GeoJSON', async () => {
|
|
224
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
225
|
-
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
|
226
|
-
new Response('Not Found', { status: 404 }),
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
const map = new Map();
|
|
230
|
-
const geojsonUrl = 'https://example.com/404.geojson';
|
|
231
|
-
const ss = new SimpleStyle(geojsonUrl);
|
|
232
|
-
ss.addTo(map).fitBounds();
|
|
233
|
-
|
|
234
|
-
await ss._loadingPromise;
|
|
235
|
-
|
|
236
|
-
expect(Object.keys(map.sources)).toEqual(
|
|
237
|
-
['geolonia-simple-style', 'geolonia-simple-style-points'],
|
|
238
|
-
);
|
|
239
|
-
expect(map.layers.length).toEqual(8);
|
|
240
|
-
expect(map.bounds).toEqual(false);
|
|
241
|
-
|
|
242
|
-
fetchSpy.mockRestore();
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it('should update GeoJSON from url', async () => {
|
|
246
|
-
const { SimpleStyle } = await import('./simplestyle');
|
|
247
|
-
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
|
|
248
|
-
new Response(JSON.stringify(sampleRemoteGeoJSON), {
|
|
249
|
-
status: 200,
|
|
250
|
-
headers: { 'content-type': 'application/json' },
|
|
251
|
-
}),
|
|
252
|
-
);
|
|
253
|
-
|
|
254
|
-
const map = new Map();
|
|
255
|
-
const empty = {
|
|
256
|
-
type: 'FeatureCollection',
|
|
257
|
-
features: [],
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
const ss = new SimpleStyle(empty);
|
|
261
|
-
ss.addTo(map).fitBounds();
|
|
262
|
-
|
|
263
|
-
await ss._loadingPromise;
|
|
264
|
-
|
|
265
|
-
const geojsonUrl = 'https://example.com/test.geojson';
|
|
266
|
-
|
|
267
|
-
ss.updateData(geojsonUrl);
|
|
268
|
-
|
|
269
|
-
await ss._loadingPromise;
|
|
270
|
-
|
|
271
|
-
const geometry =
|
|
272
|
-
map.sources['geolonia-simple-style'].data.features[0].geometry;
|
|
273
|
-
const coordinates = geometry.coordinates;
|
|
274
|
-
const type = geometry.type;
|
|
275
|
-
|
|
276
|
-
const expectCoordinates = [
|
|
277
|
-
[139.6870422363281, 35.73425097869431],
|
|
278
|
-
[139.76943969726562, 35.73425097869431],
|
|
279
|
-
[139.73922729492188, 35.66399091134812],
|
|
280
|
-
[139.70352172851562, 35.698571062054015],
|
|
281
|
-
];
|
|
282
|
-
|
|
283
|
-
expect(coordinates).toEqual(expectCoordinates);
|
|
284
|
-
expect(type).toEqual('LineString');
|
|
285
|
-
expect(map.bounds).toEqual(true);
|
|
286
|
-
|
|
287
|
-
fetchSpy.mockRestore();
|
|
288
|
-
});
|
|
289
|
-
});
|
package/src/lib/simplestyle.ts
DELETED
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import maplibregl from 'maplibre-gl';
|
|
4
|
-
import geojsonExtent from '@mapbox/geojson-extent';
|
|
5
|
-
import turfCenter from '@turf/center';
|
|
6
|
-
import { isURL, sanitizeDescription } from './util';
|
|
7
|
-
|
|
8
|
-
const textColor = '#000000';
|
|
9
|
-
const textHaloColor = '#FFFFFF';
|
|
10
|
-
const backgroundColor = 'rgba(255, 0, 0, 0.4)';
|
|
11
|
-
const strokeColor = '#FFFFFF';
|
|
12
|
-
|
|
13
|
-
const template = {
|
|
14
|
-
type: 'FeatureCollection',
|
|
15
|
-
features: [],
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export class SimpleStyle {
|
|
19
|
-
public _loadingPromise: Promise<unknown>;
|
|
20
|
-
private callFitBounds = false;
|
|
21
|
-
private geojson;
|
|
22
|
-
private map;
|
|
23
|
-
private options;
|
|
24
|
-
private _eventHandlers: { event: string; layer: string; handler: (...args) => void }[] = [];
|
|
25
|
-
|
|
26
|
-
constructor(geojson, options?) {
|
|
27
|
-
this.setGeoJSON(geojson);
|
|
28
|
-
|
|
29
|
-
this.options = {
|
|
30
|
-
id: 'geolonia-simple-style',
|
|
31
|
-
cluster: true,
|
|
32
|
-
heatmap: false, // TODO: It should support heatmap.
|
|
33
|
-
clusterColor: '#ff0000',
|
|
34
|
-
...options,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
updateData(geojson) {
|
|
39
|
-
this.setGeoJSON(geojson);
|
|
40
|
-
|
|
41
|
-
const features = this.geojson.features;
|
|
42
|
-
const polygonAndLines = features.filter(
|
|
43
|
-
(feature) => feature.geometry.type.toLowerCase() !== 'point',
|
|
44
|
-
);
|
|
45
|
-
const points = features.filter(
|
|
46
|
-
(feature) => feature.geometry.type.toLowerCase() === 'point',
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
this.map.getSource(this.options.id).setData({
|
|
50
|
-
type: 'FeatureCollection',
|
|
51
|
-
features: polygonAndLines,
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
this.map.getSource(`${this.options.id}-points`).setData({
|
|
55
|
-
type: 'FeatureCollection',
|
|
56
|
-
features: points,
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
return this;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
addTo(map) {
|
|
63
|
-
this.map = map;
|
|
64
|
-
|
|
65
|
-
const features = this.geojson.features;
|
|
66
|
-
const polygonAndLines = features.filter(
|
|
67
|
-
(feature) => feature.geometry.type.toLowerCase() !== 'point',
|
|
68
|
-
);
|
|
69
|
-
const points = features.filter(
|
|
70
|
-
(feature) => feature.geometry.type.toLowerCase() === 'point',
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
this.map.addSource(this.options.id, {
|
|
74
|
-
type: 'geojson',
|
|
75
|
-
data: {
|
|
76
|
-
type: 'FeatureCollection',
|
|
77
|
-
features: polygonAndLines,
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
this.setPolygonGeometries();
|
|
82
|
-
this.setLineGeometries();
|
|
83
|
-
|
|
84
|
-
this.map.addSource(`${this.options.id}-points`, {
|
|
85
|
-
type: 'geojson',
|
|
86
|
-
data: {
|
|
87
|
-
type: 'FeatureCollection',
|
|
88
|
-
features: points,
|
|
89
|
-
},
|
|
90
|
-
cluster: this.options.cluster,
|
|
91
|
-
clusterMaxZoom: 14,
|
|
92
|
-
clusterRadius: 50,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
this.map.addLayer({
|
|
96
|
-
id: `${this.options.id}-polygon-symbol`,
|
|
97
|
-
type: 'symbol',
|
|
98
|
-
source: this.options.id,
|
|
99
|
-
filter: ['==', '$type', 'Polygon'],
|
|
100
|
-
paint: {
|
|
101
|
-
'text-color': ['string', ['get', 'text-color'], textColor],
|
|
102
|
-
'text-halo-color': [
|
|
103
|
-
'string',
|
|
104
|
-
['get', 'text-halo-color'],
|
|
105
|
-
textHaloColor,
|
|
106
|
-
],
|
|
107
|
-
'text-halo-width': 1,
|
|
108
|
-
},
|
|
109
|
-
layout: {
|
|
110
|
-
'text-field': ['get', 'title'],
|
|
111
|
-
'text-font': ['Noto Sans Regular'],
|
|
112
|
-
'text-size': 12,
|
|
113
|
-
'text-max-width': 12,
|
|
114
|
-
'text-allow-overlap': false,
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
this.map.addLayer({
|
|
119
|
-
id: `${this.options.id}-linestring-symbol`,
|
|
120
|
-
type: 'symbol',
|
|
121
|
-
source: this.options.id,
|
|
122
|
-
filter: ['==', '$type', 'LineString'],
|
|
123
|
-
paint: {
|
|
124
|
-
'text-color': ['string', ['get', 'text-color'], textColor],
|
|
125
|
-
'text-halo-color': [
|
|
126
|
-
'string',
|
|
127
|
-
['get', 'text-halo-color'],
|
|
128
|
-
textHaloColor,
|
|
129
|
-
],
|
|
130
|
-
'text-halo-width': 1,
|
|
131
|
-
},
|
|
132
|
-
layout: {
|
|
133
|
-
'symbol-placement': 'line',
|
|
134
|
-
'text-field': ['get', 'title'],
|
|
135
|
-
'text-font': ['Noto Sans Regular'],
|
|
136
|
-
'text-size': 12,
|
|
137
|
-
'text-max-width': 12,
|
|
138
|
-
'text-allow-overlap': false,
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
this.setPointGeometries();
|
|
143
|
-
this.setCluster();
|
|
144
|
-
|
|
145
|
-
return this;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
fitBounds(options = {}) {
|
|
149
|
-
this.callFitBounds = true;
|
|
150
|
-
|
|
151
|
-
const _options = {
|
|
152
|
-
duration: 3000,
|
|
153
|
-
padding: 30,
|
|
154
|
-
...options,
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const bounds = geojsonExtent(this.geojson);
|
|
158
|
-
if (bounds) {
|
|
159
|
-
window.requestAnimationFrame(() => {
|
|
160
|
-
this.map.fitBounds(bounds, _options);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return this;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Set polygon geometries.
|
|
169
|
-
*/
|
|
170
|
-
setPolygonGeometries() {
|
|
171
|
-
this.map.addLayer({
|
|
172
|
-
id: `${this.options.id}-polygon`,
|
|
173
|
-
type: 'fill',
|
|
174
|
-
source: this.options.id,
|
|
175
|
-
filter: ['==', '$type', 'Polygon'],
|
|
176
|
-
paint: {
|
|
177
|
-
'fill-color': ['string', ['get', 'fill'], backgroundColor],
|
|
178
|
-
'fill-opacity': ['number', ['get', 'fill-opacity'], 1.0],
|
|
179
|
-
'fill-outline-color': ['string', ['get', 'stroke'], strokeColor],
|
|
180
|
-
},
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
this.setPopup(this.map, `${this.options.id}-polygon`);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Set line geometries.
|
|
188
|
-
*/
|
|
189
|
-
setLineGeometries() {
|
|
190
|
-
this.map.addLayer({
|
|
191
|
-
id: `${this.options.id}-linestring`,
|
|
192
|
-
type: 'line',
|
|
193
|
-
source: this.options.id,
|
|
194
|
-
filter: ['==', '$type', 'LineString'],
|
|
195
|
-
paint: {
|
|
196
|
-
'line-width': ['number', ['get', 'stroke-width'], 2],
|
|
197
|
-
'line-color': ['string', ['get', 'stroke'], backgroundColor],
|
|
198
|
-
'line-opacity': ['number', ['get', 'stroke-opacity'], 1.0],
|
|
199
|
-
},
|
|
200
|
-
layout: {
|
|
201
|
-
'line-cap': 'round',
|
|
202
|
-
'line-join': 'round',
|
|
203
|
-
},
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
this.setPopup(this.map, `${this.options.id}-linestring`);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Setup point geometries.
|
|
211
|
-
*/
|
|
212
|
-
setPointGeometries() {
|
|
213
|
-
this.map.addLayer({
|
|
214
|
-
id: `${this.options.id}-circle-points`,
|
|
215
|
-
type: 'circle',
|
|
216
|
-
source: `${this.options.id}-points`,
|
|
217
|
-
filter: ['all', ['!has', 'point_count'], ['!has', 'marker-symbol']],
|
|
218
|
-
paint: {
|
|
219
|
-
'circle-radius': [
|
|
220
|
-
'case',
|
|
221
|
-
['==', 'small', ['get', 'marker-size']],
|
|
222
|
-
7,
|
|
223
|
-
['==', 'large', ['get', 'marker-size']],
|
|
224
|
-
13,
|
|
225
|
-
9,
|
|
226
|
-
],
|
|
227
|
-
'circle-color': ['string', ['get', 'marker-color'], backgroundColor],
|
|
228
|
-
'circle-opacity': ['number', ['get', 'fill-opacity'], 1.0],
|
|
229
|
-
'circle-stroke-width': ['number', ['get', 'stroke-width'], 1],
|
|
230
|
-
'circle-stroke-color': ['string', ['get', 'stroke'], strokeColor],
|
|
231
|
-
'circle-stroke-opacity': ['number', ['get', 'stroke-opacity'], 1.0],
|
|
232
|
-
},
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
this.map.addLayer({
|
|
236
|
-
id: `${this.options.id}-symbol-points`,
|
|
237
|
-
type: 'symbol',
|
|
238
|
-
source: `${this.options.id}-points`,
|
|
239
|
-
filter: ['!', ['has', 'point_count']],
|
|
240
|
-
paint: {
|
|
241
|
-
'text-color': ['string', ['get', 'text-color'], textColor],
|
|
242
|
-
'text-halo-color': [
|
|
243
|
-
'string',
|
|
244
|
-
['get', 'text-halo-color'],
|
|
245
|
-
textHaloColor,
|
|
246
|
-
],
|
|
247
|
-
'text-halo-width': 1,
|
|
248
|
-
},
|
|
249
|
-
layout: {
|
|
250
|
-
'icon-image': ['get', 'marker-symbol'],
|
|
251
|
-
'text-field': ['get', 'title'],
|
|
252
|
-
'text-font': ['Noto Sans Regular'],
|
|
253
|
-
'text-size': 12,
|
|
254
|
-
'text-max-width': 12,
|
|
255
|
-
'text-allow-overlap': true,
|
|
256
|
-
'icon-allow-overlap': true,
|
|
257
|
-
'text-variable-anchor': ['top', 'bottom', 'left', 'right'],
|
|
258
|
-
'text-radial-offset': [
|
|
259
|
-
'case',
|
|
260
|
-
['==', 'small', ['get', 'marker-size']],
|
|
261
|
-
1,
|
|
262
|
-
['==', 'large', ['get', 'marker-size']],
|
|
263
|
-
1.6,
|
|
264
|
-
1.2,
|
|
265
|
-
],
|
|
266
|
-
},
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
this.setPopup(this.map, `${this.options.id}-circle-points`);
|
|
270
|
-
this.setPopup(this.map, `${this.options.id}-symbol-points`);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async setPopup(map, source) {
|
|
274
|
-
const clickHandler = async (e) => {
|
|
275
|
-
const center = turfCenter(e.features[0]).geometry.coordinates as [
|
|
276
|
-
number,
|
|
277
|
-
number,
|
|
278
|
-
];
|
|
279
|
-
const description = e.features[0].properties.description;
|
|
280
|
-
|
|
281
|
-
if (description) {
|
|
282
|
-
const sanitizedDescription = await sanitizeDescription(description);
|
|
283
|
-
new maplibregl.Popup()
|
|
284
|
-
.setLngLat(center)
|
|
285
|
-
.setHTML(sanitizedDescription)
|
|
286
|
-
.addTo(map);
|
|
287
|
-
}
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
const mouseEnterHandler = (e) => {
|
|
291
|
-
if (e.features[0].properties.description) {
|
|
292
|
-
map.getCanvas().style.cursor = 'pointer';
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
const mouseLeaveHandler = () => {
|
|
297
|
-
map.getCanvas().style.cursor = '';
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
map.on('click', source, clickHandler);
|
|
301
|
-
map.on('mouseenter', source, mouseEnterHandler);
|
|
302
|
-
map.on('mouseleave', source, mouseLeaveHandler);
|
|
303
|
-
|
|
304
|
-
this._eventHandlers.push(
|
|
305
|
-
{ event: 'click', layer: source, handler: clickHandler },
|
|
306
|
-
{ event: 'mouseenter', layer: source, handler: mouseEnterHandler },
|
|
307
|
-
{ event: 'mouseleave', layer: source, handler: mouseLeaveHandler },
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Setup cluster markers
|
|
313
|
-
*/
|
|
314
|
-
setCluster() {
|
|
315
|
-
this.map.addLayer({
|
|
316
|
-
id: `${this.options.id}-clusters`,
|
|
317
|
-
type: 'circle',
|
|
318
|
-
source: `${this.options.id}-points`,
|
|
319
|
-
filter: ['has', 'point_count'],
|
|
320
|
-
paint: {
|
|
321
|
-
'circle-radius': 20,
|
|
322
|
-
'circle-color': this.options.clusterColor,
|
|
323
|
-
'circle-opacity': 1.0,
|
|
324
|
-
},
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
this.map.addLayer({
|
|
328
|
-
id: `${this.options.id}-cluster-count`,
|
|
329
|
-
type: 'symbol',
|
|
330
|
-
source: `${this.options.id}-points`,
|
|
331
|
-
filter: ['has', 'point_count'],
|
|
332
|
-
layout: {
|
|
333
|
-
'text-field': '{point_count_abbreviated}',
|
|
334
|
-
'text-size': 14,
|
|
335
|
-
'text-font': ['Noto Sans Regular'],
|
|
336
|
-
},
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
const clusterLayer = `${this.options.id}-clusters`;
|
|
340
|
-
|
|
341
|
-
const clusterClickHandler = async (e) => {
|
|
342
|
-
const features = this.map.queryRenderedFeatures(e.point, {
|
|
343
|
-
layers: [clusterLayer],
|
|
344
|
-
});
|
|
345
|
-
const clusterId = features[0].properties.cluster_id;
|
|
346
|
-
const zoom = await this.map
|
|
347
|
-
.getSource(`${this.options.id}-points`)
|
|
348
|
-
.getClusterExpansionZoom(clusterId);
|
|
349
|
-
|
|
350
|
-
this.map.easeTo({
|
|
351
|
-
center: features[0].geometry.coordinates,
|
|
352
|
-
zoom: zoom,
|
|
353
|
-
});
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
const clusterEnterHandler = () => {
|
|
357
|
-
this.map.getCanvas().style.cursor = 'pointer';
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
const clusterLeaveHandler = () => {
|
|
361
|
-
this.map.getCanvas().style.cursor = '';
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
this.map.on('click', clusterLayer, clusterClickHandler);
|
|
365
|
-
this.map.on('mouseenter', clusterLayer, clusterEnterHandler);
|
|
366
|
-
this.map.on('mouseleave', clusterLayer, clusterLeaveHandler);
|
|
367
|
-
|
|
368
|
-
this._eventHandlers.push(
|
|
369
|
-
{ event: 'click', layer: clusterLayer, handler: clusterClickHandler },
|
|
370
|
-
{ event: 'mouseenter', layer: clusterLayer, handler: clusterEnterHandler },
|
|
371
|
-
{ event: 'mouseleave', layer: clusterLayer, handler: clusterLeaveHandler },
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
remove() {
|
|
376
|
-
if (!this.map) return this;
|
|
377
|
-
const id = this.options.id;
|
|
378
|
-
|
|
379
|
-
for (const { event, layer, handler } of this._eventHandlers) {
|
|
380
|
-
this.map.off(event, layer, handler);
|
|
381
|
-
}
|
|
382
|
-
this._eventHandlers = [];
|
|
383
|
-
|
|
384
|
-
const layerIds = [
|
|
385
|
-
`${id}-polygon-symbol`,
|
|
386
|
-
`${id}-linestring-symbol`,
|
|
387
|
-
`${id}-circle-points`,
|
|
388
|
-
`${id}-symbol-points`,
|
|
389
|
-
`${id}-polygon`,
|
|
390
|
-
`${id}-linestring`,
|
|
391
|
-
`${id}-clusters`,
|
|
392
|
-
`${id}-cluster-count`,
|
|
393
|
-
];
|
|
394
|
-
for (const layerId of layerIds) {
|
|
395
|
-
if (this.map.getLayer(layerId)) {
|
|
396
|
-
this.map.removeLayer(layerId);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
for (const sourceId of [id, `${id}-points`]) {
|
|
400
|
-
if (this.map.getSource(sourceId)) {
|
|
401
|
-
this.map.removeSource(sourceId);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
this.map.getCanvas().style.cursor = '';
|
|
406
|
-
|
|
407
|
-
return this;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
setGeoJSON(geojson) {
|
|
411
|
-
if (typeof geojson === 'string' && isURL(geojson)) {
|
|
412
|
-
this.geojson = template;
|
|
413
|
-
|
|
414
|
-
const fetchGeoJSON = async () => {
|
|
415
|
-
try {
|
|
416
|
-
const response = await window.fetch(geojson);
|
|
417
|
-
const data = response.ok ? await response.json() : {};
|
|
418
|
-
this.geojson = data;
|
|
419
|
-
this.updateData(data);
|
|
420
|
-
|
|
421
|
-
if (this.callFitBounds) {
|
|
422
|
-
this.fitBounds();
|
|
423
|
-
this.callFitBounds = false;
|
|
424
|
-
}
|
|
425
|
-
} catch (error) {
|
|
426
|
-
console.error('[Geolonia] Failed to load GeoJSON:', error); // eslint-disable-line no-console
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
this._loadingPromise = fetchGeoJSON();
|
|
431
|
-
} else {
|
|
432
|
-
this.geojson = geojson;
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
}
|