Geometric objects, predicates, and operations
Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects. It is not concerned with data formats or coordinate systems. It is based on the widely deployed GEOS (the engine of PostGIS) and JTS (from which GEOS is ported) libraries. This C dependency is traded for the ability to execute with blazing speed.
In a nutshell: Shapely lets you do PostGIS-ish stuff outside the context of a database using idiomatic Python. For more details, see:
Shapely 1.2 depends on:
libgeos_c >=3.1 (3.0 and below have not been tested, YMMV)
Windows users should use the executable installer, which contains the required GEOS DLL. Other users should acquire libgeos_c by any means, make sure that it is on the system library path, and install from the Python package index:
$ pip install Shapely
or from a source distribution with the setup script:
$ python setup.py install
Here is the canonical example of building an approximately circular patch by buffering a point:
>>> from shapely.geometry import Point >>> patch = Point(0.0, 0.0).buffer(10.0) >>> patch <shapely.geometry.polygon.Polygon object at 0x...> >>> patch.area 313.65484905459385
Shapely does not read or write data files, but it can serialize and deserialize using several well known formats and protocols. The shapely.wkb and shapely.wkt modules provide dumpers and loaders inspired by Python’s pickle module.:
>>> from shapely.wkt import dumps, loads >>> dumps(loads('POINT (0 0)')) 'POINT (0.0000000000000000 0.0000000000000000)'
All linear objects, such as the rings of a polygon (like patch above), provide the Numpy array interface.:
>>> from numpy import asarray >>> ag = asarray(patch.exterior) >>> ag array([[ 1.00000000e+01, 0.00000000e+00], [ 9.95184727e+00, -9.80171403e-01], [ 9.80785280e+00, -1.95090322e+00], ... [ 1.00000000e+01, 0.00000000e+00]])
That yields a numpy array of [x, y] arrays. This is not always exactly what one wants for plotting shapes with Matplotlib, so Shapely 1.2 adds a xy property for getting separate arrays of coordinate x and y values.:
>>> x, y = patch.exterior.xy >>> ax = asarray(x) >>> ax array([ 1.00000000e+01, 9.95184727e+00, 9.80785280e+00, ...])
Numpy arrays can also be adapted to Shapely linestrings:
>>> from shapely.geometry import asLineString >>> asLineString(ag).length 62.806623139095073 >>> asLineString(ag).wkt 'LINESTRING (10.0000000000000000 0.0000000000000000, ...)'
Shapely uses a Zope-stye suite of unittests and doctests, excercised via setup.py.:
$ python setup.py test
For current information about this project, see the wiki.
If you have questions, please consider joining our community list:
Shapely is written by:
Patches contributed by:
Additional help from:
Justin Bronn (GeoDjango) for ctypes inspiration
Martin Davis (JTS)
Jaakko Salli for the Windows distributions
Sandro Santilli, Mateusz Loskot, Paul Ramsey, et al (GEOS Project)
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.