diff --git a/src/ol/geom/multilinestring.exports b/src/ol/geom/multilinestring.exports index 47f28467da..23b3e5987e 100644 --- a/src/ol/geom/multilinestring.exports +++ b/src/ol/geom/multilinestring.exports @@ -1,5 +1,6 @@ @exportSymbol ol.geom.MultiLineString @exportProperty ol.geom.MultiLineString.prototype.clone +@exportProperty ol.geom.MultiLineString.prototype.getCoordinateAtM @exportProperty ol.geom.MultiLineString.prototype.getCoordinates @exportProperty ol.geom.MultiLineString.prototype.getLineStrings @exportProperty ol.geom.MultiLineString.prototype.getType diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 5edb6f2267..b096e2fe65 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -78,6 +78,41 @@ ol.geom.MultiLineString.prototype.closestPointXY = }; +/** + * Returns the coordinate at `m` using linear interpolation, or `null` if no + * such coordinate exists. + * + * `opt_extrapolate` controls extrapolation beyond the range of Ms in the + * MultiLineString. If `opt_extrapolate` is `true` then Ms less than the first + * M will return the first coordinate and Ms greater than the last M will + * return the last coordinate. + * + * `opt_interpolate` controls interpolation between consecutive LineStrings + * within the MultiLineString. If `opt_interpolate` is `true` the coordinates + * will be linearly interpolated between the last coordinate of one LineString + * and the first coordinate of the next LineString. If `opt_interpolate` is + * `false` then the function will return `null` for Ms falling between + * LineStrings. + * + * @param {number} m M. + * @param {boolean=} opt_extrapolate Extrapolate. + * @param {boolean=} opt_interpolate Interpolate. + * @return {ol.Coordinate} Coordinate. + */ +ol.geom.MultiLineString.prototype.getCoordinateAtM = + function(m, opt_extrapolate, opt_interpolate) { + if ((this.layout != ol.geom.GeometryLayout.XYM && + this.layout != ol.geom.GeometryLayout.XYZM) || + this.flatCoordinates.length === 0) { + return null; + } + var extrapolate = goog.isDef(opt_extrapolate) ? opt_extrapolate : false; + var interpolate = goog.isDef(opt_interpolate) ? opt_interpolate : false; + return ol.geom.flat.lineStringsCoordinateAtM(this.flatCoordinates, 0, + this.ends_, this.stride, m, extrapolate, interpolate); +}; + + /** * @return {ol.geom.RawMultiLineString} Coordinates. * @todo stability experimental diff --git a/test/spec/ol/geom/multilinestring.test.js b/test/spec/ol/geom/multilinestring.test.js index c3fdb0e174..8fe2d205a0 100644 --- a/test/spec/ol/geom/multilinestring.test.js +++ b/test/spec/ol/geom/multilinestring.test.js @@ -142,6 +142,110 @@ describe('ol.geom.MultiLineString', function() { expect(multiLineString.getStride()).to.be(3); }); + describe('#getCoordinateAtM', function() { + + describe('with extrapolation and interpolation', function() { + + it('returns the expected value', function() { + expect(multiLineString.getCoordinateAtM(0, true, true)).to.eql( + [1, 2, 0]); + expect(multiLineString.getCoordinateAtM(3, true, true)).to.eql( + [1, 2, 3]); + expect(multiLineString.getCoordinateAtM(4.5, true, true)).to.eql( + [2.5, 3.5, 4.5]); + expect(multiLineString.getCoordinateAtM(6, true, true)).to.eql( + [4, 5, 6]); + expect(multiLineString.getCoordinateAtM(7.5, true, true)).to.eql( + [5.5, 6.5, 7.5]); + expect(multiLineString.getCoordinateAtM(9, true, true)).to.eql( + [7, 8, 9]); + expect(multiLineString.getCoordinateAtM(10.5, true, true)).to.eql( + [8.5, 9.5, 10.5]); + expect(multiLineString.getCoordinateAtM(12, true, true)).to.eql( + [10, 11, 12]); + expect(multiLineString.getCoordinateAtM(15, true, true)).to.eql( + [10, 11, 15]); + }); + + }); + + describe('with extrapolation and no interpolation', function() { + + it('returns the expected value', function() { + expect(multiLineString.getCoordinateAtM(0, true, false)).to.eql( + [1, 2, 0]); + expect(multiLineString.getCoordinateAtM(3, true, false)).to.eql( + [1, 2, 3]); + expect(multiLineString.getCoordinateAtM(4.5, true, false)).to.eql( + [2.5, 3.5, 4.5]); + expect(multiLineString.getCoordinateAtM(6, true, false)).to.eql( + [4, 5, 6]); + expect(multiLineString.getCoordinateAtM(7.5, true, false)).to.be( + null); + expect(multiLineString.getCoordinateAtM(9, true, false)).to.eql( + [7, 8, 9]); + expect(multiLineString.getCoordinateAtM(10.5, true, false)).to.eql( + [8.5, 9.5, 10.5]); + expect(multiLineString.getCoordinateAtM(12, true, false)).to.eql( + [10, 11, 12]); + expect(multiLineString.getCoordinateAtM(15, true, false)).to.eql( + [10, 11, 15]); + }); + + }); + + describe('with no extrapolation and interpolation', function() { + + it('returns the expected value', function() { + expect(multiLineString.getCoordinateAtM(0, false, true)).to.eql( + null); + expect(multiLineString.getCoordinateAtM(3, false, true)).to.eql( + [1, 2, 3]); + expect(multiLineString.getCoordinateAtM(4.5, false, true)).to.eql( + [2.5, 3.5, 4.5]); + expect(multiLineString.getCoordinateAtM(6, false, true)).to.eql( + [4, 5, 6]); + expect(multiLineString.getCoordinateAtM(7.5, false, true)).to.eql( + [5.5, 6.5, 7.5]); + expect(multiLineString.getCoordinateAtM(9, false, true)).to.eql( + [7, 8, 9]); + expect(multiLineString.getCoordinateAtM(10.5, false, true)).to.eql( + [8.5, 9.5, 10.5]); + expect(multiLineString.getCoordinateAtM(12, false, true)).to.eql( + [10, 11, 12]); + expect(multiLineString.getCoordinateAtM(15, false, true)).to.eql( + null); + }); + + }); + + describe('with no extrapolation or interpolation', function() { + + it('returns the expected value', function() { + expect(multiLineString.getCoordinateAtM(0, false, false)).to.eql( + null); + expect(multiLineString.getCoordinateAtM(3, false, false)).to.eql( + [1, 2, 3]); + expect(multiLineString.getCoordinateAtM(4.5, false, false)).to.eql( + [2.5, 3.5, 4.5]); + expect(multiLineString.getCoordinateAtM(6, false, false)).to.eql( + [4, 5, 6]); + expect(multiLineString.getCoordinateAtM(7.5, false, false)).to.eql( + null); + expect(multiLineString.getCoordinateAtM(9, false, false)).to.eql( + [7, 8, 9]); + expect(multiLineString.getCoordinateAtM(10.5, false, false)).to.eql( + [8.5, 9.5, 10.5]); + expect(multiLineString.getCoordinateAtM(12, false, false)).to.eql( + [10, 11, 12]); + expect(multiLineString.getCoordinateAtM(15, false, false)).to.eql( + null); + }); + + }); + + }); + }); describe('construct with 4D coordinates', function() {