Toolbox for generating alpha shapes.
Project description
Alpha Shape Toolbox
Toolbox for generating alpha shapes.
Alpha shapes are often used to generalize bounding polygons containing sets of points. The alpha parameter is defined as the value a
, such that an edge of a disk of radius 1/a
can be drawn between any two edge members of a set of points and still contain all the points. The convex hull, a shape resembling what you would see if you wrapped a rubber band around pegs at all the data points, is an alpha shape where the alpha parameter is equal to zero. In this toolbox we will be generating alpha complexes, which are closely related to alpha shapes, but which consist of straight lines between the edge points instead of arcs of circles.
https://en.wikipedia.org/wiki/Alpha_shape
https://en.wikipedia.org/wiki/Convex_hull
Creating alpha shapes around sets of points usually requires a visually interactive step where the alpha parameter for a concave hull is determined by iterating over or bisecting values to approach a best fit. The alpha shape toolbox will provide workflows to shorten the development loop on this manual process, or to bypass it completely by solving for an alpha shape with particular characteristics. A python API will be provided to aid in the scripted generation of alpha shapes. A console application will also be provided as an example usage of the alpha shape toolbox, and to facilitate generation of alpha shapes from the command line. In addition to the utility of being able to quickly iterate on alpha parameters to generate concave hulls, the framework of this tool set will provide a template for generating GIS tool sets accessible from python, the console, or graphical user interfaces without the licensing restrictions of proprietary tool sets and without having a python installation for the console and user interface.
 Free software: MIT license
 Documentation: https://alphashape.readthedocs.io.
Features
Import Dependencies
import sys from descartes import PolygonPatch import matplotlib.pyplot as plt import alphashape
Define a set of points
points = [(0., 0.), (0., 1.), (1., 1.), (1., 0.), (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]
Plotting the input points
fig, ax = plt.subplots() ax.scatter(*zip(*points)) plt.show()
Generate an Alpha Shape (Alpha=0.0) (Convex Hull)
Every convex hull is an alpha shape, but not every alpha shape is a convex hull. When the alphashape
function is called with an alpha parameter of 0, a convex hull will always be returned.
Create the alpha shape
alpha_shape = alphashape.alphashape(points, 0.)
Plotting the alpha shape over the input data
fig, ax = plt.subplots() ax.scatter(*zip(*points)) ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2)) plt.show()
Generate an Alpha Shape (Alpha=2.0) (Concave Hull)
As we increase the alpha parameter value, the bounding shape will begin to fit the sample data with a more tightly fitting bounding box.
Create the alpha shape
alpha_shape = alphashape.alphashape(points, 2.0)
Plotting the alpha shape over the input data
fig, ax = plt.subplots() ax.scatter(*zip(*points)) ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2)) plt.show()
Generate an Alpha Shape (Alpha=3.5)
If you go too high on the alpha parameter, you will start to lose points from the original data set.
Create the alpha shape
alpha_shape = alphashape.alphashape(points, 3.5)
Plotting the alpha shape over the input data
fig, ax = plt.subplots() ax.scatter(*zip(*points)) ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2)) plt.show()
Generate an Alpha Shape (Alpha=5.0)
If you go too far, you will lose everything.
alpha_shape = alphashape.alphashape(points, 5.0) print(alpha_shape)
GEOMETRYCOLLECTION EMPTY
Generate an Alpha Shape by Solving for an Optimal Alpha Value
The alpha parameter can be solved for if it is not provided as an argument, but with large datasets this can take a long time to calculate.
Create the alpha shape
alpha_shape = alphashape.alphashape(points)
Plotting the alpha shape over the input data
fig, ax = plt.subplots() ax.scatter(*zip(*points)) ax.add_patch(PolygonPatch(alpha_shape, alpha=0.2)) plt.show()
Alpha Shapes with GeoPandas
Sample Data
The data used in this notebook can be obtained from the Alaska Department of Transportation and Public Facilities website at the link below. It consists of a point collection for each of the public airports in Alaska.
http://www.dot.alaska.gov/stwdplng/mapping/shapefiles.shtml
Load the Shapefile
import os import geopandas data = os.path.join(os.getcwd(), 'data', 'Public_Airports_March2018.shp') gdf = geopandas.read_file(data)
%matplotlib inline gdf.plot()
gdf.crs
{'init': 'epsg:4269'}
Generate Alpha Shape
The alpha shape will be generated in the coordinate frame the geodataframe is in. In this example, we will project into an Albers Equal Area projection, construct our alpha shape in that coordinate system, and then convert back to the source projection.
Project to Albers Equal Area Spatial Reference
import cartopy.crs as ccrs gdf_proj = gdf.to_crs(ccrs.AlbersEqualArea().proj4_init) gdf_proj.plot()
Determine the Alpha Shape
import alphashape alpha_shape = alphashape.alphashape(gdf_proj) alpha_shape.plot()
Plotting the Alpha Shape over the Data Points
Plate Carree Projection
import matplotlib.pyplot as plt ax = plt.axes(projection=ccrs.PlateCarree()) ax.scatter([p.x for p in gdf_proj['geometry']], [p.y for p in gdf_proj['geometry']], transform=ccrs.AlbersEqualArea()) ax.add_geometries( alpha_shape['geometry'], crs=ccrs.AlbersEqualArea(), alpha=.2) plt.show()
Robinson Projection
import matplotlib.pyplot as plt ax = plt.axes(projection=ccrs.Robinson()) ax.scatter([p.x for p in gdf_proj['geometry']], [p.y for p in gdf_proj['geometry']], transform=ccrs.AlbersEqualArea()) ax.add_geometries( alpha_shape['geometry'], crs=ccrs.AlbersEqualArea(), alpha=.2) plt.show()
Credits
This package was created with Cookiecutter and the audreyr/cookiecutterpypackage project template.
History
1.2.0 (20210225)
 Updated dependencies for geopandas notebook examples.
 Updated source information for Alaska Airports example data set.
 Dropping support for Python 3.6.
1.1.0 (20200819)
 Updated dependency version numbers.
 Including optional bounds for alpha paramter solver.
1.0.1 (20190506)
 Added gallery plot for optimized alpha function.
 Documentation cleanup.
1.0.0 (20190506)
0.1.10 (20190505)
 Correcting formatting on PyPi long description.
0.1.9 (20190505)
0.1.8 (20190505)
0.1.7 (20190426)
 Complete code coverage of existing capabilities.
0.1.6 (20190424)
0.1.5 (20190424)
0.1.4 (20190424)
 Bug fixes.
0.1.3 (20190424)
 Bug fixes.
0.1.2 (20190424)
 Bug fixes.
0.1.1 (20190424)
 Bug fixes.
0.1.0 (20190423)
 First release on PyPI.
Project details
Release history Release notifications  RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size  File type  Python version  Upload date  Hashes 

Filename, size alphashape1.2.0py2.py3noneany.whl (10.6 kB)  File type Wheel  Python version py2.py3  Upload date  Hashes View 
Filename, size alphashape1.2.0.tar.gz (18.0 kB)  File type Source  Python version None  Upload date  Hashes View 
Hashes for alphashape1.2.0py2.py3noneany.whl
Algorithm  Hash digest  

SHA256  3afbb396f0988e8adb440d740d574b6bf474d25061131a8df98fe7cbc67d3c7b 

MD5  8c02a0685bdfa755f9ea1167b7bc0a48 

BLAKE2256  7dfe0c6618a0745681cb43e8688fc2d0e239938314896f0004506668e7b422df 