polygon2contour: avoid overflow

This commit is contained in:
Robert Sachunsky 2025-08-29 12:16:56 +02:00
parent 7a9e8256ee
commit 11e143afee

View file

@ -356,7 +356,8 @@ def contour2polygon(contour: Union[np.ndarray, Sequence[Sequence[Sequence[Number
return make_valid(polygon) return make_valid(polygon)
def polygon2contour(polygon: Polygon) -> np.ndarray: def polygon2contour(polygon: Polygon) -> np.ndarray:
return np.array(polygon.exterior.coords[:-1], dtype=np.uint)[:, np.newaxis] polygon = np.array(polygon.exterior.coords[:-1], dtype=int)
return np.maximum(0, polygon).astype(np.uint)[:, np.newaxis]
def make_valid(polygon: Polygon) -> Polygon: def make_valid(polygon: Polygon) -> Polygon:
"""Ensures shapely.geometry.Polygon object is valid by repeated rearrangement/simplification/enlargement.""" """Ensures shapely.geometry.Polygon object is valid by repeated rearrangement/simplification/enlargement."""