Skip to content

Commit 6fc8a2e

Browse files
shaneahmedJiaqi-Lvpre-commit-ci[bot]
authored
🐛 Fix Invalid Geometry in JSON Outputs (#1057)
- Replace direct `Polygon` construction with validated geometry via `feature2geometry` and `make_valid_poly`. - Ensure all shapes are repaired and valid before JSON export. - Prevents malformed or invalid polygons from breaking QuPath JSON outputs. --------- Co-authored-by: Jiaqi Lv <lvjiaqi9@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 36ecfa6 commit 6fc8a2e

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

tiatoolbox/models/engine/multi_task_segmentor.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
import zarr
132132
from dask import delayed
133133
from matplotlib import pyplot as plt
134-
from shapely import Polygon
135134
from shapely.geometry import mapping
136135
from shapely.geometry import shape as feature2geometry
137136
from shapely.strtree import STRtree
@@ -3535,12 +3534,16 @@ def _build_single_qupath_feature(
35353534
to QuPath JSON.
35363535
35373536
"""
3538-
contour = np.array(self._contours[i], dtype=float)
3539-
contour[:, 0] = contour[:, 0] * scale_factor[0] + origin[0]
3540-
contour[:, 1] = contour[:, 1] * scale_factor[1] + origin[1]
3541-
3542-
poly = Polygon(contour)
3543-
poly_geo = mapping(poly)
3537+
geom = make_valid_poly(
3538+
feature2geometry(
3539+
{
3540+
"type": self._processed_predictions.get("geom_type", "Polygon"),
3541+
"coordinates": scale_factor * np.array([self._contours[i]]),
3542+
}
3543+
),
3544+
tuple(origin),
3545+
)
3546+
geo_map = mapping(geom)
35443547

35453548
props = {}
35463549
class_value = None
@@ -3584,7 +3587,7 @@ def _build_single_qupath_feature(
35843587
return {
35853588
"type": "Feature",
35863589
"id": f"object_{i}",
3587-
"geometry": poly_geo,
3590+
"geometry": geo_map,
35883591
"properties": props,
35893592
"objectType": "annotation",
35903593
"name": class_name if class_name is not None else "object",

0 commit comments

Comments
 (0)