diff --git a/src/ol/geom/Geometry.js b/src/ol/geom/Geometry.js index 540559670d..5886ca90fe 100644 --- a/src/ol/geom/Geometry.js +++ b/src/ol/geom/Geometry.js @@ -92,7 +92,8 @@ class Geometry extends BaseObject { * @return {boolean} Contains (x, y). */ containsXY(x, y) { - return false; + const coord = this.getClosestPoint([x, y]); + return coord[0] === x && coord[1] === y; } /** 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); + }); + + }); + }); diff --git a/test/spec/ol/geom/multilinestring.test.js b/test/spec/ol/geom/multilinestring.test.js index e497d21db6..9b4b9302a9 100644 --- a/test/spec/ol/geom/multilinestring.test.js +++ b/test/spec/ol/geom/multilinestring.test.js @@ -355,4 +355,30 @@ describe('ol.geom.MultiLineString', function() { }); }); + describe('#containsXY()', function() { + + let multiLineString; + beforeEach(function() { + multiLineString = new MultiLineString( + [[[1, 2, 3], [4, 5, 6]], [[-1, -1, 9], [2, 2, 12]]]); + }); + + it('does contain XY', function() { + expect(multiLineString.containsXY(1, 2)).to.be(true); + expect(multiLineString.containsXY(4, 5)).to.be(true); + expect(multiLineString.containsXY(3, 4)).to.be(true); + + expect(multiLineString.containsXY(-1, -1)).to.be(true); + expect(multiLineString.containsXY(2, 2)).to.be(true); + expect(multiLineString.containsXY(0, 0)).to.be(true); + }); + + it('does not contain XY', function() { + expect(multiLineString.containsXY(1, 3)).to.be(false); + expect(multiLineString.containsXY(2, 11)).to.be(false); + expect(multiLineString.containsXY(-2, 3)).to.be(false); + }); + + }); + }); diff --git a/test/spec/ol/geom/multipoint.test.js b/test/spec/ol/geom/multipoint.test.js index bdbb32533c..206eb8dd5f 100644 --- a/test/spec/ol/geom/multipoint.test.js +++ b/test/spec/ol/geom/multipoint.test.js @@ -287,4 +287,27 @@ describe('ol.geom.MultiPoint', function() { }); + describe('#containsXY()', function() { + + it('does contain XY', function() { + const multi = new MultiPoint([[1, 2], [10, 20]]); + + expect(multi.containsXY(1, 2)).to.be(true); + expect(multi.containsXY(10, 20)).to.be(true); + }); + + it('does not contain XY', function() { + const multi = new MultiPoint([[1, 2], [10, 20]]); + + expect(multi.containsXY(1, 3)).to.be(false); + expect(multi.containsXY(2, 2)).to.be(false); + expect(multi.containsXY(2, 3)).to.be(false); + + expect(multi.containsXY(10, 30)).to.be(false); + expect(multi.containsXY(20, 20)).to.be(false); + expect(multi.containsXY(20, 30)).to.be(false); + }); + + }); + }); diff --git a/test/spec/ol/geom/point.test.js b/test/spec/ol/geom/point.test.js index 0f9e4e9aa6..3865b5ccd5 100644 --- a/test/spec/ol/geom/point.test.js +++ b/test/spec/ol/geom/point.test.js @@ -215,4 +215,22 @@ describe('ol.geom.Point', function() { }); + describe('#containsXY()', function() { + + it('does contain XY', function() { + const point = new Point([1, 2]); + + expect(point.containsXY(1, 2)).to.be(true); + }); + + it('does not contain XY', function() { + const point = new Point([1, 2]); + + expect(point.containsXY(1, 3)).to.be(false); + expect(point.containsXY(2, 2)).to.be(false); + expect(point.containsXY(2, 3)).to.be(false); + }); + + }); + });