Returns the geometry that represents the shared area between two geometry or geography objects.
Syntax
geometry ST_Intersection(geometry geomA, geometry geomB);
geography ST_Intersection(geography geogA, geography geogB);
geometry ST_Intersection(geometry set gField);Parameters
| Parameter | Description |
|---|---|
geomA / geomB | The two geometry objects to intersect. |
geogA / geogB | The two geography objects to intersect. |
gField | The geometry field to aggregate across all rows. |
Usage notes
If the two input geometry objects do not intersect, the function returns an empty geometry object.
For geography inputs, the function converts them to geometry objects first, computes the intersection in the planar spatial reference, and then transforms the result back to a geography object using the World Geodetic System 1984 (WGS 84) coordinate system.
GeometryCollection objects are not supported.
M coordinates are dropped from the input objects.
Examples
Compute the intersection of two polygons
SELECT ST_AsText(
ST_Intersection(
'POLYGON((0 0,0 2,2 2,2 0,0 0))'::geometry,
'POLYGON((0 0,3 0,3 1,0 1,0 0))'::geometry
)
);Result:
st_astext
--------------------------------
POLYGON((0 0,0 1,2 1,2 0,0 0))
(1 row)Compute the aggregate intersection of a geometry set
The aggregate form of ST_Intersection takes a geometry field as input and intersects all geometries in the set sequentially, returning the shared area across all of them.
-- Create a table and insert sample geometries
CREATE TABLE agg_result (id integer, geom geometry);
INSERT INTO agg_result VALUES
(0, ST_GeomFromText('POLYGON((0 0, 0 0.5, 0.5 0.5, 0.5 0, 0 0))'));
INSERT INTO agg_result
SELECT i, st_buffer('POINT(0 0)', 0.8 + random() * 0.1)
FROM generate_series(1, 100) AS i;
-- Compute the intersection across all rows
SELECT ST_AsText(ST_Intersection(geom)) AS intersection_result
FROM agg_result;Result:
st_astext
----------------------------------------
POLYGON((0 0,0 0.5,0.5 0.5,0.5 0,0 0))
(1 row)The small square polygon (row 0) is entirely contained within all the buffered circles (rows 1–100), so it is returned as the intersection of the entire set.
