SHAPELY_LINESTRING
Creates a LineString geometry from a 2D coordinate table where each row contains an (x,y) pair.
A line string is an ordered polyline made of connected linear segments. Its total length is the sum of segment lengths:
L = \sum_{i=1}^{n-1} \lVert p_{i+1} - p_i \rVert_2
The function returns an Excel data type with WKT as the primary value and line length as a property.
Excel Usage
=SHAPELY_LINESTRING(points)
points(list[list], required): List of [x, y] coordinates (e.g., [[0,0], [1,1]]).
Returns (dict): Excel Entity representing the line string (WKT).
Example 1: Simple Line
Inputs:
| points | |
|---|---|
| 0 | 0 |
| 1 | 1 |
Excel formula:
=SHAPELY_LINESTRING({0,0;1,1})
Expected output:
{"type":"String","basicValue":"LINESTRING (0 0, 1 1)","properties":{"Length":{"type":"Double","basicValue":1.41421},"WKT":{"type":"String","basicValue":"LINESTRING (0 0, 1 1)"}}}
Example 2: Multi-segment Line
Inputs:
| points | |
|---|---|
| 0 | 0 |
| 10 | 0 |
| 10 | 10 |
Excel formula:
=SHAPELY_LINESTRING({0,0;10,0;10,10})
Expected output:
{"type":"String","basicValue":"LINESTRING (0 0, 10 0, 10 10)","properties":{"Length":{"type":"Double","basicValue":20},"WKT":{"type":"String","basicValue":"LINESTRING (0 0, 10 0, 10 10)"}}}
Example 3: Descending line segment
Inputs:
| points | |
|---|---|
| 5 | 5 |
| 1 | 1 |
Excel formula:
=SHAPELY_LINESTRING({5,5;1,1})
Expected output:
{"type":"String","basicValue":"LINESTRING (5 5, 1 1)","properties":{"Length":{"type":"Double","basicValue":5.65685},"WKT":{"type":"String","basicValue":"LINESTRING (5 5, 1 1)"}}}
Example 4: Polyline with four points
Inputs:
| points | |
|---|---|
| 0 | 0 |
| 2 | 1 |
| 4 | 1 |
| 6 | 0 |
Excel formula:
=SHAPELY_LINESTRING({0,0;2,1;4,1;6,0})
Expected output:
{"type":"String","basicValue":"LINESTRING (0 0, 2 1, 4 1, 6 0)","properties":{"Length":{"type":"Double","basicValue":6.47214},"WKT":{"type":"String","basicValue":"LINESTRING (0 0, 2 1, 4 1, 6 0)"}}}
Python Code
from shapely import LineString
def shapely_linestring(points):
"""
Create a geometric line string from a list of points.
See: https://shapely.readthedocs.io/en/stable/reference/shapely.LineString.html
This example function is provided as-is without any representation of accuracy.
Args:
points (list[list]): List of [x, y] coordinates (e.g., [[0,0], [1,1]]).
Returns:
dict: Excel Entity representing the line string (WKT).
"""
try:
if not isinstance(points, list):
return "Error: points must be a list of coordinates"
clean_points = []
for row in points:
if isinstance(row, list) and len(row) >= 2:
try:
clean_points.append((float(row[0]), float(row[1])))
except (TypeError, ValueError):
continue
if len(clean_points) < 2:
return "Error: LineString requires at least 2 points"
line = LineString(clean_points)
wkt_str = line.wkt
return {
"type": "String",
"basicValue": wkt_str,
"properties": {
"Length": {"type": "Double", "basicValue": float(line.length)},
"WKT": {"type": "String", "basicValue": wkt_str}
}
}
except Exception as e:
return f"Error: {str(e)}"