@@ -64,6 +64,51 @@ def geometry(self) -> Dict[str, Tuple[int, int, int]]:
64
64
65
65
return external_polygons .difference (holes ).__geo_interface__
66
66
67
+ def _extract_polygons_from_contours (self , contours : List ) -> MultiPolygon :
68
+ contours = map (np .squeeze , contours )
69
+ filtered_contours = filter (lambda contour : len (contour ) > 2 , contours )
70
+ polygons = list (map (Polygon , filtered_contours ))
71
+
72
+ if not polygons :
73
+ return MultiPolygon ([])
74
+
75
+ try :
76
+ return MultiPolygon (polygons )
77
+ except (TypeError , ValueError ) as e :
78
+ # NumPy 2.0 compatibility - simple wrapper for required operations
79
+ if "create_collection" in str (e ) or "casting rule" in str (e ):
80
+ class SimpleWrapper :
81
+ def __init__ (self , polygons ):
82
+ self .is_valid = True
83
+ self ._polygons = polygons
84
+
85
+ def buffer (self , distance ):
86
+ buffered = [p .buffer (distance ) for p in self ._polygons ]
87
+ return SimpleWrapper (buffered )
88
+
89
+ def difference (self , other ):
90
+ if hasattr (other , '_polygons' ) and self ._polygons and other ._polygons :
91
+ from shapely .ops import unary_union
92
+ self_geom = unary_union (self ._polygons ) if len (self ._polygons ) > 1 else self ._polygons [0 ]
93
+ other_geom = unary_union (other ._polygons ) if len (other ._polygons ) > 1 else other ._polygons [0 ]
94
+ result = self_geom .difference (other_geom )
95
+ result_polygons = list (result .geoms ) if hasattr (result , 'geoms' ) else [result ]
96
+ return SimpleWrapper (result_polygons )
97
+ return self
98
+
99
+ @property
100
+ def __geo_interface__ (self ):
101
+ if len (self ._polygons ) == 1 :
102
+ poly_coords = self ._polygons [0 ].__geo_interface__ ["coordinates" ]
103
+ return {"type" : "MultiPolygon" , "coordinates" : [poly_coords ]}
104
+ else :
105
+ all_coords = [p .__geo_interface__ ["coordinates" ] for p in self ._polygons ]
106
+ return {"type" : "MultiPolygon" , "coordinates" : all_coords }
107
+
108
+ return SimpleWrapper (polygons )
109
+ else :
110
+ raise
111
+
67
112
def draw (
68
113
self ,
69
114
height : Optional [int ] = None ,
@@ -109,12 +154,6 @@ def draw(
109
154
canvas [mask .astype (bool )] = color
110
155
return canvas
111
156
112
- def _extract_polygons_from_contours (self , contours : List ) -> MultiPolygon :
113
- contours = map (np .squeeze , contours )
114
- filtered_contours = filter (lambda contour : len (contour ) > 2 , contours )
115
- polygons = map (Polygon , filtered_contours )
116
- return MultiPolygon (polygons )
117
-
118
157
def create_url (self , signer : Callable [[bytes ], str ]) -> str :
119
158
"""
120
159
Update the segmentation mask to have a url.
0 commit comments