Geometric objects, predicates, and operations
PostGIS-ish operations outside a database context for Pythoneers and Pythonistas.
Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects. 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. Shapely is not concerned with data formats or coordinate systems, but can be readily integrated with packages that are. For more details, see:
Shapely 1.2 depends on:
- Python >=2.5,<3
- 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 (for example), so Shapely 1.2 adds a xy property for obtaining 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, exercised via setup.py.:
$ python setup.py test
Nosetests won’t run the tests properly; Zope doctest suites are not currently supported well by nose.
Bugs may be reported and questions asked via https://github.com/Toblerity/Shapely.
Shapely is written by:
- Sean Gillies
- Aron Bierbaum
- Kai Lautaportti
- Oliver Tonnhofer
Patches contributed by:
- Howard Butler
- Frédéric Junod
- Eric Lemoine
- Jonathan Tartley
- Kristian Thy
- Mike Toews (https://github.com/mwtoews)
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)
- Add ops.unary_union function.
- Alias ops.cascaded_union to ops.unary_union when GEOS CAPI >= (1,7,0).
- Add geos_version_string attribute to shapely.geos.
- Ensure parent is set when child geometry is accessed.
- Generate _speedups.c using Cython when building from repo when missing, stale, or the build target is “sdist”.
- The is_simple predicate of invalid, self-intersecting linear rings now returns False.
- Remove VERSION.txt from repo, it’s now written by the distutils setup script with value of shapely.__version__.
- Eliminate numerical sensitivity in a method chaining test (Debian bug #663210).
- Account for cascaded union of random buffered test points being a polygon or multipolygon (Debian bug #666655).
- Use Cython to build speedups if it is installed.
- Avoid stumbling over SVN revision numbers in GEOS C API version strings.
- A geometry’s coords property is now sliceable, yielding a list of coordinate values.
- Homogeneous collections are now sliceable, yielding a new collection of the same type.
- Fixed errors in speedups on 32bit systems when GEOS references memory above 2GB.
- Add shapely.__version__ attribute.
- Update the manual.
- Build Windows distributions with VC7 or VC9 as appropriate.
- More verbose report on failure to speed up.
- Fix for prepared geometries broken in 1.2.11.
- DO NOT INSTALL 1.2.11
- Ignore AttributeError during exit.
- PyPy 1.5 support.
- Prevent operation on prepared geometry crasher (#12).
- Optional Cython speedups for Windows.
- Linux 3 platform support.
- Add optional Cython speedups.
- Add is_cww predicate to LinearRing.
- Add function that forces orientation of Polygons.
- Disable build of speedups on Windows pending packaging work.
- Remove extra glob import.
- Move examples to shapely.examples.
- Add box() constructor for rectangular polygons.
- Fix extraneous imports.
- New parallel_offset method (#6).
- Support for Python 2.4.
- Support for Windows eggs.
- The geoms property of an empty collection yields  instead of a ValueError (#3).
- The coords and geometry type sproperties have the same behavior as above.
- Ensure that z values carry through into products of operations (#4).
- Stop distributing docs/_build.
- Include library fallbacks in test_dlls.py for linux platform.
- Raise AttributeError when there’s no backend support for a method.
- Raise OSError if libgeos_c.so (or variants) can’t be found and loaded.
- Add geos_c DLL loading support for linux platforms where find_library doesn’t work.
- Add mapping function.
- Fix problem with GEOSisValidReason symbol for GEOS < 3.1.
- Add representative_point method.
- Fixed bounds of singular polygons.
- Added shapely.validation.explain_validity function (#226).
- Final release.
- Add examples and tests to MANIFEST.in.
- Release candidate 2.
- Release candidate.
- Memory leak associated with new empty geometry state fixed.
- Broken GeometryCollection fixed.
- Objects can be constructed from others of the same type, thereby making copies. Collections can be constructed from sequences of objects, also making copies.
- Collections are now iterators over their component objects.
- New code for manual figures, using the descartes package.
- Adds support for the “sunos5” platform.
- Only provide simplification implementations for GEOS C API >= 1.5.
- Fix cascaded_union bug introduced in 1.2b1 (#212).
- Update the README. Remove cruft from setup.py. Add some version 1.2 metadata regarding required Python version (>=2.5,<3) and external dependency (libgeos_c >= 3.1).
- Add accessor for separate arrays of X and Y values (#210).
TODO: fill gap here
- Proper prototyping of WKB writer, and avoidance of errors on 64-bit systems (#191).
- Prototype libgeos_c functions in a way that lets py2exe apps import shapely (#189).
1.2 Branched (2009-09-19)
- Fix for references held by topology and predicate descriptors.
- Work around bug in GEOS 2.2.3, GEOSCoordSeq_getOrdinate not exported properly (#178).
- Fixed compatibility with GEOS 2.2.3 that was broken in 1.0.8 release (#176).
- Find and load MacPorts libgeos.
- Fill out GEOS function result and argument types to prevent faults on a 64-bit arch.
- Polygon rings now have the same dimensions as parent (#168).
- Eliminated reference cycles in polygons (#169).
- Fixed adaptation of multi polygon data.
- Raise exceptions earlier from binary predicates.
- Beginning distributing new windows DLLs (#166).
- Added access to GEOS polygonizer function.
- Raise exception when insufficient coordinate tuples are passed to LinearRing constructor (#164).
- Disentangle Python and topological equality (#163).
- Add shape(), a factory that copies coordinates from a geo interface provider. To be used instead of asShape() unless you really need to store coordinates outside shapely for efficient use in other code.
- Cache GEOS geometries in adapters (#163).
- Do not release GIL when calling GEOS functions (#158).
- Prevent faults when chaining multiple GEOS operators (#159).
- Fix loss of dimensionality in polygon rings (#155).
- Allow chaining expressions involving coordinate sequences and geometry parts (#151).
- Protect against abnormal use of coordinate accessors (#152).
- Coordinate sequences now implement the numpy array protocol (#153).
- Final release.
1.0 RC2 (2008-01-16)
- Added temporary solution for #149.
1.0 RC1 (2008-01-14)
- First release candidate