From b20767dd6acf902e083bb2608c0a3366df56f476 Mon Sep 17 00:00:00 2001 From: Jan Zak Date: Thu, 23 Jul 2020 10:51:14 +0200 Subject: [PATCH] Support getting the last polygon coordinate --- src/feature_types/polygon.js | 8 ++++++-- test/polygon.test.js | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/feature_types/polygon.js b/src/feature_types/polygon.js index e2da0b321..b256b8c1f 100644 --- a/src/feature_types/polygon.js +++ b/src/feature_types/polygon.js @@ -47,12 +47,16 @@ Polygon.prototype.removeCoordinate = function(path) { Polygon.prototype.getCoordinate = function(path) { const ids = path.split('.').map(x => parseInt(x, 10)); - const ring = this.coordinates[ids[0]]; + const coords = this.coordinates[ids[0]]; + const ring = coords.concat([coords[0]]); return JSON.parse(JSON.stringify(ring[ids[1]])); }; Polygon.prototype.getCoordinates = function() { - return this.coordinates.map(coords => coords.concat([coords[0]])); + return this.coordinates.map((coords) => { + const ring = coords.concat([coords[0]]); + return ring; + }); }; Polygon.prototype.updateCoordinate = function(path, lng, lat) { diff --git a/test/polygon.test.js b/test/polygon.test.js index 98b82bb39..b65184c53 100644 --- a/test/polygon.test.js +++ b/test/polygon.test.js @@ -53,6 +53,17 @@ test('Polygon#isValid', (t) => { t.end(); }); +test('Polygon#getCoordinate', (t) => { + const rawPolygon = createFeature('polygon'); + rawPolygon.geometry.coordinates = [[[1, 2], [3, 4], [5, 6], [1, 2]]]; + const polygon = new Polygon(createMockCtx(), rawPolygon); + + t.deepEqual(polygon.getCoordinate('0.0'), [1, 2], 'getCoordinate returns the first coordinate'); + t.deepEqual(polygon.getCoordinate('0.3'), [1, 2], 'getCoordinate returns the first coordinate as the last coordinate'); + + t.end(); +}); + test('Polygon#incomingCoords, Polygon#getCoordinates', (t) => { const rawPolygon = createFeature('polygon'); const polygon = new Polygon(createMockCtx(), rawPolygon);