shapely.get_segments

Contents

shapely.get_segments#

get_segments(geometry, *, include_z=False, return_index=False, **kwargs)#

Get segments of each input linear geometry object.

Here ‘segments’ is defined as the individual pairwise coordinates comprising a LineString or LinearRing. Multi* geometry objects are not supported.

Parameters:
geometryGeometry or array_like

A single linear object or collection of linear objects.

include_zbool, default False

If True, return LINESTRING Z (3D) geometries.

return_indexbool, default False

If True, also return the index of each returned geometry as a separate ndarray of integers.

**kwargsdict

Keyword arguments to pass into shapely.linestrings().

Returns:
ndarray of constituent pairwise segments.

Examples

>>> from shapely import get_segments
>>> from shapely import LineString, LinearRing

Return the 2 constituent pairwise segments of a 3-coordinate linestring.

>>> get_segments(LineString(([0, 0], [1, 1], [2, 2]))).tolist()
[<LINESTRING (0 0, 1 1)>, <LINESTRING (1 1, 2 2)>]

Return the 3 constituent pairwise segments of a 4-coordinate linearring.

>>> get_segments(LinearRing(([0, 0], [1, 1], [2, 2], [0,0]))).tolist()
[<LINESTRING (0 0, 1 1)>, <LINESTRING (1 1, 2 2)>, <LINESTRING (2 2, 0 0)>]

When return_index=True, indexes are returned also:

>>> segments, index = get_segments(
...      [
...          LineString(([0, 0], [1, 1], [2, 2])),
...          LinearRing(([0, 0], [1, 1], [2, 2], [0,0])),
...      ],
...      return_index=True,
... )
>>> segments.tolist(), index.tolist()
([<LINESTRING (0 0, 1 1)>,
  <LINESTRING (1 1, 2 2)>,
  <LINESTRING (0 0, 1 1)>,
  <LINESTRING (1 1, 2 2)>,
  <LINESTRING (2 2, 0 0)>],
 [0, 0, 1, 1, 1])

By default the third dimension (Z) is ignored.

>>> get_segments(LineString(([0, 0, 1], [1, 1, 1], [2, 2, 1]))).tolist()
[<LINESTRING (0 0, 1 1)>, <LINESTRING (1 1, 2 2)>]
>>> get_segments(
...     LineString(([0, 0, 1], [1, 1, 1], [2, 2, 1])), include_z=True,
... ).tolist()
[<LINESTRING Z (0 0 1, 1 1 1)>, <LINESTRING Z (1 1 1, 2 2 1)>]

If geometries don’t have a Z dimension, these values will be NaN.

>>> get_segments(LineString(([0, 0], [1, 1], [2, 2])), include_z=True).tolist()
[<LINESTRING Z (0 0 NaN, 1 1 NaN)>, <LINESTRING Z (1 1 NaN, 2 2 NaN)>]