Skip to main content

raygeo.geo.shape.polygon

Polygon manipulation functions.

Provides area (signed and unsigned), perimeter, bounding box, centroid, convex hull, point-in-polygon containment test, edge extraction, convexity testing, boolean operations (union, intersection, difference), offset (inflate or deflate), cleaning (removing near-duplicate vertices), normalisation (outer CCW / inner CW winding order), and transformations (translate, rotate, scale, flip). All functions accept list-of-tuples input; many also provide numpy array variants.

Functions

clean_polygon()

clean_polygon(polygon: collections.abc.Sequence[types.Point], tolerance: Optional[float] = None) -> Optional[types.Polygon]

Clean a polygon by removing near-duplicate points.

Returns: Cleaned polygon or None.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Input polygon as (x, y) points.
toleranceOptional[float] = NoneDistance tolerance for deduplication.
ReturnsOptional[types.Polygon]

flip_polygon()

flip_polygon(polygon: collections.abc.Sequence[types.Point], flip_h: bool, flip_v: bool) -> types.Polygon

Flip a polygon horizontally and/or vertically.

Returns: Flipped polygon.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
flip_hboolWhether to flip horizontally.
flip_vboolWhether to flip vertically.
Returnstypes.Polygon

flip_polygon_numpy()

flip_polygon_numpy(polygon: numpy.NDArray, flip_h: bool, flip_v: bool) -> Any

Flip a polygon from numpy array.

Returns: Flipped polygon as numpy array.

ParameterTypeDescription
polygonnumpy.NDArrayPolygon as a 2D numpy array.
flip_hboolWhether to flip horizontally.
flip_vboolWhether to flip vertically.
ReturnsAny

flip_polygons()

flip_polygons(polygons: Any, flip_h: bool, flip_v: bool) -> list[types.Polygon]

Flip multiple polygons.

Returns: Flipped polygons.

ParameterTypeDescription
polygonsAnyList of polygons to flip.
flip_hboolWhether to flip horizontally.
flip_vboolWhether to flip vertically.
Returnslist[types.Polygon]

flip_polygons_numpy()

flip_polygons_numpy(polygons: list, flip_h: bool, flip_v: bool) -> Any

Flip polygons from numpy arrays.

Returns: List of flipped numpy arrays.

ParameterTypeDescription
polygonslistList of 2D numpy arrays.
flip_hboolWhether to flip horizontally.
flip_vboolWhether to flip vertically.
ReturnsAny

get_polygon_area()

get_polygon_area(polygon: collections.abc.Sequence[types.Point]) -> float

Get the unsigned area of a polygon.

Returns: Unsigned area.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnsfloat

get_polygon_bounds()

get_polygon_bounds(polygon: collections.abc.Sequence[types.Point]) -> types.Rect

Get the bounding rectangle of a polygon.

Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnstypes.Rect

get_polygon_centroid()

get_polygon_centroid(polygon: collections.abc.Sequence[types.Point]) -> types.Point

Get the centroid of a polygon.

Returns: Centroid point (x, y).

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnstypes.Point

get_polygon_convex_hull()

get_polygon_convex_hull(polygon: collections.abc.Sequence[types.Point]) -> types.Polygon

Get the convex hull of a polygon.

Returns: Convex hull as list of points.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnstypes.Polygon

get_polygon_edges()

get_polygon_edges(polygon: collections.abc.Sequence[types.Point]) -> list[tuple[types.Point, types.Point]]

Get the edges of a polygon.

Returns: List of ((x1, y1), (x2, y2)) edges.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnslist[tuple[types.Point, types.Point]]

get_polygon_group_bounds()

get_polygon_group_bounds(polygons: Any) -> types.Rect

Get the bounding rectangle of a group of polygons.

Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).

ParameterTypeDescription
polygonsAnyList of polygons.
Returnstypes.Rect

get_polygon_perimeter()

get_polygon_perimeter(polygon: collections.abc.Sequence[types.Point]) -> float

Get the perimeter of a polygon.

Returns: Perimeter length.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnsfloat

get_polygon_signed_area()

get_polygon_signed_area(polygon: collections.abc.Sequence[types.Point]) -> float

Get the signed area of a polygon.

Returns: Signed area (positive for CCW, negative for CW).

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnsfloat

get_polygons_difference()

get_polygons_difference(poly1: collections.abc.Sequence[types.Point], poly2: collections.abc.Sequence[types.Point]) -> list[types.Polygon]

Get the difference of two polygons.

Returns: Difference polygon(s).

ParameterTypeDescription
poly1collections.abc.Sequence[types.Point]First polygon as (x, y) points.
poly2collections.abc.Sequence[types.Point]Second polygon to subtract.
Returnslist[types.Polygon]

get_polygons_intersection()

get_polygons_intersection(poly1: collections.abc.Sequence[types.Point], poly2: collections.abc.Sequence[types.Point]) -> list[types.Polygon]

Get the intersection of two polygons.

Returns: Intersection polygon(s).

ParameterTypeDescription
poly1collections.abc.Sequence[types.Point]First polygon as (x, y) points.
poly2collections.abc.Sequence[types.Point]Second polygon as (x, y) points.
Returnslist[types.Polygon]

get_polygons_union()

get_polygons_union(polygons: Any) -> list[types.Polygon]

Get the union of multiple polygons.

Returns: Union polygon(s).

ParameterTypeDescription
polygonsAnyList of polygons to union.
Returnslist[types.Polygon]

is_almost_equal()

is_almost_equal(a: float, b: float, tolerance: Optional[float] = None) -> bool

Check if two floats are almost equal.

Returns: True if |a - b| < tolerance.

ParameterTypeDescription
afloatFirst float.
bfloatSecond float.
toleranceOptional[float] = NoneComparison tolerance.
Returnsbool

is_point_inside_polygon()

is_point_inside_polygon(point: types.Point, polygon: collections.abc.Sequence[types.Point]) -> bool

Check if a point is inside a polygon.

Returns: True if point is inside the polygon.

ParameterTypeDescription
pointtypes.PointPoint (x, y) to test.
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnsbool

is_polygon_clockwise()

is_polygon_clockwise(points: collections.abc.Sequence[types.Point2DOr3D]) -> bool

Check if a polygon has clockwise winding order.

Returns: True if the winding is clockwise.

ParameterTypeDescription
pointscollections.abc.Sequence[types.Point2DOr3D]Sequence of (x, y) points defining a polygon.
Returnsbool

is_polygon_convex()

is_polygon_convex(polygon: collections.abc.Sequence[types.Point]) -> bool

Check if a polygon is convex.

Returns: True if the polygon is convex.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
Returnsbool

normalize_polygons()

normalize_polygons(polygons: Any) -> tuple[list[types.Polygon], float, float]

Normalize polygons (outer CCW, inner CW).

Returns: Tuple of (normalized_polygons, min_x, min_y).

ParameterTypeDescription
polygonsAnyList of polygons to normalize.
Returnstuple[list[types.Polygon], float, float]

normalize_polygons_numpy()

normalize_polygons_numpy(polygons: collections.abc.Sequence[numpy.NDArray]) -> tuple[list[numpy.NDArray], float, float]

Normalize polygons from numpy arrays.

Returns: Tuple of (normalized_arrays, min_x, min_y).

ParameterTypeDescription
polygonscollections.abc.Sequence[numpy.NDArray]Sequence of 2D numpy arrays.
Returnstuple[list[numpy.NDArray], float, float]

offset_polygon()

offset_polygon(polygon: collections.abc.Sequence[types.Point], offset: float) -> list[types.Polygon]

Offset (inflate/deflate) a polygon.

Returns: Offset polygon(s).

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
offsetfloatOffset distance (positive to inflate, negative to deflate).
Returnslist[types.Polygon]

point_in_polygon_numpy()

point_in_polygon_numpy(point: types.Point, polygon: numpy.NDArray) -> bool

Check if point is in polygon from numpy array.

Returns: True if point is inside the polygon.

ParameterTypeDescription
pointtypes.PointPoint (x, y) to test.
polygonnumpy.NDArrayPolygon as a 2D numpy array.
Returnsbool

point_line_distance()

point_line_distance(point: types.Point, line_start: types.Point, line_end: types.Point) -> float

Compute the distance from a point to a line.

Returns: Perpendicular distance.

ParameterTypeDescription
pointtypes.PointPoint (x, y).
line_starttypes.PointLine start point (x, y).
line_endtypes.PointLine end point (x, y).
Returnsfloat

polygon_area_numpy()

polygon_area_numpy(polygon: numpy.NDArray) -> float

Get the area of a polygon from numpy array.

Returns: Signed area.

ParameterTypeDescription
polygonnumpy.NDArrayPolygon as a 2D numpy array.
Returnsfloat

polygon_bounds_numpy()

polygon_bounds_numpy(polygon: numpy.NDArray) -> types.Rect

Get bounds of a polygon from numpy array.

Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).

ParameterTypeDescription
polygonnumpy.NDArrayPolygon as a 2D numpy array.
Returnstypes.Rect

polygon_group_bounds_numpy()

polygon_group_bounds_numpy(polygons: collections.abc.Sequence[numpy.NDArray]) -> types.Rect

Get bounds of polygon group from numpy arrays.

Returns: Bounding rectangle as (x_min, y_min, x_max, y_max).

ParameterTypeDescription
polygonscollections.abc.Sequence[numpy.NDArray]Sequence of 2D numpy arrays.
Returnstypes.Rect

polygon_perimeter_numpy()

polygon_perimeter_numpy(polygon: numpy.NDArray) -> float

Get the perimeter of a polygon from numpy array.

Returns: Perimeter length.

ParameterTypeDescription
polygonnumpy.NDArrayPolygon as a 2D numpy array.
Returnsfloat

polygons_intersect()

polygons_intersect(p1: collections.abc.Sequence[types.Point], p2: collections.abc.Sequence[types.Point], min_area: float = 0) -> bool

Check if two polygons intersect.

Returns: True if polygons intersect.

ParameterTypeDescription
p1collections.abc.Sequence[types.Point]First polygon as (x, y) points.
p2collections.abc.Sequence[types.Point]Second polygon as (x, y) points.
min_areafloat = 0Minimum intersection area threshold.
Returnsbool

polygons_intersect_numpy()

polygons_intersect_numpy(poly1: numpy.NDArray, poly2: numpy.NDArray, min_area: float = 0) -> bool

Check if polygons intersect from numpy arrays.

Returns: True if polygons intersect.

ParameterTypeDescription
poly1numpy.NDArrayFirst polygon as a 2D numpy array.
poly2numpy.NDArraySecond polygon as a 2D numpy array.
min_areafloat = 0Minimum intersection area threshold.
Returnsbool

rotate_polygon()

rotate_polygon(polygon: collections.abc.Sequence[types.Point], angle: float) -> types.Polygon

Rotate a polygon by an angle.

Returns: Rotated polygon.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
anglefloatRotation angle in radians.
Returnstypes.Polygon

rotate_polygon_numpy()

rotate_polygon_numpy(polygon: numpy.NDArray, angle: float) -> Any

Rotate a polygon from numpy array.

Returns: Rotated polygon as numpy array.

ParameterTypeDescription
polygonnumpy.NDArrayPolygon as a 2D numpy array.
anglefloatRotation angle in radians.
ReturnsAny

rotate_polygons()

rotate_polygons(polygons: Any, angle: float) -> list[types.Polygon]

Rotate multiple polygons by an angle.

Returns: Rotated polygons.

ParameterTypeDescription
polygonsAnyList of polygons to rotate.
anglefloatRotation angle in radians.
Returnslist[types.Polygon]

rotate_polygons_numpy()

rotate_polygons_numpy(polygons: collections.abc.Sequence[numpy.NDArray], angle: float) -> list[Any]

Rotate polygons from numpy arrays.

Returns: List of rotated numpy arrays.

ParameterTypeDescription
polygonscollections.abc.Sequence[numpy.NDArray]Sequence of 2D numpy arrays.
anglefloatRotation angle in radians.
Returnslist[Any]

scale_polygon()

scale_polygon(polygon: collections.abc.Sequence[types.Point], scale: float, scale_y: Optional[float] = None) -> types.Polygon

Scale a polygon.

Returns: Scaled polygon.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
scalefloatX (and Y if scale_y is None) scale factor.
scale_yOptional[float] = NoneY scale factor (optional).
Returnstypes.Polygon

to_clipper_numpy()

to_clipper_numpy(polygon: Any, scale: int = 10000000) -> list[tuple[int, int]]

Convert a polygon to Clipper coordinates.

Returns: Polygon with integer coordinates for Clipper.

ParameterTypeDescription
polygonAnyPolygon as numpy array or list of tuples.
scaleint = 10000000Scale factor for integer conversion.
Returnslist[tuple[int, int]]

translate_bounds()

translate_bounds(bounds: types.Rect, dx: float, dy: float) -> types.Rect

Translate a bounding rectangle.

Returns: Translated bounding rectangle.

ParameterTypeDescription
boundstypes.RectBounding rectangle (x_min, y_min, x_max, y_max).
dxfloatX translation.
dyfloatY translation.
Returnstypes.Rect

translate_polygon()

translate_polygon(polygon: collections.abc.Sequence[types.Point], dx: float, dy: float) -> types.Polygon

Translate a polygon.

Returns: Translated polygon.

ParameterTypeDescription
polygoncollections.abc.Sequence[types.Point]Polygon as (x, y) points.
dxfloatX translation.
dyfloatY translation.
Returnstypes.Polygon

translate_polygon_numpy()

translate_polygon_numpy(polygon: numpy.NDArray, dx: float, dy: float) -> Any

Translate a polygon from numpy array.

Returns: Translated polygon as numpy array.

ParameterTypeDescription
polygonnumpy.NDArrayPolygon as a 2D numpy array.
dxfloatX translation.
dyfloatY translation.
ReturnsAny

translate_polygons()

translate_polygons(polygons: Any, dx: float, dy: float) -> list[types.Polygon]

Translate a list of polygons.

Returns: Translated polygons.

ParameterTypeDescription
polygonsAnyList of polygons to translate.
dxfloatX translation.
dyfloatY translation.
Returnslist[types.Polygon]

translate_polygons_numpy()

translate_polygons_numpy(polygons: collections.abc.Sequence[numpy.NDArray], dx: float, dy: float) -> list[Any]

Translate polygons from numpy arrays.

Returns: List of translated numpy arrays.

ParameterTypeDescription
polygonscollections.abc.Sequence[numpy.NDArray]Sequence of 2D numpy arrays.
dxfloatX translation.
dyfloatY translation.
Returnslist[Any]