- Saved searches
- Use saved searches to filter your results more quickly
- License
- shapely/shapely
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.rst
- About
- Installation#
- Installation from PyPI#
- Installation using conda#
- Installation from source with custom GEOS libary#
- Installation for local development#
- Testing Shapely#
- GEOS discovery (compile time)#
- GEOS discovery (runtime)#
- shapely 2.0.1
- What is a ufunc?
- Multithreading
- Usage
- Requirements
- Installing Shapely
- Integration
- Support
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Manipulation and analysis of geometric objects
License
shapely/shapely
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.rst
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 library GEOS (the engine of PostGIS, and a port of JTS). Shapely wraps GEOS geometries and operations to provide both a feature rich Geometry interface 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.
A universal function (or ufunc for short) is a function that operates on n-dimensional arrays on an element-by-element fashion and supports array broadcasting. The underlying for loops are implemented in C to reduce the overhead of the Python interpreter.
Shapely 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.
Here is the canonical example of building an approximately circular patch by buffering a point, using the scalar Geometry interface:
>>> from shapely import Point >>> patch = Point(0.0, 0.0).buffer(10.0) >>> patch >>> patch.area 313.6548490545941
Using the vectorized ufunc interface (instead of using a manual for loop), compare an array of points with a polygon:
>>> import shapely >>> import numpy as np >>> 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.io
We recommend installing Shapely using one of the available built distributions, for example using pip or conda :
$ pip install shapely # or using conda $ conda install shapely --channel conda-forge
See the installation documentation for more details and advanced installation instructions.
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)'
Shapely can also integrate with other Python GIS packages using GeoJSON-like dicts.
>>> import json >>> from shapely.geometry import mapping, shape >>> s = shape(json.loads(‘‘)) >>> s >>> print(json.dumps(mapping(s)))
Questions about using Shapely may be asked on the GIS StackExchange using the «shapely» tag.
Shapely is licensed under BSD 3-Clause license. GEOS is available under the terms of GNU Lesser General Public License (LGPL) 2.1 at https://libgeos.org.
About
Manipulation and analysis of geometric objects
Installation#
Built distributions don’t require compiling Shapely and its dependencies, and can be installed using pip or conda . In addition, Shapely is also available via some system package management tools like apt.
Installation from PyPI#
Shapely is available as a binary distribution (wheel) for Linux, macOS, and Windows platforms on PyPI. The distribution includes the most recent version of GEOS available at the time of the Shapely release. Install the binary wheel with pip as follows:
Installation using conda#
Shapely is available on the conda-forge channel. Install as follows:
$ conda install shapely --channel conda-forge
Installation from source with custom GEOS libary#
You may want to use a specific GEOS version or a GEOS distribution that is already present on your system (for compatibility with other modules that depend on GEOS, such as cartopy or osgeo.ogr). In such cases you will need to ensure the GEOS library is installed on your system and then compile Shapely from source yourself, by directing pip to ignore the binary wheels.
$ sudo apt install libgeos-dev # skip this if you already have GEOS $ pip install shapely --no-binary shapely
$ brew install geos # skip this if you already have GEOS $ pip install shapely --no-binary shapely
If you’ve installed GEOS to a standard location on Linux or macOS, the installation will automatically find it using geos-config . See the notes below on GEOS discovery at compile time to configure this.
We do not have a recipe for Windows platforms. The following steps should enable you to build Shapely yourself:
- Get a C compiler applicable to your Python version (https://wiki.python.org/moin/WindowsCompilers)
- Download and install a GEOS binary (https://trac.osgeo.org/osgeo4w/)
- Set GEOS_INCLUDE_PATH and GEOS_LIBRARY_PATH environment variables (see below for notes on GEOS discovery)
- Run pip install shapely —no-binary
- Make sure the GEOS .dll files are available on the PATH
Installation for local development#
This is similar to installing with a custom GEOS binary, but then instead of installing Shapely with pip from PyPI, you clone the package from Github:
$ git clone git@github.com:shapely/shapely.git $ cd shapely/
Install it in development mode using pip :
For development, use of a virtual environment is strongly recommended. For example using venv :
$ python3 -m venv . $ source bin/activate (env) $ pip install -e .[test]
$ conda create -n env python=3 geos numpy cython pytest $ conda activate env (env) $ pip install -e .
Testing Shapely#
Shapely can be tested using pytest :
$ pip install pytest # or shapely[test] $ pytest --pyargs shapely.tests
GEOS discovery (compile time)#
If GEOS is installed on Linux or macOS, the geos-config command line utility should be available and pip will find GEOS automatically. If the correct geos-config is not on the PATH, you can add it as follows (on Linux/macOS):
$ export PATH=/path/to/geos/bin:$PATH
Alternatively, you can specify where Shapely should look for GEOS library and header files using environment variables (on Linux/macOS):
$ export GEOS_INCLUDE_PATH=/path/to/geos/include $ export GEOS_LIBRARY_PATH=/path/to/geos/lib
On Windows, there is no geos-config and the include and lib folders need to be specified manually in any case:
$ set GEOS_INCLUDE_PATH=C:\path\to\geos\include $ set GEOS_LIBRARY_PATH=C:\path\to\geos\lib
Common locations of GEOS (to be suffixed by lib , include or bin ):
- Anaconda (Linux/macOS): $CONDA_PREFIX/Library
- Anaconda (Windows): %CONDA_PREFIX%\Library
- OSGeo4W (Windows): C:\OSGeo4W64
GEOS discovery (runtime)#
Shapely is dynamically linked to GEOS. This means that the same GEOS library that was used during Shapely compilation is required on your system at runtime. When using Shapely that was distributed as a binary wheel or through conda, this is automatically the case and you can stop reading.
In other cases this can be tricky, especially if you have multiple GEOS installations next to each other. We only include some guidelines here to address this issue as this document is not intended as a general guide of shared library discovery.
If you encounter exceptions like:
ImportError: libgeos_c.so.1: cannot open shared object file: No such file or directory
You will have to make the shared library file available to the Python interpreter. There are in general four ways of making Python aware of the location of shared library:
- Copy the shared libraries into the shapely module directory (this is how Windows binary wheels work: they are distributed with the correct dlls in the shapely module directory)
- Copy the shared libraries into the library directory of the Python interpreter (this is how Anaconda environments work)
- Copy the shared libraries into some system location ( C:\Windows\System32 ; /usr/local/lib , this happens if you installed GEOS through apt or brew )
- Add the shared library location to a the dynamic linker path variable at runtime. (Advanced usage; Linux and macOS only; on Windows this method was deprecated in Python 3.8)
The filenames of the GEOS shared libraries are:
- On Linux: libgeos-*.so.*, libgeos_c-*.so.*
- On macOS: libgeos.dylib, libgeos_c.dylib
- On Windows: geos-*.dll, geos_c-*.dll
Note that Shapely does not make use of any RUNPATH (RPATH) header. The location of the GEOS shared library is not stored inside the compiled Shapely library.
shapely 2.0.1
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 library GEOS (the engine of PostGIS, and a port of JTS). Shapely wraps GEOS geometries and operations to provide both a feature rich Geometry interface 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 on n-dimensional arrays on an element-by-element fashion and supports array broadcasting. The underlying for loops are implemented in C to reduce the overhead of the Python interpreter.
Multithreading
Shapely 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.
Usage
Here is the canonical example of building an approximately circular patch by buffering a point, using the scalar Geometry interface:
See the documentation for more examples and guidance: https://shapely.readthedocs.io
Requirements
Installing Shapely
We recommend installing Shapely using one of the available built distributions, for example using pip or conda :
pip install shapely or using conda conda install shapely --channel conda-forge
See the installation documentation for more details and advanced installation instructions.
Integration
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.
Shapely can also integrate with other Python GIS packages using GeoJSON-like dicts.
Support
Questions about using Shapely may be asked on the GIS StackExchange using the “shapely” tag.