SHAPELY_CONTAINS

Tests the spatial predicate “contains” between two geometries expressed as WKT. A geometry contains another if the second lies completely in the interior of the first, with no points in the exterior.

This predicate is directional and generally follows:

ext{contains}(A, B) \neq \text{contains}(B, A)

Boundary-only contact does not satisfy containment under this definition.

Excel Usage

=SHAPELY_CONTAINS(geom_one, geom_two)
  • geom_one (str, required): Container geometry (WKT).
  • geom_two (str, required): Content geometry (WKT).

Returns (bool): True if geom1 contains geom2.

Example 1: Point in Polygon

Inputs:

geom_one geom_two
POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0)) POINT (5 5)

Excel formula:

=SHAPELY_CONTAINS("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", "POINT (5 5)")

Expected output:

true

Example 2: Point outside Polygon

Inputs:

geom_one geom_two
POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0)) POINT (20 20)

Excel formula:

=SHAPELY_CONTAINS("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", "POINT (20 20)")

Expected output:

false

Example 3: Polygon contains inner line

Inputs:

geom_one geom_two
POLYGON ((0 0, 8 0, 8 8, 0 8, 0 0)) LINESTRING (1 1, 7 7)

Excel formula:

=SHAPELY_CONTAINS("POLYGON ((0 0, 8 0, 8 8, 0 8, 0 0))", "LINESTRING (1 1, 7 7)")

Expected output:

true

Example 4: Polygon does not contain boundary point

Inputs:

geom_one geom_two
POLYGON ((0 0, 8 0, 8 8, 0 8, 0 0)) POINT (0 0)

Excel formula:

=SHAPELY_CONTAINS("POLYGON ((0 0, 8 0, 8 8, 0 8, 0 0))", "POINT (0 0)")

Expected output:

false

Python Code

from shapely import wkt

def shapely_contains(geom_one, geom_two):
    """
    Returns True if the first geometry contains the second.

    See: https://shapely.readthedocs.io/en/stable/reference/shapely.contains.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        geom_one (str): Container geometry (WKT).
        geom_two (str): Content geometry (WKT).

    Returns:
        bool: True if geom1 contains geom2.
    """
    try:
        if not geom_one or not geom_two:
            return False

        g_one = wkt.loads(geom_one)
        g_two = wkt.loads(geom_two)

        return bool(g_one.contains(g_two))
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Container geometry (WKT).
Content geometry (WKT).