diff --git a/src/ol/geom/LineString.js b/src/ol/geom/LineString.js index 0fe7da857b..b21c0bb44c 100644 --- a/src/ol/geom/LineString.js +++ b/src/ol/geom/LineString.js @@ -88,6 +88,16 @@ class LineString extends SimpleGeometry { return new LineString(this.flatCoordinates.slice(), this.layout); } + /** + * @inheritDoc + * @override + * @api + */ + containsXY(x, y) { + const coord = this.getClosestPoint([x, y]); + return coord[0] === x && coord[1] === y; + } + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index 70968b30a0..a4c7892bca 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -443,4 +443,37 @@ describe('ol.geom.LineString', function() { }); + describe('#containsXY()', function() { + + let lineString; + beforeEach(function() { + lineString = new LineString([ + [0, 0, 0, 0], + [1, -1, 2, 1], + [2, -2, 4, 2], + [4, -4, 8, 4], + [8, -8, 16, 8], + [12, -12, 24, 12], + [14, -14, 28, 14], + [15, -15, 30, 15], + [16, -16, 32, 16], + [18, -18, 36, 18], + [22, -22, 44, 22] + ]); + }); + + it('does contain XY', function() { + expect(lineString.containsXY(1, -1)).to.be(true); + expect(lineString.containsXY(16, -16)).to.be(true); + expect(lineString.containsXY(3, -3)).to.be(true); + }); + + it('does not contain XY', function() { + expect(lineString.containsXY(1, 3)).to.be(false); + expect(lineString.containsXY(2, 2)).to.be(false); + expect(lineString.containsXY(2, 3)).to.be(false); + }); + + }); + });