Shapely

2.1.2 · active · verified Sat Mar 28

Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects. It wraps the widely deployed open-source geometry library GEOS (the engine of PostGIS) to provide both a feature-rich Geometry interface for scalar geometries and high-performance NumPy ufuncs for operations on arrays of geometries. The current version is 2.1.2, with a regular release cadence that includes bug fixes, new features, and compatibility updates for Python and GEOS versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating basic `Point` and `Polygon` geometries and performing a simple spatial intersection. It also includes an example of a vectorized operation using NumPy arrays, a key performance feature introduced in Shapely 2.0.

import shapely
from shapely import Point, Polygon

# Create a point and a polygon
point = Point(0, 0)
polygon = Polygon([(0, 0), (1, 1), (1, 0)])

# Perform a spatial operation
intersection = point.intersection(polygon)

print(f"Point: {point}")
print(f"Polygon: {polygon}")
print(f"Intersection: {intersection}")
print(f"Point within polygon: {polygon.contains(point)}")

# Example of a vectorized operation (Shapely 2.0+)
import numpy as np
geoms = np.array([Point(0.5, 0.5), Point(1.5, 0.5)])
print(f"\nVectorized contains: {shapely.contains(polygon, geoms)}")

view raw JSON →