diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 39ac1fbc97..ad99d257bd 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -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_; diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index 023510b090..9c9719907e 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -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() {