shapely.get_coordinates

shapely.get_coordinates#

get_coordinates(geometry, include_z=False, return_index=False, *, include_m=False)#

Get coordinates from a geometry array as an array of floats.

The shape of the returned array is (N, 2), with N being the number of coordinate pairs. The shape of the data may also be (N, 3) or (N, 4), depending on include_z and include_m options.

Parameters:
geometryGeometry or array_like

Geometry or geometries to get the coordinates of.

include_z, include_mbool, default False

If both are False, return XY (2D) geometries. If both are True, return XYZM (4D) geometries. If either are True, return XYZ or XYM (3D) geometries. If a geometry has no Z or M dimension, extra coordinate data will be NaN.

Added in version 2.1.0: The include_m parameter was added to support XYM (3D) and XYZM (4D) geometries available with GEOS 3.12.0 or later. With older GEOS versions, M dimension coordinates will be NaN.

return_indexbool, default False

If True, also return the index of each returned geometry as a separate ndarray of integers. For multidimensional arrays, this indexes into the flattened array (in C contiguous order).

Notes

Deprecated since version 2.1.0: A deprecation warning is shown if include_z or return_index are specified as positional arguments. In a future release, these will need to be specified as keyword arguments.

Examples

>>> import shapely
>>> from shapely import LineString, Point
>>> shapely.get_coordinates(Point(1, 2)).tolist()
[[1.0, 2.0]]
>>> shapely.get_coordinates(LineString([(2, 2), (4, 4)])).tolist()
[[2.0, 2.0], [4.0, 4.0]]
>>> shapely.get_coordinates(None)
array([], shape=(0, 2), dtype=float64)

By default the third dimension is ignored:

>>> shapely.get_coordinates(Point(1, 2, 3)).tolist()
[[1.0, 2.0]]
>>> shapely.get_coordinates(Point(1, 2, 3), include_z=True).tolist()
[[1.0, 2.0, 3.0]]

If geometries don’t have Z or M dimension, these values will be NaN:

>>> pt = Point(1, 2)
>>> shapely.get_coordinates(pt, include_z=True).tolist()
[[1.0, 2.0, nan]]
>>> shapely.get_coordinates(pt, include_z=True, include_m=True).tolist()
[[1.0, 2.0, nan, nan]]

When return_index=True, indexes are returned also:

>>> geometries = [LineString([(2, 2), (4, 4)]), Point(0, 0)]
>>> coordinates, index = shapely.get_coordinates(geometries, return_index=True)
>>> coordinates.tolist(), index.tolist()
([[2.0, 2.0], [4.0, 4.0], [0.0, 0.0]], [0, 0, 1])