Shapely Geometry — Core Concepts
Shapely is a Python library for manipulating and analyzing planar geometric objects. It wraps the GEOS library (the same engine behind PostGIS) and provides Pythonic access to geometry creation, spatial predicates, and set-theoretic operations.
Geometry types
Shapely implements the core types from the Simple Features specification:
from shapely.geometry import Point, LineString, Polygon, MultiPoint
point = Point(2.0, 3.0)
line = LineString([(0, 0), (1, 1), (2, 0)])
polygon = Polygon([(0, 0), (4, 0), (4, 3), (0, 3)])
- Point — a single coordinate pair (or triple for 3D).
- LineString — an ordered sequence of points forming a path.
- LinearRing — a closed LineString (used as polygon boundaries).
- Polygon — an exterior ring with optional holes.
- Multi-variants —
MultiPoint,MultiLineString,MultiPolygonfor collections.
Polygons with holes
exterior = [(0, 0), (10, 0), (10, 10), (0, 10)]
hole = [(3, 3), (7, 3), (7, 7), (3, 7)]
polygon_with_hole = Polygon(exterior, [hole])
print(polygon_with_hole.area) # 100 - 16 = 84
Spatial predicates
Predicates answer yes/no questions about spatial relationships:
park = Polygon([(0, 0), (5, 0), (5, 5), (0, 5)])
house = Point(3, 3)
river = LineString([(0, 6), (5, 6)])
park.contains(house) # True — the point is inside the polygon
house.within(park) # True — inverse of contains
park.intersects(river) # False — they do not touch
park.touches(river) # False
park.disjoint(river) # True — completely separate
DE-9IM relationships
For complex spatial reasoning, Shapely exposes the DE-9IM (Dimensionally Extended 9-Intersection Model) matrix:
park.relate(house) # "0FFFFF212" — encodes the full topological relationship
This is the same predicate system used in PostGIS spatial queries.
Set operations
Combine or subtract geometries like set operations:
a = Polygon([(0, 0), (3, 0), (3, 3), (0, 3)])
b = Polygon([(2, 0), (5, 0), (5, 3), (2, 3)])
a.intersection(b) # The overlapping rectangle
a.union(b) # Combined shape covering both
a.difference(b) # Part of A not in B
a.symmetric_difference(b) # Parts in either but not both
These operations return new geometry objects that you can inspect, measure, or pass to further operations.
Measurements
polygon = Polygon([(0, 0), (4, 0), (4, 3), (0, 3)])
polygon.area # 12.0
polygon.perimeter # 14.0 (use .length for lines)
polygon.centroid # POINT (2 1.5)
polygon.bounds # (0.0, 0.0, 4.0, 3.0) — bounding box
line = LineString([(0, 0), (3, 4)])
line.length # 5.0
Important caveat: Shapely works in a flat Cartesian plane. If your coordinates are latitude/longitude, areas and distances are in degrees, not meters. For geographic calculations, project coordinates to a local CRS (e.g., UTM) first, or use libraries like pyproj for transformation.
Buffering
Create a zone around any geometry:
point = Point(0, 0)
buffer = point.buffer(1.0) # Circle with radius 1.0
print(buffer.area) # ~3.14
line = LineString([(0, 0), (5, 0)])
corridor = line.buffer(0.5) # Rectangle-like corridor around the line
Buffering is how you answer questions like “what is within 500 meters of this road?” — buffer the road geometry by 500 meters, then check what intersects.
Common misconception
People often assume Shapely handles coordinate reference systems (CRS) and map projections. It does not — Shapely operates on raw numbers with no concept of units or projections. If you pass latitude/longitude, it computes areas in “square degrees,” which is meaningless. Always project to meters-based coordinates before measuring real-world areas and distances.
When to use Shapely
- Geofencing: Is a delivery driver inside the allowed zone?
- Overlap analysis: Which parcels overlap with the flood plain?
- Route buffers: What properties are within 200m of the proposed highway?
- Data cleaning: Remove points that fall outside valid boundaries.
The one thing to remember: Shapely gives Python the spatial math engine to create, test, and combine geometric shapes — but always project your coordinates to the right system before measuring real-world areas or distances.
See Also
- Python Adaptive Learning Systems How Python builds learning apps that adjust to each student like a personal tutor who knows exactly what you need next.
- Python Airflow Learn Airflow as a timetable manager that makes sure data tasks run in the right order every day.
- Python Altair Learn Altair through the idea of drawing charts by describing rules, not by hand-placing every visual element.
- Python Automated Grading How Python grades homework and exams automatically, from simple answer keys to understanding written essays.
- Python Batch Vs Stream Processing Batch processing is like doing laundry once a week; stream processing is like a self-cleaning shirt that cleans itself constantly.