shapely.transform_coordseq

shapely.transform_coordseq#

transform_coordseq(geometry, transformation, *, include_z: bool | None = False, interleaved: bool = True)#

Apply a transformation to the coordinate sequences of a geometry (array).

This function differs from transform in the following ways:

  • The transformation function is applied once per coordinate sequence. For polygons this means: per ring. For collections this means: per element.

  • The number of coordinate pairs per coordinate sequence is allowed to change.

The transform function is the more performant option, so we recommend using that function when not changing the number of coordinate pairs.

Added in version 2.2.0.

Parameters:
geometryGeometry or array_like

Geometry or geometries to transform.

transformationfunction

A function that transforms a (N, 2) or (N, 3) ndarray of float64 to another (N, 2) or (N, 3) ndarray of float64. The function may change the value of N.

include_zbool, optional, default False

If False, always return 2D geometries. If True, the data being passed to the transformation function will include the third dimension (if a geometry has no third dimension, the z-coordinates will be NaN). If None, will infer the dimensionality using has_z. Note that this inference can be unreliable with empty geometries or NaN coordinates: for a guaranteed result, it is recommended to specify include_z explicitly.

interleavedbool, default True

If set to False, the transformation function should accept 2 or 3 separate one-dimensional coordinate arrays as arguments (x, y and optional z) instead of a single one. The return value must be a tuple of (x, y and optional z).

See also

has_z

Return True if a geometry has Z coordinates.

transform

Transform arrays of Geometry objects.

Examples

>>> import shapely
>>> from shapely import LineString, Point

Reduce a linestring to only its first 2 points:

>>> shapely.transform_coordseq(LineString([(2, 2), (4, 4), (6, 6)]),     lambda coords: coords[:2])
<LINESTRING (2 2, 4 4)>

The same with a function that accepts separate x, y arrays:

>>> shapely.transform_coordseq(LineString([(2, 2), (4, 4), (6, 6)]),     lambda x, y: (x[:2], y[:2]), interleaved=False)
<LINESTRING (2 2, 4 4)>