Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show warning on console when coordinate is string #296

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -37,6 +37,9 @@
"jsdom-global": "^3.0.2",
"mocha": "^10.2.0",
"node-fetch": "2",
"pngjs": "^6.0.0",
"puppeteer": "^9.1.1",
"sinon": "^14.0.0",
"prettier-eslint": "^15.0.0",
"prettier-eslint-cli": "^7.1.0",
"style-loader": "^3.3.1",
35 changes: 35 additions & 0 deletions src/lib/simplestyle.js
Original file line number Diff line number Diff line change
@@ -329,7 +329,9 @@ class SimpleStyle {

const response = await window.fetch(geojson);
const data = await response.json();
this.validateGeoJSON(data);
this.geojson = data;

this.updateData(data);

if (this.callFitBounds) {
@@ -349,10 +351,43 @@ class SimpleStyle {
this._loadingPromise = fetchGeoJSON();

} else {

this.validateGeoJSON(geojson);
this.geojson = geojson;
}

}

/**
* Validate GeoJSON coordinate is number.
*/
validateGeoJSON(geojson) {

if (geojson.features) {

geojson.features.forEach((feature) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const feature of geojson.features || []) { で直前のif文も省ける
基本的に for .. of 使いましょう


if (feature.geometry.coordinates) {

const coordinates = feature.geometry.coordinates.flat(3); // flatten array for each geometry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(feature.geometry.coordinates || []).flat(3) でif文省く


for (let i = 0; i < coordinates.length; i++) {

const coordinate = coordinates[i];

if (typeof coordinate !== 'number') {
console.warn('GeoJSON coordinate value must be number'); // eslint-disable-line no-console
break;
}

}

}

});
}
}

}

export default SimpleStyle;
128 changes: 128 additions & 0 deletions src/lib/simplestyle.test.js
Original file line number Diff line number Diff line change
@@ -3,10 +3,12 @@

import assert from 'assert';
import nodeFetch from 'node-fetch';
import sinon from 'sinon';

window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
window.requestAnimationFrame = (cb) => cb();
window.fetch = nodeFetch;
let spy;

class Map {
constructor(json, options) {
@@ -74,6 +76,15 @@ const geojson = {
};

describe('Tests for simpleStyle()', () => {

beforeEach(() => {
spy = sinon.spy(console, 'warn');
});

afterEach(() => {
sinon.restore();
});

it('should has sources and layers as expected', async () => {
const { default: simpleStyle } = await import('./simplestyle');

@@ -237,4 +248,121 @@ describe('Tests for simpleStyle()', () => {
assert.deepEqual(true, map.bounds);
});

it('should show console.warn when coordinates is string type with Point', async () => {
const { default: simpleStyle } = await import('./simplestyle');

const geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Point',
'coordinates': [
'139.77012634277344',
'35.68518697509636',
],
},
},
],
};

const map = new Map();
new simpleStyle(geojson).addTo(map).fitBounds();

assert.deepEqual(true, spy.calledWith('GeoJSON coordinate value must be number'));
});

it('should show console.warn when coordinates is string type with LineString', async () => {
const { default: simpleStyle } = await import('./simplestyle');

const geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
[
'139.75503623485565',
'35.67479010282487',
],
[
'139.755819439888',
'35.67316029425285',
],
[
'139.75565314292908',
'35.6741974490131',
],
[
'139.75497722625732',
'35.67337818502668',
],
],
},
},
],
};

const map = new Map();
new simpleStyle(geojson).addTo(map).fitBounds();

assert.deepEqual(true, spy.calledWith('GeoJSON coordinate value must be number'));
});

it('should show console.warn when coordinates is string type with Polygon', async () => {
const { default: simpleStyle } = await import('./simplestyle');

const geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'Polygon',
'coordinates': [
[
[
'139.75602865219116',
'35.674171302420035',
],
[
'139.75779354572296',
'35.6737660291323',
],
[
'139.75753605365753',
'35.67519537091245',
],
[
'139.75613057613373',
'35.67496005420893',
],
[
'139.75573360919952',
'35.67431075081735',
],
[
'139.75602865219116',
'35.674171302420035',
],
],
],
},
},
],
};

const map = new Map();
new simpleStyle(geojson).addTo(map).fitBounds();

assert.deepEqual(true, spy.calledWith('GeoJSON coordinate value must be number'));

});

});
Loading