SHAPELY_INTERSECT
Computes the geometric intersection between two WKT geometries, returning only the shared spatial portion.
Set-theoretically, the operation is:
A \cap B
The result may be empty or may have a different geometry type than either input. The function returns an Excel data type containing WKT and key metadata.
Excel Usage
=SHAPELY_INTERSECT(geom_one, geom_two)
geom_one(str, required): First geometry (WKT).geom_two(str, required): Second geometry (WKT).
Returns (dict): Excel Entity representing the intersection geometry.
Example 1: Intersection of overlapping squares
Inputs:
| geom_one | geom_two |
|---|---|
| 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_INTERSECT("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 2, 2 1, 1 1, 1 2, 2 2))","properties":{"WKT":{"type":"String","basicValue":"POLYGON ((2 2, 2 1, 1 1, 1 2, 2 2))"},"Is Empty":{"type":"Boolean","basicValue":false},"Geometry Type":{"type":"String","basicValue":"Polygon"},"Area":{"type":"Double","basicValue":1},"Length":{"type":"Double","basicValue":4}}}
Example 2: Disjoint geometries (empty intersection)
Inputs:
| geom_one | geom_two |
|---|---|
| POINT (0 0) | POINT (10 10) |
Excel formula:
=SHAPELY_INTERSECT("POINT (0 0)", "POINT (10 10)")
Expected output:
{"type":"String","basicValue":"POINT EMPTY","properties":{"WKT":{"type":"String","basicValue":"POINT EMPTY"},"Is Empty":{"type":"Boolean","basicValue":true},"Geometry Type":{"type":"String","basicValue":"Point"}}}
Example 3: Intersection of overlapping lines
Inputs:
| geom_one | geom_two |
|---|---|
| LINESTRING (0 0, 5 0) | LINESTRING (3 0, 8 0) |
Excel formula:
=SHAPELY_INTERSECT("LINESTRING (0 0, 5 0)", "LINESTRING (3 0, 8 0)")
Expected output:
{"type":"String","basicValue":"LINESTRING (3 0, 5 0)","properties":{"WKT":{"type":"String","basicValue":"LINESTRING (3 0, 5 0)"},"Is Empty":{"type":"Boolean","basicValue":false},"Geometry Type":{"type":"String","basicValue":"LineString"},"Area":{"type":"Double","basicValue":0},"Length":{"type":"Double","basicValue":2}}}
Example 4: Point intersection with polygon
Inputs:
| geom_one | geom_two |
|---|---|
| POINT (1 1) | POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0)) |
Excel formula:
=SHAPELY_INTERSECT("POINT (1 1)", "POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))")
Expected output:
{"type":"String","basicValue":"POINT (1 1)","properties":{"WKT":{"type":"String","basicValue":"POINT (1 1)"},"Is Empty":{"type":"Boolean","basicValue":false},"Geometry Type":{"type":"String","basicValue":"Point"},"Area":{"type":"Double","basicValue":0},"Length":{"type":"Double","basicValue":0}}}
Python Code
from shapely import wkt
def shapely_intersect(geom_one, geom_two):
"""
Returns a representation of the intersection of this object with another geometric object.
See: https://shapely.readthedocs.io/en/stable/reference/shapely.intersection.html
This example function is provided as-is without any representation of accuracy.
Args:
geom_one (str): First geometry (WKT).
geom_two (str): Second geometry (WKT).
Returns:
dict: Excel Entity representing the intersection geometry.
"""
try:
if not geom_one or not geom_two:
return "Error: Both geometries required"
g_one = wkt.loads(geom_one)
g_two = wkt.loads(geom_two)
result = g_one.intersection(g_two)
wkt_str = result.wkt
props = {
"WKT": {"type": "String", "basicValue": wkt_str},
"Is Empty": {"type": "Boolean", "basicValue": bool(result.is_empty)},
"Geometry Type": {"type": "String", "basicValue": result.geom_type}
}
if not result.is_empty:
props["Area"] = {"type": "Double", "basicValue": float(result.area)}
props["Length"] = {"type": "Double", "basicValue": float(result.length)}
return {
"type": "String",
"basicValue": wkt_str,
"properties": props
}
except Exception as e:
return f"Error: {str(e)}"