SHAPELY_CONVEX_HULL

Computes the convex hull of an input geometry provided as WKT. The convex hull is the smallest convex geometry that encloses all points of the original geometry.

In optimization terms, it is the minimal convex set H such that:

G \subseteq H

The function returns the hull WKT as the primary value along with area and perimeter metadata.

Excel Usage

=SHAPELY_CONVEX_HULL(geometry)
  • geometry (str, required): Input geometry (WKT).

Returns (dict): Excel Entity representing the convex hull.

Example 1: Convex hull of points

Inputs:

geometry
MULTIPOINT (0 0, 10 0, 10 10, 0 10, 5 5)

Excel formula:

=SHAPELY_CONVEX_HULL("MULTIPOINT (0 0, 10 0, 10 10, 0 10, 5 5)")

Expected output:

{"type":"String","basicValue":"POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))","properties":{"Area":{"type":"Double","basicValue":100},"Perimeter":{"type":"Double","basicValue":40},"WKT":{"type":"String","basicValue":"POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))"}}}

Example 2: Convex hull of a line

Inputs:

geometry
LINESTRING (0 0, 3 0, 6 0)

Excel formula:

=SHAPELY_CONVEX_HULL("LINESTRING (0 0, 3 0, 6 0)")

Expected output:

{"type":"String","basicValue":"LINESTRING (0 0, 6 0)","properties":{"Area":{"type":"Double","basicValue":0},"Perimeter":{"type":"Double","basicValue":6},"WKT":{"type":"String","basicValue":"LINESTRING (0 0, 6 0)"}}}

Example 3: Convex hull of concave polygon

Inputs:

geometry
POLYGON ((0 0, 4 0, 4 4, 2 2, 0 4, 0 0))

Excel formula:

=SHAPELY_CONVEX_HULL("POLYGON ((0 0, 4 0, 4 4, 2 2, 0 4, 0 0))")

Expected output:

{"type":"String","basicValue":"POLYGON ((0 0, 0 4, 4 4, 4 0, 0 0))","properties":{"Area":{"type":"Double","basicValue":16},"Perimeter":{"type":"Double","basicValue":16},"WKT":{"type":"String","basicValue":"POLYGON ((0 0, 0 4, 4 4, 4 0, 0 0))"}}}

Example 4: Convex hull of a point

Inputs:

geometry
POINT (2 3)

Excel formula:

=SHAPELY_CONVEX_HULL("POINT (2 3)")

Expected output:

{"type":"String","basicValue":"POINT (2 3)","properties":{"Area":{"type":"Double","basicValue":0},"Perimeter":{"type":"Double","basicValue":0},"WKT":{"type":"String","basicValue":"POINT (2 3)"}}}

Python Code

from shapely import wkt

def shapely_convex_hull(geometry):
    """
    Returns the smallest convex polygon that contains all the points in the object.

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

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

    Args:
        geometry (str): Input geometry (WKT).

    Returns:
        dict: Excel Entity representing the convex hull.
    """
    try:
        if not geometry:
            return "Error: Geometry required"

        geom = wkt.loads(geometry)
        hull = geom.convex_hull
        wkt_str = hull.wkt

        return {
            "type": "String",
            "basicValue": wkt_str,
            "properties": {
              "Area": {"type": "Double", "basicValue": float(hull.area)},
              "Perimeter": {"type": "Double", "basicValue": float(hull.length)},
              "WKT": {"type": "String", "basicValue": wkt_str}
            }
        }
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Input geometry (WKT).