Validate that polygon has at least 3 vertices in geometry utils.py

This commit is contained in:
Andrey Blazejuk
2024-10-17 13:58:37 -03:00
committed by GitHub
parent 75a4ff1c64
commit 5ac3999aeb
+6
View File
@@ -15,6 +15,9 @@ def get_polygon_center(polygon: np.ndarray) -> Point:
Returns:
Point: The center of the polygon, represented as a
Point object with x and y attributes.
Raises:
ValueError: If the polygon has less than 3 vertices.
Examples:
```python
@@ -30,6 +33,9 @@ def get_polygon_center(polygon: np.ndarray) -> Point:
# This is one of the 3 candidate algorithms considered for centroid calculation.
# For a more detailed discussion, see PR #1084 and commit eb33176
if len(polygon) < 3:
raise ValueError("Polygon must have at least 3 vertices.")
shift_polygon = np.roll(polygon, -1, axis=0)
signed_areas = np.cross(polygon, shift_polygon) / 2
if signed_areas.sum() == 0: