PolygonRegion#
- class rapidtools.core.PolygonRegion(shell: Iterable[tuple[float, float]])[source]#
Bases:
RegionRepresents a geographical region defined by a single polygon shell.
This class wraps a Shapely Polygon and provides initialization from explicit coordinates. Use
from_geometryto wrap existing Shapely objects.Example
Define a triangle using (x, y) coordinates:
>>> from rapidtools.core import PolygonRegion >>> >>> coords = [(0, 0), (10, 0), (0, 10)] >>> region = PolygonRegion(coords) >>> region.area 50.0
Methods
Create a new Region expanded (or shrunk) by a constant distance.
containsCheck if the region completely encloses another geometry.
distanceCalculate the minimum Euclidean distance to another region or geometry.
Create a PolygonRegion from a Shapely geometry.
Calculate the smallest bounding box that encloses the polygon.
intersectsDetermine if the region spatially intersects with another geometry.
- classmethod from_geometry(geometry: BaseGeometry) Self[source]#
Create a PolygonRegion from a Shapely geometry.
- Parameters:
geometry (BaseGeometry) – A Shapely Polygon.
- Returns:
PolygonRegion – The wrapped region.
- Raises:
TypeError – If the geometry is not a Polygon.
Example
>>> from shapely.geometry import Polygon >>> from rapidtools.core import PolygonRegion >>> >>> raw_poly = Polygon([(0, 0), (5, 0), (5, 5)]) >>> region = PolygonRegion.from_geometry(raw_poly) >>> region.area 12.5
- buffer(distance: float) PolygonRegion[source]#
Create a new Region expanded (or shrunk) by a constant distance.
- Parameters:
distance (float) – The distance to buffer. Positive expands, negative erodes.
- Returns:
PolygonRegion – A new PolygonRegion representing the result.
Example
Initialize a
PolygonRegionrepresenting a 10x10 square:>>> from shapely.geometry import Polygon >>> from rapidtools.core import PolygonRegion >>> >>> region = PolygonRegion([(0, 0), (10, 0), (10, 10), (0, 10)]) >>> region.area 100.0
Apply a positive buffer to expand the region:
>>> expanded = region.buffer(1.0) >>> expanded.area > 100.0 True
Apply a negative buffer to erode (shrink) the region:
>>> shrunk = region.buffer(-1.0) >>> shrunk.area < 100.0 True
- get_bounding_box() BoundingBox[source]#
Calculate the smallest bounding box that encloses the polygon.
- Returns:
BoundingBox – A new BoundingBox object representing the calculated bounds.
Example
Initialize a triangular
PolygonRegionand retrieve its enclosingBoundingBox:>>> from rapidtools.core import PolygonRegion >>> >>> region = PolygonRegion([(0, 0), (2, 5), (4, 0)]) >>> bbox = region.get_bounding_box() >>> bbox.bounds (0.0, 0.0, 4.0, 5.0)