Merge pull request #6027 from bjornharrtell/issue-6023

Close polygon sketch at all times
This commit is contained in:
Andreas Hocevar
2016-10-28 00:37:22 +02:00
committed by GitHub
2 changed files with 16 additions and 5 deletions

View File

@@ -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

View File

@@ -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);