ST_Intersection returns a geometry object representing the set of points shared by two input geometry objects. Use it with ST_Intersects to clip geometries in bounding box, buffer, or region queries—for example, to extract only the portion of a road or boundary that falls within a given area.
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 on which to perform an aggregate intersection. |
Behavior
If the two input geometry objects do not intersect, the function returns an empty geometry object.
This function does not support GeometryCollection objects.
The aggregate form performs an intersection operation on all geometry objects in sequence and returns the intersection of the geometry objects.
This function drops M coordinates from the input objects.
For geography inputs, the function processes the objects by using geometry functions. It first converts the geography objects to geometry, calculates the intersection in the planar spatial reference, and then transforms the result back to a geography object based on the World Geodetic System 1984 (WGS 84) coordinate system.
Examples
Basic 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));
st_astext
--------------------------------
POLYGON((0 1,2 1,2 0,0 0,0 1))
(1 row)Aggregate intersection
The following example creates a table, inserts a small square and 100 buffered points around the origin, then uses the aggregate form of ST_Intersection to find the region common to all geometries. Because the small square is entirely contained within all the large circles, the result equals the square itself.
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;
select st_astext(st_intersection(geom)) from agg_result;
st_astext
----------------------------------------
POLYGON((0 0,0 0.5,0.5 0.5,0.5 0,0 0))
(1 row)