diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 5915684be4..c463be099f 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -310,16 +310,13 @@ ol.geom.flat.lineStringInterpolate = * @param {number} offset Offset. * @param {number} end End. * @param {number} stride Stride. - * @param {ol.geom.GeometryLayout} layout Layout. * @param {number} m M. * @param {boolean} extrapolate Extrapolate. * @return {ol.Coordinate} Coordinate. */ ol.geom.flat.lineStringCoordinateAtM = - function(flatCoordinates, offset, end, stride, layout, m, extrapolate) { - if ((layout != ol.geom.GeometryLayout.XYM && - layout != ol.geom.GeometryLayout.XYZM) || - end == offset) { + function(flatCoordinates, offset, end, stride, m, extrapolate) { + if (end == offset) { return null; } var coordinate; @@ -374,6 +371,60 @@ ol.geom.flat.lineStringCoordinateAtM = }; +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {number} m M. + * @param {boolean} extrapolate Extrapolate. + * @param {boolean} interpolate Interpolate. + * @return {ol.Coordinate} Coordinate. + */ +ol.geom.flat.lineStringsCoordinateAtM = function( + flatCoordinates, offset, ends, stride, m, extrapolate, interpolate) { + if (interpolate) { + return ol.geom.flat.lineStringCoordinateAtM( + flatCoordinates, offset, ends[ends.length - 1], stride, m, extrapolate); + } + var coordinate; + if (m < flatCoordinates[stride - 1]) { + if (extrapolate) { + coordinate = flatCoordinates.slice(0, stride); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } + if (flatCoordinates[flatCoordinates.length - 1] < m) { + if (extrapolate) { + coordinate = flatCoordinates.slice(flatCoordinates.length - stride); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + if (offset == end) { + continue; + } + if (m < flatCoordinates[offset + stride - 1]) { + return null; + } else if (m <= flatCoordinates[end - 1]) { + return ol.geom.flat.lineStringCoordinateAtM( + flatCoordinates, offset, end, stride, m, false); + } + offset = end; + } + goog.asserts.fail(); + return null; +}; + + /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index d97a316f6a..989c9c88bb 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -81,14 +81,26 @@ ol.geom.LineString.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. + * * @param {number} m M. * @param {boolean=} opt_extrapolate Extrapolate. * @return {ol.Coordinate} Coordinate. */ ol.geom.LineString.prototype.getCoordinateAtM = function(m, opt_extrapolate) { + if (this.layout != ol.geom.GeometryLayout.XYM && + this.layout != ol.geom.GeometryLayout.XYZM) { + return null; + } var extrapolate = goog.isDef(opt_extrapolate) ? opt_extrapolate : false; return ol.geom.flat.lineStringCoordinateAtM(this.flatCoordinates, 0, - this.flatCoordinates.length, this.stride, this.layout, m, extrapolate); + this.flatCoordinates.length, this.stride, m, extrapolate); }; 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() {