In PostGIS, there are two ways in which geometries can be intersected. The first way is checking whether two geometries intersect (at least one point of geometry A lies within the interior of geometry B). That's the ST_Intersects function. It accepts two arguments of the geometry type, and returns a Boolean.
For example, these lines intersect:
SELECT ST_Intersects(
ST_MakeLine(ST_MakePoint(20,50),ST_MakePoint(21,51)),
ST_MakeLine(ST_MakePoint(20.5,50.5), ST_MakePoint(22,52))
);
But these lines don't:
st_intersects
---------------
t
SELECT ST_Intersects(
ST_MakeLine(ST_MakePoint(20,50),ST_MakePoint(21,51)),
ST_MakeLine(ST_MakePoint(21.5,51.5), ST_MakePoint(22,52))
);
st_intersects
---------------
f
ST_Intersects can also be used for table joining. The following query will return land features that had at least one earthquake:
SELECT ne_110m_land.* FROM data_import.ne_110m_land JOIN data_import.earthquakes_subset_with_geom ON ST_Intersects(ne_110m_land.geom, earthquakes_subset_with_geom.geom);
The second way of intersecting features is by using the ST_Intersection function. Despite its similar name, it does a different job: it accepts two arguments of the geometry type, and returns a geometry containing the portion shared by those two features.
For example, let's compute an intersection of the British coastline with an arbitrary polygon:
SELECT gid,ST_Intersection(geom,
ST_SetSRID('POLYGON((-2 53, 2 53, 2 50, -2 50, -2 53))'::geometry,4326)
) FROM data_import.ne_coastline WHERE gid = 73;
