Перейти до основного вмісту

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: Sequence[Point], tolerance: Optional[float] = None) -> Optional[Polygon]

Clean a polygon by removing near-duplicate points.

Returns: Cleaned polygon or None.

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

flip_polygon()

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

Flip a polygon horizontally and/or vertically.

Returns: Flipped polygon.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
flip_hboolWhether to flip horizontally.
flip_vboolWhether to flip vertically.
ReturnsPolygon

flip_polygon_numpy()

flip_polygon_numpy(polygon: NDArray[f64], flip_h: bool, flip_v: bool) -> Any

Flip a polygon from numpy array.

Returns: Flipped polygon as numpy array.

ParameterTypeDescription
polygonNDArray[f64]Polygon 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[Polygon]

Flip multiple polygons.

Returns: Flipped polygons.

ParameterTypeDescription
polygonsAnyList of polygons to flip.
flip_hboolWhether to flip horizontally.
flip_vboolWhether to flip vertically.
Returnslist[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: Sequence[Point]) -> float

Get the unsigned area of a polygon.

Returns: Unsigned area.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
Returnsfloat

get_polygon_bounds()

get_polygon_bounds(polygon: Sequence[Point]) -> Rect

Get the bounding rectangle of a polygon.

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

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
ReturnsRect

get_polygon_centroid()

get_polygon_centroid(polygon: Sequence[Point]) -> Point

Get the centroid of a polygon.

Returns: Centroid point (x, y).

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
ReturnsPoint

get_polygon_convex_hull()

get_polygon_convex_hull(polygon: Sequence[Point]) -> Polygon

Get the convex hull of a polygon.

Returns: Convex hull as list of points.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
ReturnsPolygon

get_polygon_edges()

get_polygon_edges(polygon: Sequence[Point]) -> list[tuple[Point, Point]]

Get the edges of a polygon.

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

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
Returnslist[tuple[Point, Point]]

get_polygon_group_bounds()

get_polygon_group_bounds(polygons: Any) -> 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.
ReturnsRect

get_polygon_perimeter()

get_polygon_perimeter(polygon: Sequence[Point]) -> float

Get the perimeter of a polygon.

Returns: Perimeter length.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
Returnsfloat

get_polygon_signed_area()

get_polygon_signed_area(polygon: Sequence[Point]) -> float

Get the signed area of a polygon.

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

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
Returnsfloat

get_polygons_difference()

get_polygons_difference(poly1: Sequence[Point], poly2: Sequence[Point]) -> list[Polygon]

Get the difference of two polygons.

Returns: Difference polygon(s).

ParameterTypeDescription
poly1Sequence[Point]First polygon as (x, y) points.
poly2Sequence[Point]Second polygon to subtract.
Returnslist[Polygon]

get_polygons_intersection()

get_polygons_intersection(poly1: Sequence[Point], poly2: Sequence[Point]) -> list[Polygon]

Get the intersection of two polygons.

Returns: Intersection polygon(s).

ParameterTypeDescription
poly1Sequence[Point]First polygon as (x, y) points.
poly2Sequence[Point]Second polygon as (x, y) points.
Returnslist[Polygon]

get_polygons_union()

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

Get the union of multiple polygons.

Returns: Union polygon(s).

ParameterTypeDescription
polygonsAnyList of polygons to union.
Returnslist[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: Point, polygon: Sequence[Point]) -> bool

Check if a point is inside a polygon.

Returns: True if point is inside the polygon.

ParameterTypeDescription
pointPointPoint (x, y) to test.
polygonSequence[Point]Polygon as (x, y) points.
Returnsbool

is_polygon_clockwise()

is_polygon_clockwise(points: Sequence[Point]) -> bool

Check if a polygon has clockwise winding order.

Returns: True if the winding is clockwise.

ParameterTypeDescription
pointsSequence[Point]Sequence of (x, y) points defining a polygon.
Returnsbool

is_polygon_convex()

is_polygon_convex(polygon: Sequence[Point]) -> bool

Check if a polygon is convex.

Returns: True if the polygon is convex.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
Returnsbool

normalize_polygons()

normalize_polygons(polygons: Any) -> tuple[list[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[Polygon], float, float]

normalize_polygons_numpy()

normalize_polygons_numpy(polygons: Sequence[NDArray[f64]]) -> tuple[list[NDArray[f64]], float, float]

Normalize polygons from numpy arrays.

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

ParameterTypeDescription
polygonsSequence[NDArray[f64]]Sequence of 2D numpy arrays.
Returnstuple[list[NDArray[f64]], float, float]

offset_polygon()

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

Offset (inflate/deflate) a polygon.

Returns: Offset polygon(s).

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

point_in_polygon_numpy()

point_in_polygon_numpy(point: Point, polygon: NDArray[f64]) -> bool

Check if point is in polygon from numpy array.

Returns: True if point is inside the polygon.

ParameterTypeDescription
pointPointPoint (x, y) to test.
polygonNDArray[f64]Polygon as a 2D numpy array.
Returnsbool

point_line_distance()

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

Compute the distance from a point to a line.

Returns: Perpendicular distance.

ParameterTypeDescription
pointPointPoint (x, y).
line_startPointLine start point (x, y).
line_endPointLine end point (x, y).
Returnsfloat

polygon_area_numpy()

polygon_area_numpy(polygon: NDArray[f64]) -> float

Get the area of a polygon from numpy array.

Returns: Signed area.

ParameterTypeDescription
polygonNDArray[f64]Polygon as a 2D numpy array.
Returnsfloat

polygon_bounds_numpy()

polygon_bounds_numpy(polygon: NDArray[f64]) -> Rect

Get bounds of a polygon from numpy array.

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

ParameterTypeDescription
polygonNDArray[f64]Polygon as a 2D numpy array.
ReturnsRect

polygon_group_bounds_numpy()

polygon_group_bounds_numpy(polygons: Sequence[NDArray[f64]]) -> Rect

Get bounds of polygon group from numpy arrays.

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

ParameterTypeDescription
polygonsSequence[NDArray[f64]]Sequence of 2D numpy arrays.
ReturnsRect

polygon_perimeter_numpy()

polygon_perimeter_numpy(polygon: NDArray[f64]) -> float

Get the perimeter of a polygon from numpy array.

Returns: Perimeter length.

ParameterTypeDescription
polygonNDArray[f64]Polygon as a 2D numpy array.
Returnsfloat

polygons_intersect()

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

Check if two polygons intersect.

Returns: True if polygons intersect.

ParameterTypeDescription
p1Sequence[Point]First polygon as (x, y) points.
p2Sequence[Point]Second polygon as (x, y) points.
min_areafloat = 0Minimum intersection area threshold.
Returnsbool

polygons_intersect_numpy()

polygons_intersect_numpy(poly1: NDArray[f64], poly2: NDArray[f64], min_area: float = 0) -> bool

Check if polygons intersect from numpy arrays.

Returns: True if polygons intersect.

ParameterTypeDescription
poly1NDArray[f64]First polygon as a 2D numpy array.
poly2NDArray[f64]Second polygon as a 2D numpy array.
min_areafloat = 0Minimum intersection area threshold.
Returnsbool

rotate_polygon()

rotate_polygon(polygon: Sequence[Point], angle: float) -> Polygon

Rotate a polygon by an angle.

Returns: Rotated polygon.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
anglefloatRotation angle in radians.
ReturnsPolygon

rotate_polygon_numpy()

rotate_polygon_numpy(polygon: NDArray[f64], angle: float) -> Any

Rotate a polygon from numpy array.

Returns: Rotated polygon as numpy array.

ParameterTypeDescription
polygonNDArray[f64]Polygon as a 2D numpy array.
anglefloatRotation angle in radians.
ReturnsAny

rotate_polygons()

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

Rotate multiple polygons by an angle.

Returns: Rotated polygons.

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

rotate_polygons_numpy()

rotate_polygons_numpy(polygons: Sequence[NDArray[f64]], angle: float) -> list[Any]

Rotate polygons from numpy arrays.

Returns: List of rotated numpy arrays.

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

scale_polygon()

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

Scale a polygon.

Returns: Scaled polygon.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
scalefloatX (and Y if scale_y is None) scale factor.
scale_yOptional[float] = NoneY scale factor (optional).
ReturnsPolygon

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: Rect, dx: float, dy: float) -> Rect

Translate a bounding rectangle.

Returns: Translated bounding rectangle.

ParameterTypeDescription
boundsRectBounding rectangle (x_min, y_min, x_max, y_max).
dxfloatX translation.
dyfloatY translation.
ReturnsRect

translate_polygon()

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

Translate a polygon.

Returns: Translated polygon.

ParameterTypeDescription
polygonSequence[Point]Polygon as (x, y) points.
dxfloatX translation.
dyfloatY translation.
ReturnsPolygon

translate_polygon_numpy()

translate_polygon_numpy(polygon: NDArray[f64], dx: float, dy: float) -> Any

Translate a polygon from numpy array.

Returns: Translated polygon as numpy array.

ParameterTypeDescription
polygonNDArray[f64]Polygon as a 2D numpy array.
dxfloatX translation.
dyfloatY translation.
ReturnsAny

translate_polygons()

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

Translate a list of polygons.

Returns: Translated polygons.

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

translate_polygons_numpy()

translate_polygons_numpy(polygons: Sequence[NDArray[f64]], dx: float, dy: float) -> list[Any]

Translate polygons from numpy arrays.

Returns: List of translated numpy arrays.

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