locatePointInPolygon static method

int locatePointInPolygon(
  1. Coordinate p,
  2. Polygon poly
)

Determines the {@link Location} of a point in a {@link Polygon}. The return value is one of:

  • {@link Location.INTERIOR} if the point is in the geometry interior
  • {@link Location.BOUNDARY} if the point lies exactly on the boundary
  • {@link Location.EXTERIOR} if the point is outside the geometry

This method is provided for backwards compatibility only. Use {@link #locate(Coordinate, Geometry)} instead.

@param p the point to test @param poly the geometry to test @return the Location of the point in the polygon

Implementation

static int locatePointInPolygon(Coordinate p, Polygon poly) {
  if (poly.isEmpty()) return Location.EXTERIOR;
  LinearRing shell = poly.getExteriorRing();
  int shellLoc = locatePointInRing(p, shell);
  if (shellLoc != Location.INTERIOR) return shellLoc;

  // now test if the point lies in or on the holes
  for (int i = 0; i < poly.getNumInteriorRing(); i++) {
    LinearRing hole = poly.getInteriorRingN(i);
    int holeLoc = locatePointInRing(p, hole);
    if (holeLoc == Location.BOUNDARY) return Location.BOUNDARY;
    if (holeLoc == Location.INTERIOR) return Location.EXTERIOR;
    // if in EXTERIOR of this hole keep checking the other ones
  }
  // If not in any hole must be inside polygon
  return Location.INTERIOR;
}