From d7e212c115552976c236e516176aa5d696a49c54 Mon Sep 17 00:00:00 2001 From: Olivier Guiter Date: Tue, 7 Nov 2017 12:24:10 +0100 Subject: [PATCH] Allow three elements in LngLat.convert, for GeoJSON Following RFC 7946, position is an array of number with two OR MORE elements (Implementations SHOULD NOT extend positions beyond three elements). The 3rd element is usually Altitude. We could ignore it but we must not fail. This small fix allows well-formed geojson file to be fully processed Signed-off-by: Olivier Guiter --- src/geo/lng_lat.js | 2 +- test/unit/geo/lng_lat.test.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/geo/lng_lat.js b/src/geo/lng_lat.js index f6781eaae3a..f53194e4762 100644 --- a/src/geo/lng_lat.js +++ b/src/geo/lng_lat.js @@ -107,7 +107,7 @@ class LngLat { if (input instanceof LngLat) { return input; } - if (Array.isArray(input) && input.length === 2) { + if (Array.isArray(input) && (input.length === 2 || input.length === 3)) { return new LngLat(Number(input[0]), Number(input[1])); } if (!Array.isArray(input) && typeof input === 'object' && input !== null) { diff --git a/test/unit/geo/lng_lat.test.js b/test/unit/geo/lng_lat.test.js index d13c3db6291..3f96b6c725e 100644 --- a/test/unit/geo/lng_lat.test.js +++ b/test/unit/geo/lng_lat.test.js @@ -23,8 +23,13 @@ test('LngLat', (t) => { t.test('#convert', (t) => { t.ok(LngLat.convert([0, 10]) instanceof LngLat, 'convert creates a LngLat instance'); + t.ok(LngLat.convert([0, 10, 0]) instanceof LngLat, 'convert creates a LngLat instance (Elevation)'); + t.throw(() => { + LngLat.convert([0, 10, 0, 5]); + }, "LngLat must not accept an array size bigger than 3'", 'detects and throws on invalid input'); t.ok(LngLat.convert({lng: 0, lat: 10}) instanceof LngLat, 'convert creates a LngLat instance'); t.ok(LngLat.convert({lng: 0, lat: 0}) instanceof LngLat, 'convert creates a LngLat instance'); + t.ok(LngLat.convert({lng: 0, lat: 0, elev: 0}) instanceof LngLat, 'convert creates a LngLat instance'); t.ok(LngLat.convert(new LngLat(0, 0)) instanceof LngLat, 'convert creates a LngLat instance'); t.throws(() => { LngLat.convert(0, 10);