Skip to content

Commit

Permalink
Allow three elements in LngLat.convert, for GeoJSON
Browse files Browse the repository at this point in the history
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 <oguiter@free.fr>
  • Loading branch information
oguiter authored and jfirebaugh committed Nov 13, 2017
1 parent 171c536 commit 812d7c2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/geo/lng_lat.js
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/geo/lng_lat.test.js
Expand Up @@ -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);
Expand Down

0 comments on commit 812d7c2

Please sign in to comment.