diff --git a/building_boundary/core/segmentation.py b/building_boundary/core/segmentation.py index cc33c45..bcfe669 100644 --- a/building_boundary/core/segmentation.py +++ b/building_boundary/core/segmentation.py @@ -277,7 +277,7 @@ def boundary_segmentation(points, distance): shift = np.min(points_shifted, axis=0) points_shifted -= shift - mask = np.ones(len(points_shifted), dtype=np.bool) + mask = np.ones(len(points_shifted), dtype=bool) indices = np.arange(len(points_shifted)) segments = [] diff --git a/building_boundary/shapes/alpha_shape.py b/building_boundary/shapes/alpha_shape.py index 577c26d..701c43b 100644 --- a/building_boundary/shapes/alpha_shape.py +++ b/building_boundary/shapes/alpha_shape.py @@ -14,10 +14,10 @@ from CGAL.CGAL_Alpha_shape_2 import Alpha_shape_2 from CGAL.CGAL_Alpha_shape_2 import REGULAR CGAL_AVAILABLE = True - cascaded_union = None + unary_union = None Delaunay = None except ImportError: - from shapely.ops import cascaded_union + from shapely.ops import unary_union from scipy.spatial import Delaunay CGAL_AVAILABLE = False Point_2 = None @@ -110,7 +110,11 @@ def triangle_geometry(triangle): # Semiperimeter of triangle s = (a + b + c) / 2.0 # Area of triangle by Heron's formula - area = math.sqrt(s * (s - a) * (s - b) * (s - c)) + area = 0 + try: + area = math.sqrt(s * (s - a) * (s - b) * (s - c)) + except ValueError: + pass if area != 0: circum_r = (a * b * c) / (4.0 * area) else: @@ -144,9 +148,9 @@ def alpha_shape_python(points, alpha): if circum_r < 1.0 / alpha: triangles.append(Polygon(points[t])) - alpha_shape = cascaded_union(triangles) + alpha_shape = unary_union(triangles) if type(alpha_shape) == MultiPolygon: - alpha_shape = MultiPolygon([Polygon(s.exterior) for s in alpha_shape]) + alpha_shape = MultiPolygon([Polygon(s.exterior) for s in alpha_shape.geoms]) else: alpha_shape = Polygon(alpha_shape.exterior) diff --git a/building_boundary/shapes/fit.py b/building_boundary/shapes/fit.py index 39ce9d8..92ee088 100644 --- a/building_boundary/shapes/fit.py +++ b/building_boundary/shapes/fit.py @@ -7,7 +7,7 @@ import numpy as np from scipy.spatial import ConvexHull from shapely.geometry import Polygon -from shapely.ops import cascaded_union +from shapely.ops import unary_union import concave_hull @@ -45,10 +45,11 @@ def compute_shape(points, alpha=None, k=None): if k is not None: boundary_points = concave_hull.compute(points, k, True) shape_ch = Polygon(boundary_points).buffer(0) - shape = cascaded_union([shape, shape_ch]) + if shape_ch.is_valid: + shape = unary_union([shape, shape_ch]) if type(shape) != Polygon: - shape = max(shape, key=lambda s: s.area) + shape = max(shape.geoms, key=lambda s: s.area) elif k is not None: boundary_points = concave_hull.compute(points, k, True)