Alpha Shapes

1.3.1 · active · verified Sat Apr 11

Alpha Shape Toolbox is a Python library for generating n-dimensional alpha shapes, which are a generalization of convex hulls used to create bounding polygons around sets of points. It offers workflows to manually define the alpha parameter or optimize its value to best fit the data. As of version 1.3.1 (released April 16, 2021), the library supports 3D input data and GeoJSON output for its command-line interface. It is actively maintained with recent updates addressing Python version compatibility.

Warnings

Install

Imports

Quickstart

This example demonstrates how to generate a 2D alpha shape from a set of points and visualize it using Matplotlib and Descartes. The `alpha` parameter controls the 'tightness' of the shape; a value of 0.0 will typically yield the convex hull.

import alphashape
import matplotlib.pyplot as plt
from descartes import PolygonPatch

# Define input points (2D example)
points = [(0., 0.), (0., 1.), (1., 1.), (1., 0.), (0.5, 0.5)]

# Define alpha parameter (0.0 often gives the convex hull)
alpha = 0.5

# Generate the alpha shape
alpha_shape = alphashape.alphashape(points, alpha)

# Initialize plot (requires matplotlib and descartes)
fig, ax = plt.subplots()

# Plot input points
ax.scatter(*zip(*points))

# Plot alpha shape
# Note: If plotting issues occur, a common fix for descartes/shapely incompatibility
# is to edit `descartes/patch.py` line 62 from `t.exterior` to `t.exterior.coords`.
ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2, fc='blue', ec='black'))
ax.set_title(f'Alpha Shape (alpha={alpha})')
plt.xlabel('X-coordinate')
plt.ylabel('Y-coordinate')
plt.show()

view raw JSON →