Skip to content

Commit

Permalink
Merge pull request #278 from ljwolf/return-alpha-option
Browse files Browse the repository at this point in the history
Return alpha option & use pygeos for alphashaping if available
  • Loading branch information
darribas authored Apr 9, 2020
2 parents ed6eb00 + 978a5e6 commit 57c44c7
Show file tree
Hide file tree
Showing 28 changed files with 284 additions and 130 deletions.
331 changes: 222 additions & 109 deletions libpysal/cg/alpha_shapes.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion libpysal/cg/tests/data/alpha_05.cpg

This file was deleted.

Binary file removed libpysal/cg/tests/data/alpha_05.dbf
Binary file not shown.
Binary file added libpysal/cg/tests/data/alpha_05.gpkg
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_05.shp
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_05.shx
Binary file not shown.
1 change: 0 additions & 1 deletion libpysal/cg/tests/data/alpha_auto.cpg

This file was deleted.

Binary file removed libpysal/cg/tests/data/alpha_auto.dbf
Binary file not shown.
Binary file added libpysal/cg/tests/data/alpha_auto.gpkg
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_auto.shp
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_auto.shx
Binary file not shown.
1 change: 0 additions & 1 deletion libpysal/cg/tests/data/alpha_fifth.cpg

This file was deleted.

Binary file removed libpysal/cg/tests/data/alpha_fifth.dbf
Binary file not shown.
Binary file added libpysal/cg/tests/data/alpha_fifth.gpkg
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_fifth.shp
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_fifth.shx
Binary file not shown.
1 change: 0 additions & 1 deletion libpysal/cg/tests/data/alpha_fourth.cpg

This file was deleted.

Binary file removed libpysal/cg/tests/data/alpha_fourth.dbf
Binary file not shown.
Binary file added libpysal/cg/tests/data/alpha_fourth.gpkg
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_fourth.shp
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_fourth.shx
Binary file not shown.
1 change: 0 additions & 1 deletion libpysal/cg/tests/data/alpha_tenth.cpg

This file was deleted.

Binary file removed libpysal/cg/tests/data/alpha_tenth.dbf
Binary file not shown.
Binary file added libpysal/cg/tests/data/alpha_tenth.gpkg
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_tenth.shp
Binary file not shown.
Binary file removed libpysal/cg/tests/data/alpha_tenth.shx
Binary file not shown.
Binary file not shown.
78 changes: 62 additions & 16 deletions libpysal/cg/tests/test_ashapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,66 @@
try:
import geopandas
from shapely import geometry

GEOPANDAS_EXTINCT = False
except ImportError:
GEOPANDAS_EXTINCT = True

this_directory = os.path.dirname(__file__)


@skipIf(GEOPANDAS_EXTINCT, 'Geopandas is missing, so test will not run')
@skipIf(GEOPANDAS_EXTINCT, "Geopandas is missing, so test will not run")
class Test_Alpha_Shapes(TestCase):
def setUp(self):
eberly = geopandas.read_file(get_path('eberly_net.shp'))
eberly_vertices = eberly.geometry.apply(lambda x: np.hstack(x.xy).reshape(2, 2).T).values
eberly = geopandas.read_file(get_path("eberly_net.shp"))
eberly_vertices = eberly.geometry.apply(
lambda x: np.hstack(x.xy).reshape(2, 2).T
).values
eberly_vertices = np.vstack(eberly_vertices)
self.vertices = eberly_vertices

self.a05 = geopandas.read_file(os.path.join(this_directory, 'data/alpha_05.shp')).geometry.to_numpy().item()
self.a10 = geopandas.read_file(os.path.join(this_directory, 'data/alpha_tenth.shp')).geometry.to_numpy().item()
self.a2 = geopandas.read_file(os.path.join(this_directory, 'data/alpha_fifth.shp')).geometry.to_numpy().item()
self.a25 = geopandas.read_file(os.path.join(this_directory, 'data/alpha_fourth.shp')).geometry.to_numpy().item()
self.a25 = geopandas.read_file(os.path.join(this_directory, 'data/alpha_fourth.shp')).geometry.to_numpy().item()
self.a05 = (
geopandas.read_file(os.path.join(this_directory, "data/alpha_05.gpkg"))
.geometry.to_numpy()
.item()
)
self.a10 = (
geopandas.read_file(os.path.join(this_directory, "data/alpha_tenth.gpkg"))
.geometry.to_numpy()
.item()
)
self.a2 = (
geopandas.read_file(os.path.join(this_directory, "data/alpha_fifth.gpkg"))
.geometry.to_numpy()
.item()
)
self.a25 = (
geopandas.read_file(os.path.join(this_directory, "data/alpha_fourth.gpkg"))
.geometry.to_numpy()
.item()
)
self.a25 = (
geopandas.read_file(os.path.join(this_directory, "data/alpha_fourth.gpkg"))
.geometry.to_numpy()
.item()
)
circles = geopandas.read_file(
os.path.join(this_directory, "data/eberly_bounding_circles.gpkg")
)
self.circle_radii = circles.radius.iloc[0]
self.circle_verts = np.column_stack(
(circles.geometry.x.values, circles.geometry.y.values)
)

self.autoalpha = geopandas.read_file(
os.path.join(this_directory, "data/alpha_auto.gpkg")
).geometry[0]

self.autoalpha = geopandas.read_file(os.path.join(this_directory, 'data/alpha_auto.shp')).geometry[0]
def test_alpha_shapes(self):
new_a05 = alpha_shape(self.vertices, .05).to_numpy().item()
new_a10 = alpha_shape(self.vertices, .10).to_numpy().item()
new_a2 = alpha_shape(self.vertices, .2).to_numpy().item()
new_a25 = alpha_shape(self.vertices, .25).to_numpy().item()
new_a05 = alpha_shape(self.vertices, 0.05).to_numpy().item()
new_a10 = alpha_shape(self.vertices, 0.10).to_numpy().item()
new_a2 = alpha_shape(self.vertices, 0.2).to_numpy().item()
new_a25 = alpha_shape(self.vertices, 0.25).to_numpy().item()

assert new_a05.equals(self.a05)
assert new_a10.equals(self.a10)
Expand All @@ -46,9 +79,22 @@ def test_auto(self):
assert self.autoalpha.equals(auto_alpha)

def test_small_n(self):
new_singleton = alpha_shape(self.vertices[0].reshape(1,-1), .5)
new_singleton = alpha_shape(self.vertices[0].reshape(1, -1), 0.5)
assert isinstance(new_singleton, geometry.Polygon)
new_duo = alpha_shape(self.vertices[:1], .5)
new_duo = alpha_shape(self.vertices[:1], 0.5)
assert isinstance(new_duo, geometry.Polygon)
new_triple = alpha_shape(self.vertices[:2], .5)
new_triple = alpha_shape(self.vertices[:2], 0.5)
assert isinstance(new_triple, geometry.Polygon)
new_triple = alpha_shape_auto(
self.vertices[0].reshape(1, -1), return_circles=True
)
assert isinstance(new_triple[0], geometry.Polygon)
new_triple = alpha_shape_auto(self.vertices[:1], return_circles=True)
assert isinstance(new_triple[0], geometry.Polygon)
new_triple = alpha_shape_auto(self.vertices[:2], return_circles=True)
assert isinstance(new_triple[0], geometry.Polygon)

def test_circles(self):
ashape, radius, centers = alpha_shape_auto(self.vertices, return_circles=True)
np.testing.assert_allclose(radius, self.circle_radii)
np.testing.assert_allclose(centers, self.circle_verts)

0 comments on commit 57c44c7

Please sign in to comment.