Do not repeat first vertex when closed is true

This commit is contained in:
Andreas Hocevar
2016-09-12 23:42:54 +02:00
parent 51776ed79e
commit 129757578d
2 changed files with 48 additions and 7 deletions

View File

@@ -157,16 +157,11 @@ ol.render.canvas.Replay.prototype.appendFlatCoordinates = function(flatCoordinat
lastRel = nextRel; lastRel = nextRel;
} }
// handle case where there is only one point to append // When we want to close or there is only one point to append:
if (i === offset + stride) { if ((close && skipped) || i === offset + stride) {
this.coordinates[myEnd++] = lastCoord[0]; this.coordinates[myEnd++] = lastCoord[0];
this.coordinates[myEnd++] = lastCoord[1]; this.coordinates[myEnd++] = lastCoord[1];
} }
if (close) {
this.coordinates[myEnd++] = flatCoordinates[offset];
this.coordinates[myEnd++] = flatCoordinates[offset + 1];
}
return myEnd; return myEnd;
}; };

View File

@@ -169,6 +169,12 @@ describe('ol.render.canvas.Replay', function() {
expect(replay.coordinates).to.eql(flat); expect(replay.coordinates).to.eql(flat);
}); });
it('appends polygon coordinates that are within the max extent', function() {
var flat = [-110, 45, 110, 45, 110, -45, -110, -45, -110, 45];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, true);
expect(replay.coordinates).to.eql(flat);
});
it('works with a single coordinate (inside)', function() { it('works with a single coordinate (inside)', function() {
var flat = [-110, 45]; var flat = [-110, 45];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, false); replay.appendFlatCoordinates(flat, 0, flat.length, 2, false);
@@ -183,6 +189,14 @@ describe('ol.render.canvas.Replay', function() {
expect(replay.coordinates).to.eql(flat); expect(replay.coordinates).to.eql(flat);
}); });
it('always appends first polygon vertex (even if outside)', function() {
// this could be changed, but to make the code simpler for properly
// closing rings, we always add the first point
var flat = [-110, 145, -110, 145];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, true);
expect(replay.coordinates).to.eql(flat);
});
it('appends points when segments cross (top to bottom)', function() { it('appends points when segments cross (top to bottom)', function() {
// this means we get a few extra points when coordinates are not // this means we get a few extra points when coordinates are not
// part of a linestring or ring, but only a few extra // part of a linestring or ring, but only a few extra
@@ -205,18 +219,50 @@ describe('ol.render.canvas.Replay', function() {
expect(replay.coordinates).to.eql(flat); expect(replay.coordinates).to.eql(flat);
}); });
it('always appends the first polygon segment (even when outside)', function() {
// this could be changed, but to make the code simpler for properly
// closing rings, we always add the first segment
var flat = [-10, 200, 10, 200, -10, 200];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, true);
expect(replay.coordinates).to.eql(flat);
});
it('eliminates segments outside (and not changing rel)', function() {
var flat = [0, 0, 0, 200, 5, 200, 10, 200];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, false);
expect(replay.coordinates).to.eql([0, 0, 0, 200]);
});
it('eliminates polygon segments outside (and not changing rel)', function() {
var flat = [0, 0, 0, 200, 5, 200, 10, 200, 0, 0];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, true);
expect(replay.coordinates).to.eql([0, 0, 0, 200, 10, 200, 0, 0]);
});
it('eliminates segments outside (and not changing rel)', function() { it('eliminates segments outside (and not changing rel)', function() {
var flat = [0, 0, 0, 200, 10, 200]; var flat = [0, 0, 0, 200, 10, 200];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, false); replay.appendFlatCoordinates(flat, 0, flat.length, 2, false);
expect(replay.coordinates).to.eql([0, 0, 0, 200]); expect(replay.coordinates).to.eql([0, 0, 0, 200]);
}); });
it('includes polygon segments outside (and not changing rel) when on last segment', function() {
var flat = [0, 0, 0, 200, 10, 200, 0, 0];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, true);
expect(replay.coordinates).to.eql(flat);
});
it('includes outside segments that change relationship', function() { it('includes outside segments that change relationship', function() {
var flat = [0, 0, 0, 200, 200, 200, 250, 200]; var flat = [0, 0, 0, 200, 200, 200, 250, 200];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, false); replay.appendFlatCoordinates(flat, 0, flat.length, 2, false);
expect(replay.coordinates).to.eql([0, 0, 0, 200, 200, 200]); expect(replay.coordinates).to.eql([0, 0, 0, 200, 200, 200]);
}); });
it('includes outside polygon segments that change relationship when on last segment', function() {
var flat = [0, 0, 0, 200, 200, 200, 250, 200, 0, 0];
replay.appendFlatCoordinates(flat, 0, flat.length, 2, true);
expect(replay.coordinates).to.eql(flat);
});
}); });
}); });