Skip to main content

Toolbox for generating alpha shapes.

Project description

Alpha Shape Toolbox

PyPI version PyPI pyversions Travis CodeCov Binder Documentation Status MIT license

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.

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()

png

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()

png

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()

png

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()

png

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()

png

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()

png

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()

png

Determine the Alpha Shape

import alphashape
alpha_shape = alphashape.alphashape(gdf_proj)
alpha_shape.plot()

png

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()

png

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()

png

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

1.0.1 (2019-05-06)

  • Added gallery plot for optimized alpha function.
  • Documentation cleanup.

1.0.0 (2019-05-06)

0.1.10 (2019-05-05)

  • Correcting formatting on PyPi long description.

0.1.9 (2019-05-05)

0.1.8 (2019-05-05)

0.1.7 (2019-04-26)

  • Complete code coverage of existing capabilities.

0.1.6 (2019-04-24)

0.1.5 (2019-04-24)

0.1.4 (2019-04-24)

  • Bug fixes.

0.1.3 (2019-04-24)

  • Bug fixes.

0.1.2 (2019-04-24)

  • Bug fixes.

0.1.1 (2019-04-24)

  • Bug fixes.

0.1.0 (2019-04-23)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alphashape-1.0.2.tar.gz (15.9 kB view hashes)

Uploaded Source

Built Distribution

alphashape-1.0.2-py2.py3-none-any.whl (10.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page