From 1bd11c19581d59f1b8184507d62ef96720b9d4dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Tue, 25 Oct 2016 21:57:38 +0200 Subject: [PATCH 1/2] Close polygon sketch at all times --- src/ol/interaction/draw.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ol/interaction/draw.js b/src/ol/interaction/draw.js index 970e157e91..554ec6573a 100644 --- a/src/ol/interaction/draw.js +++ b/src/ol/interaction/draw.js @@ -152,7 +152,11 @@ ol.interaction.Draw = function(options) { geometryFunction = function(coordinates, opt_geometry) { var geometry = opt_geometry; if (geometry) { - geometry.setCoordinates(coordinates); + if (mode === ol.interaction.Draw.Mode.POLYGON) { + geometry.setCoordinates([coordinates[0].concat([coordinates[0][0]])]); + } else { + geometry.setCoordinates(coordinates); + } } else { geometry = new Constructor(coordinates); } @@ -617,12 +621,10 @@ ol.interaction.Draw.prototype.finishDrawing = function() { coordinates.pop(); this.geometryFunction_(coordinates, geometry); } else if (this.mode_ === ol.interaction.Draw.Mode.POLYGON) { - // When we finish drawing a polygon on the last point, - // the last coordinate is duplicated as for LineString - // we force the replacement by the first point + // remove the redundant last point in ring coordinates[0].pop(); - coordinates[0].push(coordinates[0][0]); this.geometryFunction_(coordinates, geometry); + coordinates = geometry.getCoordinates(); } // cast multi-part geometries From 9d5453a92729e8bf373f75529466f23dfeee418d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Thu, 27 Oct 2016 18:56:11 +0200 Subject: [PATCH 2/2] Test to validate sketch polygon --- test/spec/ol/interaction/draw.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/spec/ol/interaction/draw.test.js b/test/spec/ol/interaction/draw.test.js index 603a59dde7..e51b06f548 100644 --- a/test/spec/ol/interaction/draw.test.js +++ b/test/spec/ol/interaction/draw.test.js @@ -494,21 +494,30 @@ describe('ol.interaction.Draw', function() { map.addInteraction(draw); }); + function isClosed(polygon) { + var first = polygon.getFirstCoordinate(); + var last = polygon.getLastCoordinate(); + expect(first).to.eql(last); + } + it('draws polygon with clicks, finishing on first point', function() { // first point simulateEvent('pointermove', 10, 20); simulateEvent('pointerdown', 10, 20); simulateEvent('pointerup', 10, 20); + isClosed(draw.sketchFeature_.getGeometry()); // second point simulateEvent('pointermove', 30, 20); simulateEvent('pointerdown', 30, 20); simulateEvent('pointerup', 30, 20); + isClosed(draw.sketchFeature_.getGeometry()); // third point simulateEvent('pointermove', 40, 10); simulateEvent('pointerdown', 40, 10); simulateEvent('pointerup', 40, 10); + isClosed(draw.sketchFeature_.getGeometry()); // finish on first point simulateEvent('pointermove', 10, 20);