Merge pull request #4541 from fredj/linestring_interpolated_point

Add new ol.geom.LineString#getCoordinateAt function
This commit is contained in:
Frédéric Junod
2016-01-07 18:02:21 +01:00
2 changed files with 51 additions and 3 deletions

View File

@@ -166,6 +166,23 @@ ol.geom.LineString.prototype.getCoordinates = function() {
};
/**
* Return the coordinate at the provided fraction along the linestring.
* The `fraction` is a number between 0 and 1, where 0 is the start of the
* linestring and 1 is the end.
* @param {number} fraction Fraction.
* @param {ol.Coordinate=} opt_dest Optional coordinate whose values will
* be modified. If not provided, a new coordinate will be returned.
* @return {ol.Coordinate} Coordinate of the interpolated point.
* @api
*/
ol.geom.LineString.prototype.getCoordinateAt = function(fraction, opt_dest) {
return ol.geom.flat.interpolate.lineString(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
fraction, opt_dest);
};
/**
* Return the length of the linestring on projected plane.
* @return {number} Length (on projected plane).
@@ -182,9 +199,7 @@ ol.geom.LineString.prototype.getLength = function() {
*/
ol.geom.LineString.prototype.getFlatMidpoint = function() {
if (this.flatMidpointRevision_ != this.getRevision()) {
this.flatMidpoint_ = ol.geom.flat.interpolate.lineString(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
0.5, this.flatMidpoint_);
this.flatMidpoint_ = this.getCoordinateAt(0.5, this.flatMidpoint_);
this.flatMidpointRevision_ = this.getRevision();
}
return this.flatMidpoint_;

View File

@@ -89,6 +89,22 @@ describe('ol.geom.LineString', function() {
});
describe('#getCoordinateAt', function() {
it('return the first point when fraction is 0', function() {
expect(lineString.getCoordinateAt(0)).to.eql([1, 2]);
});
it('return the last point when fraction is 1', function() {
expect(lineString.getCoordinateAt(1)).to.eql([3, 4]);
});
it('return the mid point when fraction is 0.5', function() {
expect(lineString.getCoordinateAt(0.5)).to.eql([2, 3]);
});
});
});
describe('construct with 3D coordinates', function() {
@@ -320,6 +336,23 @@ describe('ol.geom.LineString', function() {
});
describe('#getCoordinateAt', function() {
it('return the first point when fraction is 0', function() {
expect(lineString.getCoordinateAt(0)).to.eql([0, 0]);
});
it('return the last point when fraction is 1', function() {
expect(lineString.getCoordinateAt(1)).to.eql([7, 5]);
});
it('return the mid point when fraction is 0.5', function() {
var midpoint = lineString.getFlatMidpoint();
expect(lineString.getCoordinateAt(0.5)).to.eql(midpoint);
});
});
});
describe('with a simple XYM coordinates', function() {