SHAPELY_BUFFER

Computes a buffer geometry around an input geometry from WKT using a specified buffer distance. Positive distance expands geometry and negative distance contracts it where possible.

Conceptually, the buffer is the Minkowski sum (or difference for negative distance) with a radius-d disk:

G \oplus B_d

The function returns an Excel data type with the buffered WKT as the primary value plus area and perimeter properties.

Excel Usage

=SHAPELY_BUFFER(geometry, distance, quad_segs)
  • geometry (str, required): Input geometry (WKT).
  • distance (float, required): The radius of the buffer.
  • quad_segs (int, optional, default: 2): Number of segments used to approximate a quarter circle.

Returns (dict): Excel Entity representing the buffered geometry (Polygon).

Example 1: Buffer around a point

Inputs:

geometry distance quad_segs
POINT (0 0) 1 1

Excel formula:

=SHAPELY_BUFFER("POINT (0 0)", 1, 1)

Expected output:

{"type":"String","basicValue":"POLYGON ((1 0, 0.0000000000000001 -1, -1 -0.0000000000000001, -0.0000000000000002 1, 1 0))","properties":{"Area":{"type":"Double","basicValue":2},"Perimeter":{"type":"Double","basicValue":5.65685},"WKT":{"type":"String","basicValue":"POLYGON ((1 0, 0.0000000000000001 -1, -1 -0.0000000000000001, -0.0000000000000002 1, 1 0))"}}}

Example 2: Buffer around a line

Inputs:

geometry distance quad_segs
LINESTRING (0 0, 10 0) 2 1

Excel formula:

=SHAPELY_BUFFER("LINESTRING (0 0, 10 0)", 2, 1)

Expected output:

{"type":"String","basicValue":"POLYGON ((10 2, 12 0, 10 -2, 0 -2, -2 0.0000000000000002, 0 2, 10 2))","properties":{"Area":{"type":"Double","basicValue":48},"Perimeter":{"type":"Double","basicValue":31.3137},"WKT":{"type":"String","basicValue":"POLYGON ((10 2, 12 0, 10 -2, 0 -2, -2 0.0000000000000002, 0 2, 10 2))"}}}

Example 3: Buffer around a polygon

Inputs:

geometry distance quad_segs
POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0)) 0.5 1

Excel formula:

=SHAPELY_BUFFER("POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))", 0.5, 1)

Expected output:

{"type":"String","basicValue":"POLYGON ((-0.5 0, -0.5 2, 0 2.5, 2 2.5, 2.5 2, 2.5 0, 2 -0.5, 0 -0.5, -0.5 0))","properties":{"Area":{"type":"Double","basicValue":8.5},"Perimeter":{"type":"Double","basicValue":10.8284},"WKT":{"type":"String","basicValue":"POLYGON ((-0.5 0, -0.5 2, 0 2.5, 2 2.5, 2.5 2, 2.5 0, 2 -0.5, 0 -0.5, -0.5 0))"}}}

Example 4: Negative buffer on polygon

Inputs:

geometry distance
POLYGON ((0 0, 6 0, 6 6, 0 6, 0 0)) -1

Excel formula:

=SHAPELY_BUFFER("POLYGON ((0 0, 6 0, 6 6, 0 6, 0 0))", -1)

Expected output:

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

Python Code

from shapely import wkt

def shapely_buffer(geometry, distance, quad_segs=2):
    """
    Returns a representation of all points within a given distance of the this geometric object.

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

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

    Args:
        geometry (str): Input geometry (WKT).
        distance (float): The radius of the buffer.
        quad_segs (int, optional): Number of segments used to approximate a quarter circle. Default is 2.

    Returns:
        dict: Excel Entity representing the buffered geometry (Polygon).
    """
    try:
        if not geometry:
            return "Error: Geometry required"

        geom = wkt.loads(geometry)
        buffered = geom.buffer(distance, quad_segs=quad_segs)
        wkt_str = buffered.wkt

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

Online Calculator

Input geometry (WKT).
The radius of the buffer.
Number of segments used to approximate a quarter circle.