Shapely
Manipulation and analysis of planar geometric objects in Python
Repository Health
Technical Analysis
Shapely is a Python library for the manipulation and analysis of planar geometric objects. It wraps the mature GEOS library (the geometry engine behind PostGIS) and exposes it through a clean Python interface for creating geometries and computing spatial relationships, measurements, and set operations.
Beyond a scalar Geometry interface, Shapely offers a NumPy-style vectorized ufunc API that operates element-by-element over arrays of geometries with the heavy lifting done in C. Its functions release the GIL, allowing GEOS computations to run in parallel across threads from a single Python process.
What You Get
- Geometry types for Point, LineString, Polygon, and their Multi- and collection variants
- Set-theoretic operations: intersection, union, difference, and symmetric difference
- Spatial predicates such as contains, intersects, within, touches, and crosses
- Measurements and constructive operations like area, length, distance, buffer, and convex hull
- A vectorized NumPy ufunc interface that releases the GIL for multithreaded, array-wide geometry processing
Common Use Cases
- Buffering a point or line to create a surrounding region
- Testing whether geometries intersect, contain, or are within one another
- Computing areas, lengths, and distances for spatial features
- Batch-processing large arrays of geometries with the vectorized interface
Under The Hood
Architecture Shapely is split into a Cython/C binding layer (_geos.pyx, _geometry_helpers.pyx, _geos.pxd, _pygeos_api.pxd) that talks to the GEOS C API and a broad set of pure-Python functional modules organized by concern — constructive.py, set_operations.py, predicates.py, measurement.py, creation.py, coordinates.py, linear.py, ops.py, and affinity.py — plus a geometry/ package for the scalar object interface and _ragged_array.py for array interop. Tech Stack Written in Python and Cython over the GEOS C++ library and NumPy, packaged with prebuilt wheels bundling GEOS; the vectorized layer is exposed as NumPy ufuncs whose inner loops run in C. Code Quality The repository maintains an extensive shapely/tests suite (constructive, creation, coordinates, coverage, predicates, geometry, and legacy tests) with a conftest.py, runs CI with coverage reporting, and reflects a mature 2,400+ commit, 175-contributor codebase. API Design The dual interface is a deliberate strength: newcomers use intuitive object methods like Point(0,0).buffer(10), while performance-sensitive users call the same operations as vectorized array functions; GIL release during GEOS calls means the ergonomic API also scales to multithreaded workloads.