shapely.Polygon#
- class Polygon(shell=None, holes=None)#
A geometry type representing an area that is enclosed by a linear ring.
A polygon is a two-dimensional feature and has a non-zero area. It may have one or more negative-space “holes” which are also bounded by linear rings. If any rings cross each other, the feature is invalid and operations on it may fail.
- Parameters:
- shellsequence
A sequence of (x, y [,z]) numeric coordinate pairs or triples, or an array-like with shape (N, 2) or (N, 3). Also can be a sequence of Point objects.
- holessequence
A sequence of objects which satisfy the same requirements as the shell parameters above
Examples
Create a square polygon with no holes
>>> coords = ((0., 0.), (0., 1.), (1., 1.), (1., 0.), (0., 0.)) >>> polygon = Polygon(coords) >>> polygon.area 1.0
- Attributes:
- almost_equals(other, decimal=6)#
Return True if all coordinates are equal to a specified decimal place.
Deprecated since version 1.8.0: The ‘almost_equals()’ method is deprecated and will be removed in Shapely 2.1 because the name is confusing. The ‘equals_exact()’ method should be used instead.
Refers to approximate coordinate equality, which requires coordinates to be approximately equal and in the same order for all components of a geometry.
Because of this it is possible for “equals()” to be True for two geometries and “almost_equals()” to be False.
- Returns:
- bool
Examples
>>> LineString( ... [(0, 0), (2, 2)] ... ).equals_exact( ... LineString([(0, 0), (1, 1), (2, 2)]), ... 1e-6 ... ) False
- property area#
Unitless area of the geometry (float).
- property boundary#
Return a lower dimension geometry that bounds the object.
The boundary of a polygon is a line, the boundary of a line is a collection of points. The boundary of a point is an empty (null) collection.
- property bounds#
Return minimum bounding region (minx, miny, maxx, maxy).
- buffer(distance, quad_segs=16, cap_style='round', join_style='round', mitre_limit=5.0, single_sided=False, **kwargs)#
Get a geometry that represents all points within a distance of this geometry.
A positive distance produces a dilation, a negative distance an erosion. A very small or zero distance may sometimes be used to “tidy” a polygon.
- Parameters:
- distancefloat
The distance to buffer around the object.
- resolutionint, optional
The resolution of the buffer around each vertex of the object.
- quad_segsint, optional
Sets the number of line segments used to approximate an angle fillet.
- cap_styleshapely.BufferCapStyle or {‘round’, ‘square’, ‘flat’}, default ‘round’
Specifies the shape of buffered line endings. BufferCapStyle.round (‘round’) results in circular line endings (see
quad_segs
). Both BufferCapStyle.square (‘square’) and BufferCapStyle.flat (‘flat’) result in rectangular line endings, only BufferCapStyle.flat (‘flat’) will end at the original vertex, while BufferCapStyle.square (‘square’) involves adding the buffer width.- join_styleshapely.BufferJoinStyle or {‘round’, ‘mitre’, ‘bevel’}, default ‘round’
Specifies the shape of buffered line midpoints. BufferJoinStyle.ROUND (‘round’) results in rounded shapes. BufferJoinStyle.bevel (‘bevel’) results in a beveled edge that touches the original vertex. BufferJoinStyle.mitre (‘mitre’) results in a single vertex that is beveled depending on the
mitre_limit
parameter.- mitre_limitfloat, optional
The mitre limit ratio is used for very sharp corners. The mitre ratio is the ratio of the distance from the corner to the end of the mitred offset corner. When two line segments meet at a sharp angle, a miter join will extend the original geometry. To prevent unreasonable geometry, the mitre limit allows controlling the maximum length of the join corner. Corners with a ratio which exceed the limit will be beveled.
- single_sidedbool, optional
The side used is determined by the sign of the buffer distance:
a positive distance indicates the left-hand side a negative distance indicates the right-hand side
The single-sided buffer of point geometries is the same as the regular buffer. The End Cap Style for single-sided buffers is always ignored, and forced to the equivalent of CAP_FLAT.
- quadsegsint, optional
Deprecated alias for quad_segs.
- **kwargsdict, optional
For backwards compatibility of renamed parameters. If an unsupported kwarg is passed, a ValueError will be raised.
- Returns:
- Geometry
Notes
The return value is a strictly two-dimensional geometry. All Z coordinates of the original geometry will be ignored.
Examples
>>> from shapely.wkt import loads >>> g = loads('POINT (0.0 0.0)')
16-gon approx of a unit radius circle:
>>> g.buffer(1.0).area 3.1365484905459...
128-gon approximation:
>>> g.buffer(1.0, 128).area 3.141513801144...
triangle approximation:
>>> g.buffer(1.0, 3).area 3.0 >>> list(g.buffer(1.0, cap_style=BufferCapStyle.square).exterior.coords) [(1.0, 1.0), (1.0, -1.0), (-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0)] >>> g.buffer(1.0, cap_style=BufferCapStyle.square).area 4.0
- property centroid#
Return the geometric center of the object.
- contains(other)#
Return True if the geometry contains the other, else False.
- contains_properly(other)#
Return True if the geometry completely contains the other.
There should be no common boundary points.
Refer to shapely.contains_properly for full documentation.
- property convex_hull#
Return the convex hull of the geometry.
Imagine an elastic band stretched around the geometry: that’s a convex hull, more or less.
The convex hull of a three member multipoint, for example, is a triangular polygon.
- property coords#
Not implemented for polygons.
- covered_by(other)#
Return True if the geometry is covered by the other, else False.
- covers(other)#
Return True if the geometry covers the other, else False.
- crosses(other)#
Return True if the geometries cross, else False.
- difference(other, grid_size=None)#
Return the difference of the geometries.
Refer to shapely.difference for full documentation.
- disjoint(other)#
Return True if geometries are disjoint, else False.
- distance(other)#
Unitless distance to other geometry (float).
- dwithin(other, distance)#
Return True if geometry is within a given distance from the other.
Refer to shapely.dwithin for full documentation.
- property envelope#
A figure that envelopes the geometry.
- equals(other)#
Return True if geometries are equal, else False.
This method considers point-set equality (or topological equality), and is equivalent to (self.within(other) & self.contains(other)).
- Returns:
- bool
Examples
>>> LineString( ... [(0, 0), (2, 2)] ... ).equals( ... LineString([(0, 0), (1, 1), (2, 2)]) ... ) True
- equals_exact(other, tolerance=0.0, normalize=False)#
Return True if the geometries are equivalent within the tolerance.
Refer to
equals_exact()
for full documentation.- Parameters:
- otherBaseGeometry
The other geometry object in this comparison.
- tolerancefloat, optional (default: 0.)
Absolute tolerance in the same units as coordinates.
- normalizebool, optional (default: False)
If True, normalize the two geometries so that the coordinates are in the same order.
Added in version 2.1.0.
- Returns:
- bool
Examples
>>> LineString( ... [(0, 0), (2, 2)] ... ).equals_exact( ... LineString([(0, 0), (1, 1), (2, 2)]), ... 1e-6 ... ) False
- property exterior#
Return the exterior ring of the polygon.
- classmethod from_bounds(xmin, ymin, xmax, ymax)#
Construct a Polygon() from spatial bounds.
- property geom_type#
Name of the geometry’s type, such as ‘Point’.
- geometryType()#
Get the geometry type (deprecated).
Deprecated since version 2.0: Use the
geom_type
attribute instead.
- property has_m#
True if the geometry’s coordinate sequence(s) have m values.
- property has_z#
True if the geometry’s coordinate sequence(s) have z values.
- hausdorff_distance(other)#
Unitless hausdorff distance to other geometry (float).
- property interiors#
Return the sequence of interior rings of the polygon.
- interpolate(distance, normalized=False)#
Return a point at the specified distance along a linear geometry.
Negative length values are taken as measured in the reverse direction from the end of the geometry. Out-of-range index values are handled by clamping them to the valid range of values. If the normalized arg is True, the distance will be interpreted as a fraction of the geometry’s length.
Alias of line_interpolate_point.
- intersection(other, grid_size=None)#
Return the intersection of the geometries.
Refer to shapely.intersection for full documentation.
- intersects(other)#
Return True if geometries intersect, else False.
- property is_closed#
True if the geometry is closed, else False.
Applicable only to linear geometries.
- property is_empty#
True if the set of points in this geometry is empty, else False.
- property is_ring#
True if the geometry is a closed ring, else False.
- property is_simple#
True if the geometry is simple.
Simple means that any self-intersections are only at boundary points.
- property is_valid#
True if the geometry is valid.
The definition depends on sub-class.
- property length#
Unitless length of the geometry (float).
- line_interpolate_point(distance, normalized=False)#
Return a point at the specified distance along a linear geometry.
Negative length values are taken as measured in the reverse direction from the end of the geometry. Out-of-range index values are handled by clamping them to the valid range of values. If the normalized arg is True, the distance will be interpreted as a fraction of the geometry’s length.
Alias of interpolate.
- line_locate_point(other, normalized=False)#
Return the distance of this geometry to a point nearest the specified point.
If the normalized arg is True, return the distance normalized to the length of the linear geometry.
Alias of project.
- property minimum_clearance#
Unitless distance a node can be moved to produce an invalid geometry (float).
- property minimum_rotated_rectangle#
Return the oriented envelope (minimum rotated rectangle) of the geometry.
Unlike envelope this rectangle is not constrained to be parallel to the coordinate axes. If the convex hull of the object is a degenerate (line or point) this degenerate is returned.
Alias of oriented_envelope.
- normalize()#
Convert geometry to normal form (or canonical form).
This method orders the coordinates, rings of a polygon and parts of multi geometries consistently. Typically useful for testing purposes (for example in combination with equals_exact).
Examples
>>> from shapely import MultiLineString >>> line = MultiLineString([[(0, 0), (1, 1)], [(3, 3), (2, 2)]]) >>> line.normalize() <MULTILINESTRING ((2 2, 3 3), (0 0, 1 1))>
- property oriented_envelope#
Return the oriented envelope (minimum rotated rectangle) of a geometry.
Unlike envelope this rectangle is not constrained to be parallel to the coordinate axes. If the convex hull of the object is a degenerate (line or point) this degenerate is returned.
Alias of minimum_rotated_rectangle.
- overlaps(other)#
Return True if geometries overlap, else False.
- point_on_surface()#
Return a point guaranteed to be within the object, cheaply.
Alias of representative_point.
- project(other, normalized=False)#
Return the distance of geometry to a point nearest the specified point.
If the normalized arg is True, return the distance normalized to the length of the linear geometry.
Alias of line_locate_point.
- relate(other)#
Return the DE-9IM intersection matrix for the two geometries (string).
- relate_pattern(other, pattern)#
Return True if the DE-9IM relationship code satisfies the pattern.
- representative_point()#
Return a point guaranteed to be within the object, cheaply.
Alias of point_on_surface.
- reverse()#
Return a copy of this geometry with the order of coordinates reversed.
If the geometry is a polygon with interior rings, the interior rings are also reversed.
Points are unchanged.
See also
is_ccw
Checks if a geometry is clockwise.
Examples
>>> from shapely import LineString, Polygon >>> LineString([(0, 0), (1, 2)]).reverse() <LINESTRING (1 2, 0 0)> >>> Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]).reverse() <POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))>
- segmentize(max_segment_length)#
Add vertices to line segments based on maximum segment length.
Additional vertices will be added to every line segment in an input geometry so that segments are no longer than the provided maximum segment length. New vertices will evenly subdivide each segment.
Only linear components of input geometries are densified; other geometries are returned unmodified.
- Parameters:
- max_segment_lengthfloat or array_like
Additional vertices will be added so that all line segments are no longer this value. Must be greater than 0.
Examples
>>> from shapely import LineString, Polygon >>> LineString([(0, 0), (0, 10)]).segmentize(max_segment_length=5) <LINESTRING (0 0, 0 5, 0 10)> >>> Polygon([(0, 0), (10, 0), (10, 10), (0, 10), (0, 0)]).segmentize(max_segment_length=5) <POLYGON ((0 0, 5 0, 10 0, 10 5, 10 10, 5 10, 0 10, 0 5, 0 0))>
- simplify(tolerance, preserve_topology=True)#
Return a simplified geometry produced by the Douglas-Peucker algorithm.
Coordinates of the simplified geometry will be no more than the tolerance distance from the original. Unless the topology preserving option is used, the algorithm may produce self-intersecting or otherwise invalid geometries.
- svg(scale_factor=1.0, fill_color=None, opacity=None)#
Return SVG path element for the Polygon geometry.
- Parameters:
- scale_factorfloat
Multiplication factor for the SVG stroke-width. Default is 1.
- fill_colorstr, optional
Hex string for fill color. Default is to use “#66cc99” if geometry is valid, and “#ff3333” if invalid.
- opacityfloat
Float number between 0 and 1 for color opacity. Default value is 0.6
- symmetric_difference(other, grid_size=None)#
Return the symmetric difference of the geometries.
Refer to shapely.symmetric_difference for full documentation.
- touches(other)#
Return True if geometries touch, else False.
- property type#
Get the geometry type (deprecated).
Deprecated since version 2.0: Use the
geom_type
attribute instead.
- union(other, grid_size=None)#
Return the union of the geometries.
Refer to shapely.union for full documentation.
- within(other)#
Return True if geometry is within the other, else False.
- property wkb#
WKB representation of the geometry.
- property wkb_hex#
WKB hex representation of the geometry.
- property wkt#
WKT representation of the geometry.
- property xy#
Separate arrays of X and Y coordinate values.