ST_Intersection

ST_Intersection takes two geometries as input parameters and returns the geometry that is the intersection of the two given 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, it will be converted to the other spatial reference system.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_Intersection(geometry1,geometry2 )

Parameter

geometry1
A value of one of the seven distinct spatial data types that represents the first geometry to compute the intersection with geometry2.
geometry2
A value of one of the seven distinct spatial data types that represents the second geometry to compute the intersection with geometry1.

Return type

db2gse.ST_Geometry

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

Example

In the following example, 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.

SET CURRENT FUNCTION PATH = CURRENT FUNCTION PATH, db2gse
CREATE TABLE sample_geoms (id INTEGER, geometry ST_Geometry)

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

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

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

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

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

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

Results:

ID          ID          INTERSECTION
----------- ----------- ------------------------------------------------
          1           1 POLYGON (( 30.00000000 30.00000000, 50.00000000 
30.00000000, 50.00000000 50.00000000, 30.00000000 50.00000000, 30.00000000 
30.00000000))

          1           2 LINESTRING ( 30.00000000 40.00000000, 30.00000000 
30.00000000)

          1           3 POLYGON (( 40.00000000 40.00000000, 50.00000000 
40.00000000, 50.00000000 50.00000000, 40.00000000 50.00000000, 40.00000000 
40.00000000))

          1           4 POINT EMPTY

          1           5 LINESTRING ( 30.00000000 30.00000000, 50.00000000 
50.00000000)

  5 record(s) selected.