Apply crop transform on a list of polygons, each represented by a Nx2 array. It will crop the polygon with the box, therefore the number of points in the polygon might change. Args: polygon (list[ndarray]): each is a Nx2 floating point array of
(self, polygons: list)
| 205 | return coords |
| 206 | |
| 207 | def apply_polygons(self, polygons: list) -> list: |
| 208 | """ |
| 209 | Apply crop transform on a list of polygons, each represented by a Nx2 array. |
| 210 | It will crop the polygon with the box, therefore the number of points in the |
| 211 | polygon might change. |
| 212 | |
| 213 | Args: |
| 214 | polygon (list[ndarray]): each is a Nx2 floating point array of |
| 215 | (x, y) format in absolute coordinates. |
| 216 | Returns: |
| 217 | ndarray: cropped polygons. |
| 218 | """ |
| 219 | import shapely.geometry as geometry |
| 220 | |
| 221 | # Create a window that will be used to crop |
| 222 | crop_box = geometry.box( |
| 223 | self.x0, self.y0, self.x0 + self.w, self.y0 + self.h |
| 224 | ).buffer(0.0) |
| 225 | |
| 226 | cropped_polygons = [] |
| 227 | |
| 228 | for polygon in polygons: |
| 229 | polygon = geometry.Polygon(polygon).buffer(0.0) |
| 230 | # polygon must be valid to perform intersection. |
| 231 | if not polygon.is_valid: |
| 232 | continue |
| 233 | cropped = polygon.intersection(crop_box) |
| 234 | if cropped.is_empty: |
| 235 | continue |
| 236 | if not isinstance(cropped, geometry.collection.BaseMultipartGeometry): |
| 237 | cropped = [cropped] |
| 238 | # one polygon may be cropped to multiple ones |
| 239 | for poly in cropped: |
| 240 | # It could produce lower dimensional objects like lines or |
| 241 | # points, which we want to ignore |
| 242 | if not isinstance(poly, geometry.Polygon) or not poly.is_valid: |
| 243 | continue |
| 244 | coords = np.asarray(poly.exterior.coords) |
| 245 | # NOTE This process will produce an extra identical vertex at |
| 246 | # the end. So we remove it. This is tested by |
| 247 | # `tests/test_data_transform.py` |
| 248 | cropped_polygons.append(coords[:-1]) |
| 249 | return [self.apply_coords(p) for p in cropped_polygons] |
| 250 | |
| 251 | def inverse(self) -> Transform: |
| 252 | assert ( |
nothing calls this directly
no test coverage detected