contains method

bool contains(
  1. Geometry g
)

Tests whether this geometry contains the argument geometry.

The contains predicate has the following equivalent definitions:

  • Every point of the other geometry is a point of this geometry, and the interiors of the two geometries have at least one point in common.
  • The DE-9IM Intersection Matrix for the two geometries matches the pattern [T*****FF*]
  • g.within(this) = true
    (contains is the converse of {@link #within} )
An implication of the definition is that "Geometries do not contain their boundary". In other words, if a geometry A is a subset of the points in the boundary of a geometry B, B.contains(A) = false. (As a concrete example, take A to be a LineString which lies in the boundary of a Polygon B.) For a predicate with similar behaviour but avoiding this subtle limitation, see {@link #covers}.

@param g the Geometry with which to compare this Geometry @return true if this Geometry contains g

@see Geometry#within @see Geometry#covers

Implementation

bool contains(Geometry g) {
  // optimization - lower dimension cannot contain areas
  if (g.getDimension() == 2 && getDimension() < 2) {
    return false;
  }
  // optimization - P cannot contain a non-zero-length L
  // Note that a point can contain a zero-length lineal geometry,
  // since the line has no boundary due to Mod-2 Boundary Rule
  if (g.getDimension() == 1 && getDimension() < 1 && g.getLength() > 0.0) {
    return false;
  }
  // optimization - envelope test
  if (!getEnvelopeInternal().containsEnvelope(g.getEnvelopeInternal()))
    return false;
  // optimization for rectangle arguments
  if (isRectangle()) {
    return RectangleContains.containsStatic(this as Polygon, g);
  }
  // general case
  return relate(g).isContains();
}