def crop(self, rect): ''' Crop the current Polyline with a given rectangle, if polyline cat't be cropped it generate exception error :param rect: Rectangle class object :return: list of Polyline class objects ''' try: clipping_window = [[rect.top, rect.left], [rect.top, rect.right], [rect.bottom, rect.right], [rect.bottom, rect.left]] clipping_window_shpl = ShapelyPolygon(clipping_window) exterior = self.exterior_np intersections_polygon = LineString(exterior).intersection( clipping_window_shpl) mapping_shpl = mapping(intersections_polygon) except Exception: logger.warn('Line cropping exception, shapely.', exc_info=False) raise res_lines_pts = shapely_figure_to_coords_list(mapping_shpl) # tiny hack to combine consecutive segments lines_combined = [] for simple_l in res_lines_pts: if len(lines_combined) > 0: prev = lines_combined[-1] if prev[-1] == simple_l[0]: lines_combined[-1] = list(prev) + list(simple_l[1:]) continue lines_combined.append(simple_l) return [ Polyline(row_col_list_to_points(line)) for line in lines_combined ]
def crop(self, rect): try: clipping_window_shpl = ShapelyPolygon( points_to_row_col_list(rect.corners)) self_shpl = ShapelyPolygon(self.exterior_np, holes=self.interior_np) intersections_shpl = self_shpl.buffer(0).intersection( clipping_window_shpl) mapping_shpl = mapping(intersections_shpl) except Exception: logger.warn('Polygon cropping exception, shapely.', exc_info=False) raise intersections = shapely_figure_to_coords_list(mapping_shpl) # Check for bad cropping cases (e.g. empty points list) out_polygons = [] for intersection in intersections: if isinstance(intersection, list) and len(intersection) > 0 and len( intersection[0]) >= 3: exterior = row_col_list_to_points(intersection[0], do_round=True) interiors = [] for interior_contour in intersection[1:]: if len(interior_contour) > 2: interiors.append( row_col_list_to_points(interior_contour, do_round=True)) out_polygons.append(Polygon(exterior, interiors)) return out_polygons
def crop(self, rect): try: clipping_window = [[rect.left, rect.top], [rect.right, rect.top], [rect.right, rect.bottom], [rect.left, rect.bottom]] clipping_window_shpl = ShapelyPolygon(clipping_window) exterior = self.exterior_np[:, ::-1] intersections_polygon = LineString(exterior).intersection( clipping_window_shpl) mapping_shpl = mapping(intersections_polygon) except Exception: logger.warn('Line cropping exception, shapely.', exc_info=False) raise res_lines_pts = shapely_figure_to_coords_list(mapping_shpl) # tiny hack to combine consecutive segments lines_combined = [] for simple_l in res_lines_pts: if len(lines_combined) > 0: prev = lines_combined[-1] if prev[-1] == simple_l[0]: lines_combined[-1] = list(prev) + list(simple_l[1:]) continue lines_combined.append(simple_l) return [ Polyline(row_col_list_to_points(line)) for line in lines_combined ]
def crop(self, rect): ''' Crop the current Polygon with a given rectangle, if polygon cat't be cropped it generate exception error :param rect: Rectangle class object :return: list of Poligon class objects ''' from supervisely_lib.geometry.point_location import PointLocation try: points = [ PointLocation(row=rect.top, col=rect.left), PointLocation(row=rect.top, col=rect.right + 1), PointLocation(row=rect.bottom + 1, col=rect.right + 1), PointLocation(row=rect.bottom + 1, col=rect.left) ] #points = rect.corners # old implementation with 1 pixel error (right bottom) #@TODO: investigate here (critical issue) clipping_window_shpl = ShapelyPolygon( points_to_row_col_list(points)) self_shpl = ShapelyPolygon(self.exterior_np, holes=self.interior_np) intersections_shpl = self_shpl.buffer(0).intersection( clipping_window_shpl) mapping_shpl = mapping(intersections_shpl) except Exception: logger.warn('Polygon cropping exception, shapely.', exc_info=True) # raise # if polygon is invalid, just print warning and skip it # @TODO: need more investigation here return [] intersections = shapely_figure_to_coords_list(mapping_shpl) # Check for bad cropping cases (e.g. empty points list) out_polygons = [] for intersection in intersections: if isinstance(intersection, list) and len(intersection) > 0 and len( intersection[0]) >= 3: exterior = row_col_list_to_points(intersection[0], do_round=True) interiors = [] for interior_contour in intersection[1:]: if len(interior_contour) > 2: interiors.append( row_col_list_to_points(interior_contour, do_round=True)) out_polygons.append(Polygon(exterior, interiors)) return out_polygons