SHAPELY_DISTANCE
Computes the minimum Cartesian distance between two geometries provided in WKT format.
The distance corresponds to the minimum point-to-point separation:
d(A,B) = \min_{a\in A,\, b\in B} \lVert a-b \rVert_2
Intersecting or overlapping geometries yield a distance of 0.
Excel Usage
=SHAPELY_DISTANCE(geom_one, geom_two)
geom_one(str, required): First geometry (WKT).geom_two(str, required): Second geometry (WKT).
Returns (float): Minimum distance.
Example 1: Distance between two points
Inputs:
| geom_one | geom_two |
|---|---|
| POINT (0 0) | POINT (3 4) |
Excel formula:
=SHAPELY_DISTANCE("POINT (0 0)", "POINT (3 4)")
Expected output:
5
Example 2: Distance between point and line
Inputs:
| geom_one | geom_two |
|---|---|
| POINT (0 0) | LINESTRING (10 0, 10 10) |
Excel formula:
=SHAPELY_DISTANCE("POINT (0 0)", "LINESTRING (10 0, 10 10)")
Expected output:
10
Example 3: Distance between overlapping polygons
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_DISTANCE("POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))", "POLYGON ((1 1, 3 1, 3 3, 1 3, 1 1))")
Expected output:
0
Example 4: Distance between parallel lines
Inputs:
| geom_one | geom_two |
|---|---|
| LINESTRING (0 0, 4 0) | LINESTRING (0 3, 4 3) |
Excel formula:
=SHAPELY_DISTANCE("LINESTRING (0 0, 4 0)", "LINESTRING (0 3, 4 3)")
Expected output:
3
Python Code
from shapely import wkt
def shapely_distance(geom_one, geom_two):
"""
Calculate the minimum distance between two geometries.
See: https://shapely.readthedocs.io/en/stable/reference/shapely.distance.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:
float: Minimum distance.
"""
try:
if not geom_one or not geom_two:
return "Error: Both geometries must be provided"
g_one = wkt.loads(geom_one)
g_two = wkt.loads(geom_two)
return float(g_one.distance(g_two))
except Exception as e:
return f"Error: {str(e)}"Online Calculator
First geometry (WKT).
Second geometry (WKT).