@geolonia/embed 2.0.2 → 2.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/embed",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "Geolonia embed JS API",
5
5
  "main": "dist/embed.js",
6
6
  "types": "src/embed.d.ts",
@@ -37,6 +37,7 @@
37
37
  "jsdom": "^16.2.2",
38
38
  "jsdom-global": "^3.0.2",
39
39
  "mocha": "^5.2.0",
40
+ "node-fetch": "2",
40
41
  "pngjs": "^6.0.0",
41
42
  "prettier-eslint": "^12.0.0",
42
43
  "prettier-eslint-cli": "^5.0.0",
@@ -212,8 +212,7 @@ export default class GeoloniaMap extends maplibregl.Map {
212
212
  if (el) {
213
213
  json = JSON.parse(el.textContent);
214
214
  } else {
215
- const response = await fetch(atts.geojson);
216
- json = await response.json();
215
+ json = atts.geojson;
217
216
  }
218
217
 
219
218
  const ss = new SimpleStyle(json, {
@@ -3,15 +3,24 @@
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import geojsonExtent from '@mapbox/geojson-extent';
5
5
  import turfCenter from '@turf/center';
6
+ import { isURL } from './util';
6
7
 
7
8
  const textColor = '#000000';
8
9
  const textHaloColor = '#FFFFFF';
9
10
  const backgroundColor = 'rgba(255, 0, 0, 0.4)';
10
11
  const strokeColor = '#FFFFFF';
11
12
 
13
+ const template = {
14
+ 'type': 'FeatureCollection',
15
+ 'features': [],
16
+ };
17
+
12
18
  class SimpleStyle {
13
19
  constructor(geojson, options) {
14
- this.geojson = geojson;
20
+
21
+ this.callFitBounds = false;
22
+
23
+ this.setGeoJSON(geojson);
15
24
 
16
25
  this.options = {
17
26
  id: 'geolonia-simple-style',
@@ -23,7 +32,10 @@ class SimpleStyle {
23
32
  }
24
33
 
25
34
  updateData(geojson) {
26
- const features = geojson.features;
35
+
36
+ this.setGeoJSON(geojson);
37
+
38
+ const features = this.geojson.features;
27
39
  const polygonandlines = features.filter((feature) => (feature.geometry.type.toLowerCase() !== 'point'));
28
40
  const points = features.filter((feature) => (feature.geometry.type.toLowerCase() === 'point'));
29
41
 
@@ -37,8 +49,6 @@ class SimpleStyle {
37
49
  'features': points,
38
50
  });
39
51
 
40
- this.geojson = geojson;
41
-
42
52
  return this;
43
53
  }
44
54
 
@@ -117,6 +127,9 @@ class SimpleStyle {
117
127
  }
118
128
 
119
129
  fitBounds(options = {}) {
130
+
131
+ this.callFitBounds = true;
132
+
120
133
  const _options = {
121
134
  duration: 3000,
122
135
  padding: 30,
@@ -305,6 +318,43 @@ class SimpleStyle {
305
318
  this.map.getCanvas().style.cursor = '';
306
319
  });
307
320
  }
321
+
322
+ setGeoJSON(geojson) {
323
+
324
+ if (typeof geojson === 'string' && isURL(geojson)) {
325
+
326
+ this.geojson = template;
327
+
328
+ const fetchGeoJSON = async () => {
329
+
330
+ try {
331
+
332
+ const response = await window.fetch(geojson);
333
+ const data = await response.json();
334
+ this.geojson = data;
335
+ this.updateData(data);
336
+
337
+ if (this.callFitBounds) {
338
+
339
+ this.fitBounds();
340
+ this.callFitBounds = false;
341
+
342
+ }
343
+
344
+ } catch (error) {
345
+
346
+ console.error('Failed to load GeoJSON:', error); // eslint-disable-line no-console
347
+ }
348
+
349
+ };
350
+
351
+ this._loadingPromise = fetchGeoJSON();
352
+
353
+ } else {
354
+ this.geojson = geojson;
355
+ }
356
+
357
+ }
308
358
  }
309
359
 
310
360
  export default SimpleStyle;
@@ -1,9 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  import assert from 'assert';
4
+ import nodeFetch from 'node-fetch';
4
5
 
5
6
  window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
6
7
  window.requestAnimationFrame = (cb) => cb();
8
+ window.fetch = nodeFetch;
7
9
 
8
10
  class Map {
9
11
  constructor(json, options) {
@@ -131,4 +133,106 @@ describe('Tests for simpleStyle()', () => {
131
133
  assert.deepEqual(false, map.bounds); // `fitBounds()` doesn't fire.
132
134
  assert.deepEqual(1, map.sources['geolonia-simple-style-points'].data.features.length);
133
135
  });
136
+
137
+ it('should load GeoJSON from url', async () => {
138
+ const { default: simpleStyle } = await import('./simplestyle');
139
+
140
+ const map = new Map();
141
+ const geojson = 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
142
+ const ss = new simpleStyle(geojson);
143
+ ss.addTo(map).fitBounds();
144
+
145
+ await ss._loadingPromise;
146
+
147
+ const geometry = map.sources['geolonia-simple-style'].data.features[0].geometry;
148
+ const coordinates = geometry.coordinates;
149
+ const type = geometry.type;
150
+
151
+ const expectCoordinates = [
152
+ [
153
+ 139.6870422363281,
154
+ 35.73425097869431,
155
+ ],
156
+ [
157
+ 139.76943969726562,
158
+ 35.73425097869431,
159
+ ],
160
+ [
161
+ 139.73922729492188,
162
+ 35.66399091134812,
163
+ ],
164
+ [
165
+ 139.70352172851562,
166
+ 35.698571062054015,
167
+ ],
168
+ ];
169
+
170
+ assert.deepEqual(expectCoordinates, coordinates);
171
+ assert.deepEqual('LineString', type);
172
+ assert.deepEqual(true, map.bounds);
173
+ });
174
+
175
+ it('should load empty GeoJSON when failed to fetch GeoJSON', async () => {
176
+ const { default: simpleStyle } = await import('./simplestyle');
177
+
178
+ const map = new Map();
179
+ const geojson = 'https://example.com/404.geojson';
180
+ const ss = new simpleStyle(geojson);
181
+ ss.addTo(map).fitBounds();
182
+
183
+ await ss._loadingPromise;
184
+
185
+ assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
186
+ assert.deepEqual(8, map.layers.length);
187
+ assert.deepEqual(false, map.bounds);
188
+ });
189
+
190
+ it('should update GeoJSON from url', async () => {
191
+ const { default: simpleStyle } = await import('./simplestyle');
192
+
193
+ const map = new Map();
194
+ const empty = {
195
+ 'type': 'FeatureCollection',
196
+ 'features': [],
197
+ };
198
+
199
+ const ss = new simpleStyle(empty);
200
+ ss.addTo(map).fitBounds();
201
+
202
+ await ss._loadingPromise;
203
+
204
+ const geojson = 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
205
+
206
+ ss.updateData(geojson);
207
+
208
+ await ss._loadingPromise;
209
+
210
+ const geometry = map.sources['geolonia-simple-style'].data.features[0].geometry;
211
+ const coordinates = geometry.coordinates;
212
+ const type = geometry.type;
213
+
214
+ const expectCoordinates = [
215
+ [
216
+ 139.6870422363281,
217
+ 35.73425097869431,
218
+ ],
219
+ [
220
+ 139.76943969726562,
221
+ 35.73425097869431,
222
+ ],
223
+ [
224
+ 139.73922729492188,
225
+ 35.66399091134812,
226
+ ],
227
+ [
228
+ 139.70352172851562,
229
+ 35.698571062054015,
230
+ ],
231
+ ];
232
+
233
+ assert.deepEqual(expectCoordinates, coordinates);
234
+ assert.deepEqual('LineString', type);
235
+ assert.deepEqual(true, map.bounds);
236
+ });
237
+
134
238
  });