shapely.polygons#
- polygons(geometries, holes=None, indices=None, *, out=None, **kwargs)#
Create an array of polygons.
- Parameters:
- geometriesarray_like
An array of linearrings or coordinates (see linearrings). Unless
indicesare given (see description below), this include the outer shells only. Theholesargument should be used to create polygons with holes.- holesarray_like, optional
An array of lists of linearrings that constitute holes for each shell. Not to be used in combination with
indices.- indicesarray_like, optional
Indices into the target array where input geometries belong. If provided, the holes are expected to be present inside
geometries; the first geometry for each index is the outer shell and all subsequent geometries in that index are the holes. Both geometries and indices should be 1D and have matching sizes. Indices should be in increasing order. Missing indices result in a ValueError unlessoutis provided, in which case the original value inoutis kept.- outndarray, optional
An array (with dtype object) to output the geometries into.
- **kwargs
See NumPy ufunc docs for other keyword arguments. Ignored if
indicesis provided.
Notes
Deprecated since version 2.1.0: A deprecation warning is shown if
indicesis specified as a positional argument. This will need to be specified as a keyword argument in a future release.Examples
>>> import shapely
Polygons are constructed from rings:
>>> ring_1 = shapely.linearrings([[0, 0], [0, 10], [10, 10], [10, 0]]) >>> ring_2 = shapely.linearrings([[2, 6], [2, 7], [3, 7], [3, 6]]) >>> shapely.polygons([ring_1, ring_2])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))> >>> shapely.polygons([ring_1, ring_2])[1] <POLYGON ((2 6, 2 7, 3 7, 3 6, 2 6))>
Or from coordinates directly:
>>> shapely.polygons([[0, 0], [0, 10], [10, 10], [10, 0]]) <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))>
Adding holes can be done using the
holeskeyword argument:>>> shapely.polygons(ring_1, holes=[ring_2]) <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (2 6, 2 7, 3 7, 3 6, 2 6))>
Or using the
indicesargument:>>> shapely.polygons([ring_1, ring_2], indices=[0, 1])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))> >>> shapely.polygons([ring_1, ring_2], indices=[0, 1])[1] <POLYGON ((2 6, 2 7, 3 7, 3 6, 2 6))> >>> shapely.polygons([ring_1, ring_2], indices=[0, 0])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (2 6, 2 7, 3 7, 3 6, 2 6))>
Missing input values (
None) are skipped and may result in an empty polygon:>>> shapely.polygons(None) <POLYGON EMPTY> >>> shapely.polygons(ring_1, holes=[None]) <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))> >>> shapely.polygons([ring_1, None], indices=[0, 0])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))>