Git Product home page Git Product logo

alphashape's Introduction

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.

alphashape's People

Contributors

bellockk avatar ngodber avatar tigerhawkvok avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.