Skip to content

Commit

Permalink
Support getting the last polygon coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
zakjan committed Jul 24, 2020
1 parent b63c0a4 commit b20767d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/feature_types/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/polygon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b20767d

Please sign in to comment.