Point-in-polygon improvements

As suggested by @tschaub in #674, geom.pointInPoly is not needed
if we have geom.LinearRing#containsCoordinate. This pull request
also adds tests and documentation on the limitations of the
containsCoordinate method.

I think for now it is ok to keep geometry/topology functions as
simple as possible in ol3. If we decide to not rely on third
party libraries like jsts for topology operations, we can always
refine what we have and e.g. port topology operations over from
ol2.
This commit is contained in:
ahocevar
2013-05-07 10:49:51 +02:00
parent aa3cc0cec4
commit 666a010e3b
5 changed files with 109 additions and 39 deletions

View File

@@ -40,6 +40,72 @@ describe('ol.geom.LinearRing', function() {
});
describe('#containsCoordinate()', function() {
it('knows when a point coordinate is inside a ring', function() {
/**
* The ring:
* edge 3
* (5, 10) __________ (15, 10)
* / /
* edge 4 / / edge 2
* / /
* (0, 0) /_________/ (10, 0)
* edge 1
*/
var ring = new ol.geom.LinearRing(
[[0, 0], [10, 0], [15, 10], [5, 10]]);
// contains: 1 (touches - not implemented), true (within), false (outside)
var cases = [{
point: [5, 5], contains: true
}, {
point: [20, 20], contains: false
}, {
point: [15, 15], contains: false
}/*, {
point: [0, 0], contains: 1 // lower left corner
}, {
point: [10, 0], contains: 1 // lower right corner
}, {
point: [15, 10], contains: 1 // upper right corner
}, {
point: [5, 10], contains: 1 // upper left corner
}, {
point: [5, 0], contains: 1 // on edge 1
}*/, {
point: [5, -0.1], contains: false // below edge 1
}, {
point: [5, 0.1], contains: true // above edge 1
}/*, {
point: [12.5, 5], contains: 1 // on edge 2
}*/, {
point: [12.4, 5], contains: true // left of edge 2
}, {
point: [12.6, 5], contains: false // right of edge 2
}/*, {
point: [10, 10], contains: 1 // on edge 3
}*/, {
point: [10, 9.9], contains: true // below edge 3
}, {
point: [10, 10.1], contains: false // above edge 3
}/*, {
point: [2.5, 5], contains: 1 // on edge 4
}*/, {
point: [2.4, 5], contains: false // left of edge 4
}, {
point: [2.6, 5], contains: true // right of edge 4
}];
var len = cases.length;
var c;
for (var i=0; i<len; ++i) {
c = cases[i];
expect(ring.containsCoordinate(c.point)).to.be(c.contains);
}
});
});
});
goog.require('ol.geom.LinearRing');