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:
@@ -33,3 +33,33 @@ goog.inherits(ol.geom.LinearRing, ol.geom.LineString);
|
||||
ol.geom.LinearRing.prototype.getType = function() {
|
||||
return ol.geom.GeometryType.LINEARRING;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Check whether a given coordinate is inside this ring. Note that this is a
|
||||
* fast and simple check - points on an edge or vertex of the ring are either
|
||||
* classified inside or outside.
|
||||
*
|
||||
* @param {ol.Coordinate} coordinate Coordinate.
|
||||
* @return {boolean} Whether the coordinate is inside the ring.
|
||||
*/
|
||||
ol.geom.LinearRing.prototype.containsCoordinate = function(coordinate) {
|
||||
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
|
||||
var x = coordinate[0], y = coordinate[1];
|
||||
var vertices = this.getCoordinates();
|
||||
var inside = false;
|
||||
var xi, yi, xj, yj, intersect;
|
||||
var numVertices = vertices.length;
|
||||
for (var i = 0, j = numVertices - 1; i < numVertices; j = i++) {
|
||||
xi = vertices[i][0];
|
||||
yi = vertices[i][1];
|
||||
xj = vertices[j][0];
|
||||
yj = vertices[j][1];
|
||||
intersect = ((yi > y) != (yj > y)) &&
|
||||
(x < (xj - xi) * (y - yi) / (yj - yi) + xi);
|
||||
if (intersect) {
|
||||
inside = !inside;
|
||||
}
|
||||
}
|
||||
return inside;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user