shapely.transform_coordseq

shapely.transform_coordseq#

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

Apply a transformation to the coordinate sequences of a single geometry.

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

This function differs with transform in the following ways:

  • It only accepts scalar Geometry objects, not arrays.

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

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

Parameters:
geomGeometry or None
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

Returns a copy of a geometry array with a function applied to its 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)>