Skip to content

Commit 527be66

Browse files
use correct typing MultiPolygon | Polygon + assert polygons
1 parent 5aa32bf commit 527be66

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

src/spatialdata/_core/operations/vectorize.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,15 @@ def _vectorize_chunk(chunk: np.ndarray, yoff: int, xoff: int) -> GeoDataFrame:
227227
return ShapesModel.parse(gdf, transformations=transformations.copy())
228228

229229

230-
def _region_props_to_polygon(region_props: RegionProperties) -> MultiPolygon:
230+
def _region_props_to_polygon(region_props: RegionProperties) -> MultiPolygon | Polygon:
231231
mask = np.pad(region_props.image, 1)
232232
contours = skimage.measure.find_contours(mask, 0.5)
233233

234234
# shapes with <= 3 vertices, i.e. lines, can't be converted into a polygon
235235
polygons = [Polygon(contour[:, [1, 0]]) for contour in contours if contour.shape[0] >= 4]
236+
237+
assert len(polygons) > 0, "No valid polygon found in the region properties."
238+
236239
polygon = MultiPolygon(polygons) if len(polygons) > 1 else polygons[0]
237240

238241
yoff, xoff, *_ = region_props.bbox

0 commit comments

Comments
 (0)