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
indices
are given (see description below), this include the outer shells only. Theholes
argument 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 unlessout
is provided, in which case the original value inout
is kept.- outndarray, optional
An array (with dtype object) to output the geometries into.
- **kwargs
See NumPy ufunc docs for other keyword arguments. Ignored if
indices
is provided.
Examples
Polygons are constructed from rings:
>>> ring_1 = linearrings([[0, 0], [0, 10], [10, 10], [10, 0]]) >>> ring_2 = linearrings([[2, 6], [2, 7], [3, 7], [3, 6]]) >>> polygons([ring_1, ring_2])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))> >>> polygons([ring_1, ring_2])[1] <POLYGON ((2 6, 2 7, 3 7, 3 6, 2 6))>
Or from coordinates directly:
>>> 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
holes
keyword argument:>>> 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
indices
argument:>>> polygons([ring_1, ring_2], indices=[0, 1])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))> >>> polygons([ring_1, ring_2], indices=[0, 1])[1] <POLYGON ((2 6, 2 7, 3 7, 3 6, 2 6))> >>> 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:>>> polygons(None) <POLYGON EMPTY> >>> polygons(ring_1, holes=[None]) <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))> >>> polygons([ring_1, None], indices=[0, 0])[0] <POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))>