Package: shapely
Manipulation and analysis of geometric objects in the Cartesian plane.Shapely is a BSD-licensed Python package for manipulation and analysis of
planar geometric objects. It is using the widely deployed open-source
geometry libraryGEOS(the engine ofPostGIS, and a port ofJTS).
Shapely wraps GEOS geometries and operations to provide both a feature richGeometryinterface for singular (scalar) geometries and higher-performance
NumPy ufuncs for operations using arrays of geometries.
Shapely is not primarily focused on data serialization formats or coordinate
systems, but can be readily integrated with packages that are.What is a ufunc?A universal function (or ufunc for short) is a function that operates onn-dimensional arrays on an element-by-element fashion and supports array
broadcasting. The underlyingforloops are implemented in C to reduce the
overhead of the Python interpreter.MultithreadingShapely functions generally support multithreading by releasing the Global
Interpreter Lock (GIL) during execution. Normally in Python, the GIL prevents
multiple threads from computing at the same time. Shapely functions
internally release this constraint so that the heavy lifting done by GEOS can
be done in parallel, from a single Python process.UsageHere is the canonical example of building an approximately circular patch by
buffering a point, using the scalar Geometry interface:>>>fromshapelyimportPoint>>>patch=Point(0.0,0.0).buffer(10.0)>>>patch<POLYGON ((10 0, 9.952 -0.98, 9.808 -1.951, 9.569 -2.903, 9.239 -3.827, 8.81...>>>>patch.area313.6548490545941Using the vectorized ufunc interface (instead of using a manual for loop),
compare an array of points with a polygon:>>>importshapely>>>importnumpyasnp>>>geoms=np.array([Point(0,0),Point(1,1),Point(2,2)])>>>polygon=shapely.box(0,0,2,2)>>>shapely.contains(polygon,geoms)array([False,True,False])See the documentation for more examples and guidance:https://shapely.readthedocs.ioRequirementsShapely 2.0 requiresPython >=3.7GEOS >=3.5NumPy >=1.14Installing ShapelyWe recommend installing Shapely using one of the available built
distributions, for example usingpiporconda:$pipinstallshapely#orusingconda$condainstallshapely--channelconda-forgeSee theinstallation documentationfor more details and advanced installation instructions.IntegrationShapely 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.>>>fromshapely.wktimportdumps,loads>>>dumps(loads('POINT (0 0)'))'POINT (0.0000000000000000 0.0000000000000000)'Shapely can also integrate with other Python GIS packages using GeoJSON-like
dicts.>>>importjson>>>fromshapely.geometryimportmapping,shape>>>s=shape(json.loads('{"type": "Point", "coordinates": [0.0, 0.0]}'))>>>s<POINT (0 0)>>>>print(json.dumps(mapping(s))){"type": "Point", "coordinates": [0.0, 0.0]}SupportQuestions about using Shapely may be asked on theGIS StackExchangeusing the “shapely”
tag.Bugs may be reported athttps://github.com/shapely/shapely/issues.Copyright & LicenseShapely is licensed under BSD 3-Clause license.
GEOS is available under the terms of GNU Lesser General Public License (LGPL) 2.1 athttps://libgeos.org.