SHAPELY_UNION_ALL
Computes the geometric union of multiple WKT geometries, combining overlapping or adjacent regions into a single resulting geometry where applicable.
The operation is the set union across all input geometries:
G = \bigcup_{i=1}^{n} G_i
The function returns an Excel data type whose primary value is the union WKT, plus area and boundary length metadata.
Excel Usage
=SHAPELY_UNION_ALL(geometries)
geometries(list[list], required): List of WKT strings.
Returns (dict): Excel Entity representing the unioned geometry.
Example 1: Union of two squares
Inputs:
| geometries |
|---|
| POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)),POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0)) |
Excel formula:
=SHAPELY_UNION_ALL(POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)),POLYGON ((1 0, 2 0, 2 1, 1 1, 1 0)))
Expected output:
{"type":"String","basicValue":"POLYGON ((0 0, 0 1, 1 1, 2 1, 2 0, 1 0, 0 0))","properties":{"Area":{"type":"Double","basicValue":2},"Length":{"type":"Double","basicValue":6},"WKT":{"type":"String","basicValue":"POLYGON ((0 0, 0 1, 1 1, 2 1, 2 0, 1 0, 0 0))"}}}
Example 2: Union of overlapping polygons
Inputs:
| geometries |
|---|
| POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0)),POLYGON ((1 1, 3 1, 3 3, 1 3, 1 1)) |
Excel formula:
=SHAPELY_UNION_ALL(POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0)),POLYGON ((1 1, 3 1, 3 3, 1 3, 1 1)))
Expected output:
{"type":"String","basicValue":"POLYGON ((2 0, 0 0, 0 2, 1 2, 1 3, 3 3, 3 1, 2 1, 2 0))","properties":{"Area":{"type":"Double","basicValue":7},"Length":{"type":"Double","basicValue":12},"WKT":{"type":"String","basicValue":"POLYGON ((2 0, 0 0, 0 2, 1 2, 1 3, 3 3, 3 1, 2 1, 2 0))"}}}
Example 3: Union of points
Inputs:
| geometries |
|---|
| POINT (0 0),POINT (1 1),POINT (2 2) |
Excel formula:
=SHAPELY_UNION_ALL(POINT (0 0),POINT (1 1),POINT (2 2))
Expected output:
{"type":"String","basicValue":"MULTIPOINT ((0 0), (1 1), (2 2))","properties":{"Area":{"type":"Double","basicValue":0},"Length":{"type":"Double","basicValue":0},"WKT":{"type":"String","basicValue":"MULTIPOINT ((0 0), (1 1), (2 2))"}}}
Example 4: Union of lines
Inputs:
| geometries |
|---|
| LINESTRING (0 0, 2 0),LINESTRING (2 0, 4 0) |
Excel formula:
=SHAPELY_UNION_ALL(LINESTRING (0 0, 2 0),LINESTRING (2 0, 4 0))
Expected output:
{"type":"String","basicValue":"MULTILINESTRING ((0 0, 2 0), (2 0, 4 0))","properties":{"Area":{"type":"Double","basicValue":0},"Length":{"type":"Double","basicValue":4},"WKT":{"type":"String","basicValue":"MULTILINESTRING ((0 0, 2 0), (2 0, 4 0))"}}}
Python Code
from shapely import wkt
from shapely.ops import unary_union
def shapely_union_all(geometries):
"""
Returns the union of all geometries in the input list.
See: https://shapely.readthedocs.io/en/stable/reference/shapely.unary_union.html
This example function is provided as-is without any representation of accuracy.
Args:
geometries (list[list]): List of WKT strings.
Returns:
dict: Excel Entity representing the unioned geometry.
"""
try:
# Normalize input list
def flatten(x):
if isinstance(x, list):
out = []
for item in x:
out.extend(flatten(item))
return out
return [x]
raw_list = flatten(geometries)
wkt_list = [str(s) for s in raw_list if s]
if not wkt_list:
return "Error: No geometries provided"
geoms = []
for s in wkt_list:
try:
geoms.append(wkt.loads(s))
except:
pass # Skip invalid WKT
if not geoms:
return "Error: No valid geometries found"
union_geom = unary_union(geoms)
wkt_str = union_geom.wkt
return {
"type": "String",
"basicValue": wkt_str,
"properties": {
"Area": {"type": "Double", "basicValue": float(union_geom.area)},
"Length": {"type": "Double", "basicValue": float(union_geom.length)},
"WKT": {"type": "String", "basicValue": wkt_str}
}
}
except Exception as e:
return f"Error: {str(e)}"