Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes to run this code in 2023 #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion building_boundary/core/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
14 changes: 9 additions & 5 deletions building_boundary/shapes/alpha_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions building_boundary/shapes/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down