ST_Intersection function

The ST_Intersection function takes two geometries as input parameters and returns the geometry that is the intersection of the two specified geometries. The intersection is the common part of the first geometry and the second geometry. The resulting geometry is represented in the spatial reference system of the first geometry.

If possible, the specific type of the returned geometry will be ST_Point, ST_LineString, or ST_Polygon. For example, the intersection of a point and a polygon is either empty or a single point, represented as ST_MultiPoint.

If any of the two geometries is null, null is returned.

If the second geometry is not represented in the same spatial reference system as the first geometry and uses the same underlying datum, it will be converted to the other spatial reference system.

Syntax

Read syntax diagramSkip visual syntax diagramST_Intersection(geometry1,geometry2 )

Parameter

geometry1
A value of type ST_Geometry or one of its subtypes that represents the first geometry to compute the intersection with geometry2.
geometry2
A value of type ST_Geometry or one of its subtypes that represents the second geometry to compute the intersection with geometry1.

Return type

ST_Geometry

The dimension of the returned geometry is that of the input with the lower dimension.

Example

In the following examples, the results have been reformatted for readability. The spacing in your results will vary according to your display.

This example creates several different geometries and then determines the intersection (if any) with the first one.

CREATE TABLE sample_geoms (id INTEGER, geometry ST_Geometry)

INSERT INTO sample_geoms VALUES
       (1, ST_Geometry('polygon((30 30, 30 50, 50 50, 50 30, 30 30))' ,0))

INSERT INTO sample_geoms VALUES
       (2, ST_Geometry('polygon((20 30, 30 30, 30 40, 20 40, 20 30))' ,0))

INSERT INTO sample_geoms VALUES
       (3, ST_Geometry('polygon((40 40, 40 60, 60 60, 60 40, 40 40))' ,0))

INSERT INTO sample_geoms VALUES
       (4, ST_Geometry('linestring(60 60, 70 70)' ,0))

INSERT INTO sample_geoms VALUES
       (5, ST_Geometry('linestring(30 30, 60 60)' ,0))

SELECT a.id, b.id, CAST(ST_AsText(ST_Intersection(a.geometry, b.geometry)) 
		 as VARCHAR(60)) Intersection
FROM sample_geoms a, sample_geoms b
WHERE a.id = 1

Results:


ID1         ID2         INTERSECTION
----------- ----------- ------------------------------------------------
          1           1 POLYGON ((30 30, 50 30, 50 50, 30 50, 30 30))                                                                                               
          1           2 LINESTRING (30 40, 30 30)                                                                                                                   
          1           3 POLYGON ((40 40, 50 40, 50 50, 40 50, 40 40))                                                                                               
          1           4 POINT EMPTY                                                                                                                                 
          1           5 LINESTRING (30 30, 50 50)