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.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alphashape-1.1.0-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file alphashape-1.1.0.tar.gz.

File metadata

  • Download URL: alphashape-1.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

File hashes

Hashes for alphashape-1.1.0.tar.gz
Algorithm Hash digest
SHA256 33d2bf4b7c4eeb413e071521d38535441f25218afc12a8804ac693d9e93bb94e
MD5 e142d5e8415562d56012b7904b671c54
BLAKE2b-256 1969fcf09ca0c7b3fa555efca26981b935539ed8418125cf33143e7acea0abe0

See more details on using hashes here.

File details

Details for the file alphashape-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: alphashape-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

File hashes

Hashes for alphashape-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d4efab9fb154ce3b992c3026a0a14482406a92913ff9057581d6087fbf88f23c
MD5 e58b2428e554d25beb642519689a591b
BLAKE2b-256 b354e96d3c4730eb7676b7617b2d8a2aa63b29942f6aee6c58d06e8c7661b4e9

See more details on using hashes here.

Supported by

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