The ST_Intersection() function

The ST_Intersection() function takes two ST_Geometry objects and returns the intersection set as an ST_Geometry object. If the two objects do not intersect, the return value is an empty geometry.

Syntax

ST_Intersection(g1 ST_Geometry, g2 ST_Geometry)

Usage

If an ST_LineString intersects an ST_Polygon, the ST_Intersection() function returns the portion of the ST_LineString common to the interior and boundary of the ST_Polygon as an ST_MultiLineString. The ST_MultiLineString contains more than one ST_LineString if the source ST_LineString intersects the ST_Polygon with two or more discontinuous segments.

The following figure illustrates examples of the ST_Intersection() function.
Figure 1: Intersection sets of geometries

This graphic shows the intersection of various geometric objects.

Return type

ST_Geometry

Example

The fire marshal must obtain the areas of the hospitals, schools, and nursing homes that are intersected by the radius of a possible hazardous waste contamination.

The sensitive areas are stored in the sensitive_areas table that is created with the CREATE TABLE statement that follows. The zone column, which is defined as an ST_Polygon type, stores the outline of each of the sensitive areas:
CREATE TABLE sensitive_areas (id     integer,
                              name   varchar(128),
                              size   float,
                              type   varchar(10),
                              zone   ST_Polygon);
The hazardous sites are stored in the hazardous_sites table that is created with the CREATE TABLE statement that follows. The location column, which is defined as an ST_Point type, stores a location that is the geographic center of each hazardous site:
CREATE TABLE hazardous_sites (site_id   integer,
                              name      varchar(40),
                              location  ST_Point);
The ST_Buffer() function generates a 5-mile buffer that surrounds the hazardous waste site locations. The ST_Intersection() function generates polygons from the intersection of the buffered hazardous waste sites and the sensitive areas. The ST_Area() function returns the intersection polygons' area, which is summarized for all hazardous sites by the SUM operator. The GROUP BY clause directs the query to aggregate the intersection areas by hazardous waste site ID:
SELECT hs.site_id, SUM(ST_Area(ST_Intersection(sa.zone,
       ST_Buffer(hs.location,(5 * 5280)))::ST_MultiPolygon))
   FROM sensitive_areas sa, hazardous_sites hs
   GROUP BY hs.site_id;

    site_id          (sum) 

        102 87000000.00000
         59 77158581.63280
In the following figure, the circles represent the 5-mile buffer polygons that surround the hazardous waste sites. The intersection of these buffer polygons with the sensitive area polygons produces three polygons: the hospital in the upper left corner is intersected twice, while the school in the lower right corner is intersected only once.
Figure 2: Using theST_Intersection() function

This graphic is described in the surrounding text.