@geolonia/embed 2.0.0 → 2.0.1
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/dist/embed +1 -1
- package/dist/embed.js +1 -1
- package/package.json +1 -1
- package/src/lib/simplestyle.js +66 -50
- package/src/lib/simplestyle.test.js +135 -0
package/package.json
CHANGED
package/src/lib/simplestyle.js
CHANGED
|
@@ -11,28 +11,48 @@ const backgroundColor = 'rgba(255, 0, 0, 0.4)';
|
|
|
11
11
|
const strokeColor = '#FFFFFF';
|
|
12
12
|
|
|
13
13
|
class SimpleStyle {
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
14
|
+
constructor(geojson, options) {
|
|
15
|
+
this.geojson = geojson;
|
|
16
16
|
|
|
17
17
|
this.options = {
|
|
18
18
|
id: 'geolonia-simple-style',
|
|
19
19
|
cluster: true,
|
|
20
|
-
heatmap: false,
|
|
20
|
+
heatmap: false, // TODO: It should support heatmap.
|
|
21
21
|
clusterColor: '#ff0000',
|
|
22
|
+
duration: 3000,
|
|
23
|
+
padding: 30,
|
|
22
24
|
...options,
|
|
23
25
|
};
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
updateData(geojson) {
|
|
29
|
+
const features = geojson.features;
|
|
30
|
+
const polygonandlines = features.filter((feature) => (feature.geometry.type.toLowerCase() !== 'point'));
|
|
31
|
+
const points = features.filter((feature) => (feature.geometry.type.toLowerCase() === 'point'));
|
|
32
|
+
|
|
33
|
+
this.map.getSource(this.options.id).setData({
|
|
34
|
+
'type': 'FeatureCollection',
|
|
35
|
+
'features': polygonandlines,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.map.getSource(`${this.options.id}-points`).setData({
|
|
39
|
+
'type': 'FeatureCollection',
|
|
40
|
+
'features': points,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
this.geojson = geojson;
|
|
44
|
+
|
|
45
|
+
return this;
|
|
28
46
|
}
|
|
29
47
|
|
|
30
48
|
addTo(map) {
|
|
31
|
-
|
|
49
|
+
this.map = map;
|
|
50
|
+
|
|
51
|
+
const features = this.geojson.features;
|
|
32
52
|
const polygonandlines = features.filter((feature) => (feature.geometry.type.toLowerCase() !== 'point'));
|
|
33
53
|
const points = features.filter((feature) => (feature.geometry.type.toLowerCase() === 'point'));
|
|
34
54
|
|
|
35
|
-
map.addSource(this.options.id, {
|
|
55
|
+
this.map.addSource(this.options.id, {
|
|
36
56
|
type: 'geojson',
|
|
37
57
|
data: {
|
|
38
58
|
type: 'FeatureCollection',
|
|
@@ -40,10 +60,10 @@ class SimpleStyle {
|
|
|
40
60
|
},
|
|
41
61
|
});
|
|
42
62
|
|
|
43
|
-
this.setPolygonGeometries(
|
|
44
|
-
this.setLineGeometries(
|
|
63
|
+
this.setPolygonGeometries();
|
|
64
|
+
this.setLineGeometries();
|
|
45
65
|
|
|
46
|
-
map.addSource(`${this.options.id}-points`, {
|
|
66
|
+
this.map.addSource(`${this.options.id}-points`, {
|
|
47
67
|
type: 'geojson',
|
|
48
68
|
data: {
|
|
49
69
|
type: 'FeatureCollection',
|
|
@@ -54,7 +74,7 @@ class SimpleStyle {
|
|
|
54
74
|
clusterRadius: 50,
|
|
55
75
|
});
|
|
56
76
|
|
|
57
|
-
map.addLayer({
|
|
77
|
+
this.map.addLayer({
|
|
58
78
|
id: `${this.options.id}-polygon-symbol`,
|
|
59
79
|
type: 'symbol',
|
|
60
80
|
source: this.options.id,
|
|
@@ -73,7 +93,7 @@ class SimpleStyle {
|
|
|
73
93
|
},
|
|
74
94
|
});
|
|
75
95
|
|
|
76
|
-
map.addLayer({
|
|
96
|
+
this.map.addLayer({
|
|
77
97
|
id: `${this.options.id}-linestring-symbol`,
|
|
78
98
|
type: 'symbol',
|
|
79
99
|
source: this.options.id,
|
|
@@ -93,27 +113,29 @@ class SimpleStyle {
|
|
|
93
113
|
},
|
|
94
114
|
});
|
|
95
115
|
|
|
96
|
-
this.setPointGeometries(
|
|
97
|
-
this.setCluster(
|
|
116
|
+
this.setPointGeometries();
|
|
117
|
+
this.setCluster();
|
|
98
118
|
|
|
99
|
-
const container = map.getContainer();
|
|
119
|
+
const container = this.map.getContainer();
|
|
100
120
|
|
|
101
121
|
if (!container.dataset || (!container.dataset.lng && !container.dataset.lat)) {
|
|
102
|
-
const bounds = geojsonExtent(this.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
122
|
+
const bounds = geojsonExtent(this.geojson);
|
|
123
|
+
if (bounds) {
|
|
124
|
+
this.map.fitBounds(bounds, {
|
|
125
|
+
duration: this.options.duration,
|
|
126
|
+
padding: this.options.padding,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
107
129
|
}
|
|
130
|
+
|
|
131
|
+
return this;
|
|
108
132
|
}
|
|
109
133
|
|
|
110
134
|
/**
|
|
111
135
|
* Set polygon geometries.
|
|
112
|
-
*
|
|
113
|
-
* @param map
|
|
114
136
|
*/
|
|
115
|
-
setPolygonGeometries(
|
|
116
|
-
map.addLayer({
|
|
137
|
+
setPolygonGeometries() {
|
|
138
|
+
this.map.addLayer({
|
|
117
139
|
id: `${this.options.id}-polygon`,
|
|
118
140
|
type: 'fill',
|
|
119
141
|
source: this.options.id,
|
|
@@ -125,16 +147,14 @@ class SimpleStyle {
|
|
|
125
147
|
},
|
|
126
148
|
});
|
|
127
149
|
|
|
128
|
-
this.setPopup(map, `${this.options.id}-polygon`);
|
|
150
|
+
this.setPopup(this.map, `${this.options.id}-polygon`);
|
|
129
151
|
}
|
|
130
152
|
|
|
131
153
|
/**
|
|
132
154
|
* Set line geometries.
|
|
133
|
-
*
|
|
134
|
-
* @param map
|
|
135
155
|
*/
|
|
136
|
-
setLineGeometries(
|
|
137
|
-
map.addLayer({
|
|
156
|
+
setLineGeometries() {
|
|
157
|
+
this.map.addLayer({
|
|
138
158
|
id: `${this.options.id}-linestring`,
|
|
139
159
|
type: 'line',
|
|
140
160
|
source: this.options.id,
|
|
@@ -150,17 +170,15 @@ class SimpleStyle {
|
|
|
150
170
|
},
|
|
151
171
|
});
|
|
152
172
|
|
|
153
|
-
this.setPopup(map, `${this.options.id}-linestring`);
|
|
173
|
+
this.setPopup(this.map, `${this.options.id}-linestring`);
|
|
154
174
|
}
|
|
155
175
|
|
|
156
176
|
/**
|
|
157
177
|
* Setup point geometries.
|
|
158
|
-
*
|
|
159
|
-
* @param map
|
|
160
178
|
*/
|
|
161
|
-
setPointGeometries(
|
|
162
|
-
map.addLayer({
|
|
163
|
-
id:
|
|
179
|
+
setPointGeometries() {
|
|
180
|
+
this.map.addLayer({
|
|
181
|
+
id: `${this.options.id}-circle-points`,
|
|
164
182
|
type: 'circle',
|
|
165
183
|
source: `${this.options.id}-points`,
|
|
166
184
|
filter: ['!', ['has', 'point_count']],
|
|
@@ -179,7 +197,7 @@ class SimpleStyle {
|
|
|
179
197
|
},
|
|
180
198
|
});
|
|
181
199
|
|
|
182
|
-
map.addLayer({
|
|
200
|
+
this.map.addLayer({
|
|
183
201
|
id: `${this.options.id}-symbol-points`,
|
|
184
202
|
type: 'symbol',
|
|
185
203
|
source: `${this.options.id}-points`,
|
|
@@ -210,7 +228,7 @@ class SimpleStyle {
|
|
|
210
228
|
},
|
|
211
229
|
});
|
|
212
230
|
|
|
213
|
-
this.setPopup(map,
|
|
231
|
+
this.setPopup(this.map, `${this.options.id}-circle-points`);
|
|
214
232
|
}
|
|
215
233
|
|
|
216
234
|
setPopup(map, source) {
|
|
@@ -236,11 +254,9 @@ class SimpleStyle {
|
|
|
236
254
|
|
|
237
255
|
/**
|
|
238
256
|
* Setup cluster markers
|
|
239
|
-
*
|
|
240
|
-
* @param map
|
|
241
257
|
*/
|
|
242
|
-
setCluster(
|
|
243
|
-
map.addLayer({
|
|
258
|
+
setCluster() {
|
|
259
|
+
this.map.addLayer({
|
|
244
260
|
id: `${this.options.id}-clusters`,
|
|
245
261
|
type: 'circle',
|
|
246
262
|
source: `${this.options.id}-points`,
|
|
@@ -252,7 +268,7 @@ class SimpleStyle {
|
|
|
252
268
|
},
|
|
253
269
|
});
|
|
254
270
|
|
|
255
|
-
map.addLayer({
|
|
271
|
+
this.map.addLayer({
|
|
256
272
|
id: `${this.options.id}-cluster-count`,
|
|
257
273
|
type: 'symbol',
|
|
258
274
|
source: `${this.options.id}-points`,
|
|
@@ -264,26 +280,26 @@ class SimpleStyle {
|
|
|
264
280
|
},
|
|
265
281
|
});
|
|
266
282
|
|
|
267
|
-
map.on('click', `${this.options.id}-clusters`, (e) => {
|
|
268
|
-
const features = map.queryRenderedFeatures(e.point, { layers: [`${this.options.id}-clusters`] });
|
|
283
|
+
this.map.on('click', `${this.options.id}-clusters`, (e) => {
|
|
284
|
+
const features = this.map.queryRenderedFeatures(e.point, { layers: [`${this.options.id}-clusters`] });
|
|
269
285
|
const clusterId = features[0].properties.cluster_id;
|
|
270
|
-
map.getSource(
|
|
286
|
+
this.map.getSource(`${this.options.id}-points`).getClusterExpansionZoom(clusterId, (err, zoom) => {
|
|
271
287
|
if (err)
|
|
272
288
|
return;
|
|
273
289
|
|
|
274
|
-
map.easeTo({
|
|
290
|
+
this.map.easeTo({
|
|
275
291
|
center: features[0].geometry.coordinates,
|
|
276
292
|
zoom: zoom,
|
|
277
293
|
});
|
|
278
294
|
});
|
|
279
295
|
});
|
|
280
296
|
|
|
281
|
-
map.on('mouseenter', `${this.options.id}-clusters`, () => {
|
|
282
|
-
map.getCanvas().style.cursor = 'pointer';
|
|
297
|
+
this.map.on('mouseenter', `${this.options.id}-clusters`, () => {
|
|
298
|
+
this.map.getCanvas().style.cursor = 'pointer';
|
|
283
299
|
});
|
|
284
300
|
|
|
285
|
-
map.on('mouseleave', `${this.options.id}-clusters`, () => {
|
|
286
|
-
map.getCanvas().style.cursor = '';
|
|
301
|
+
this.map.on('mouseleave', `${this.options.id}-clusters`, () => {
|
|
302
|
+
this.map.getCanvas().style.cursor = '';
|
|
287
303
|
});
|
|
288
304
|
}
|
|
289
305
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import assert from 'assert';
|
|
4
|
+
|
|
5
|
+
class Map {
|
|
6
|
+
constructor(json, options) {
|
|
7
|
+
this.json = json;
|
|
8
|
+
this.options = options;
|
|
9
|
+
this.sources = {};
|
|
10
|
+
this.layers = [];
|
|
11
|
+
this.bounds = false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
addSource(id, source) {
|
|
15
|
+
this.sources[id] = source;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
addLayer(layer) {
|
|
19
|
+
this.layers.push(layer);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
on() {
|
|
23
|
+
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getSource(id) {
|
|
27
|
+
class getSource {
|
|
28
|
+
constructor(id, sources) {
|
|
29
|
+
this.id = id;
|
|
30
|
+
this.sources = sources;
|
|
31
|
+
}
|
|
32
|
+
setData(geojson) {
|
|
33
|
+
this.sources[this.id] = {
|
|
34
|
+
type: 'geojson',
|
|
35
|
+
data: geojson,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return new getSource(id, this.sources);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getContainer() {
|
|
44
|
+
return { dataset: true };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// It should not be fired if GeoJSON is empty.
|
|
48
|
+
fitBounds() {
|
|
49
|
+
this.bounds = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const geojson = {
|
|
54
|
+
'type': 'FeatureCollection',
|
|
55
|
+
'features': [
|
|
56
|
+
{
|
|
57
|
+
'type': 'Feature',
|
|
58
|
+
'properties': {},
|
|
59
|
+
'geometry': {
|
|
60
|
+
'type': 'Point',
|
|
61
|
+
'coordinates': [
|
|
62
|
+
139.77012634277344,
|
|
63
|
+
35.68518697509636,
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
describe('Tests for simpleStyle()', () => {
|
|
71
|
+
it('should has sources and layers as expected', async () => {
|
|
72
|
+
window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
|
|
73
|
+
const { default: simpleStyle } = await import('./simplestyle');
|
|
74
|
+
|
|
75
|
+
const map = new Map();
|
|
76
|
+
new simpleStyle(geojson).addTo(map);
|
|
77
|
+
|
|
78
|
+
assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
|
|
79
|
+
assert.deepEqual(8, map.layers.length);
|
|
80
|
+
assert.deepEqual(true, map.bounds);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should has sources and layers as expected with custom IDs', async () => {
|
|
84
|
+
window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
|
|
85
|
+
const { default: simpleStyle } = await import('./simplestyle');
|
|
86
|
+
|
|
87
|
+
const map = new Map();
|
|
88
|
+
new simpleStyle(geojson, {id: 'hello-world'}).addTo(map);
|
|
89
|
+
|
|
90
|
+
assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
|
|
91
|
+
assert.deepEqual(8, map.layers.length);
|
|
92
|
+
assert.deepEqual(true, map.bounds);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should has sources and layers as expected with empty GeoJSON', async () => {
|
|
96
|
+
window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
|
|
97
|
+
const { default: simpleStyle } = await import('./simplestyle');
|
|
98
|
+
|
|
99
|
+
const map = new Map();
|
|
100
|
+
|
|
101
|
+
const empty = {
|
|
102
|
+
'type': 'FeatureCollection',
|
|
103
|
+
'features': [],
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
new simpleStyle(empty, {id: 'hello-world'}).addTo(map);
|
|
107
|
+
|
|
108
|
+
assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
|
|
109
|
+
assert.deepEqual(8, map.layers.length);
|
|
110
|
+
assert.deepEqual(false, map.bounds);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should update GeoJSON', async () => {
|
|
114
|
+
window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
|
|
115
|
+
const { default: simpleStyle } = await import('./simplestyle');
|
|
116
|
+
|
|
117
|
+
const map = new Map();
|
|
118
|
+
|
|
119
|
+
const empty = {
|
|
120
|
+
'type': 'FeatureCollection',
|
|
121
|
+
'features': [],
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const ss = new simpleStyle(empty).addTo(map); // The GeoJSON is empty.
|
|
125
|
+
|
|
126
|
+
assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
|
|
127
|
+
assert.deepEqual(8, map.layers.length);
|
|
128
|
+
assert.deepEqual(false, map.bounds);
|
|
129
|
+
assert.deepEqual(0, map.sources['geolonia-simple-style-points'].data.features.length);
|
|
130
|
+
|
|
131
|
+
ss.updateData(geojson); // The GeoJSON is not empty.
|
|
132
|
+
assert.deepEqual(false, map.bounds); // `fitBounds()` doesn't fire.
|
|
133
|
+
assert.deepEqual(1, map.sources['geolonia-simple-style-points'].data.features.length);
|
|
134
|
+
});
|
|
135
|
+
});
|