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:
@@ -91,22 +91,24 @@ ol.geom.Polygon.prototype.getType = function() {
|
||||
|
||||
|
||||
/**
|
||||
* Check whether a given coordinate is inside this polygon.
|
||||
* Check whether a given coordinate is inside this polygon. Note that this is a
|
||||
* fast and simple check - points on an edge or vertex of the polygon or one of
|
||||
* its inner rings are either classified inside or outside.
|
||||
*
|
||||
* @param {ol.Coordinate} coordinate Coordinate.
|
||||
* @return {boolean} Whether the coordinate is inside the polygon.
|
||||
*/
|
||||
ol.geom.Polygon.prototype.containsCoordinate = function(coordinate) {
|
||||
var rings = this.rings;
|
||||
var containsCoordinate = ol.geom.pointInPolygon(coordinate,
|
||||
rings[0].getCoordinates());
|
||||
if (containsCoordinate) {
|
||||
// if inner ring contains point, polygon does not contain it
|
||||
for (var i = 1, ii = rings.length; i < ii; ++i) {
|
||||
if (ol.geom.pointInPolygon(coordinate, rings[i].getCoordinates())) {
|
||||
containsCoordinate = false;
|
||||
break;
|
||||
}
|
||||
var containsCoordinate;
|
||||
for (var i = 0, ii = rings.length; i < ii; ++i) {
|
||||
containsCoordinate = rings[i].containsCoordinate(coordinate);
|
||||
// if inner ring (i > 0) contains coordinate, polygon does not contain it
|
||||
if (i > 0) {
|
||||
containsCoordinate = !containsCoordinate;
|
||||
}
|
||||
if (!containsCoordinate) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return containsCoordinate;
|
||||
|
||||
Reference in New Issue
Block a user