ST_Intersects and &&

Two different methods of selecting features that intersect another feature

There are two different methods for selecting features (geometries) that intersect a different feature.

1. && function

This function uses the bounding boxes of features to test whether they intersect. This may therefore return more features than actually intersect because of the overlapping bounding boxes.

Look at the following example image.

520

If we used the && function to select the lines that intersect with the star we would return both the red and blue lines because both of their bounding boxes intersect.

This is extremely fast method of selecting features that intersect.

Example SQL statement:

SELECT * FROM table_1, table_2 WHERE table_1.geometry && table_2.geometry;

2. ST_Intersects function

This uses the PostGIS function ST_Intersects to accurately select any features that intersect the other feature. This is a two stage function, the first uses a bounding box select to quickly select any features that intersect using their corresponding bounding boxes. The function will then narrow down the list of features to only those that actually intersect using the geometries.

Looking at same image again.

If we were to use ST_Intersects to determine which lines intersect with the star then only the red line would be true, as it is the only feature that has geometry that does in fact intersect the star geometry.

520

Example SQL statement

SELECT * FROM table_1, table_2 WHERE ST_Intersects(table_1.geometry,table_2.geometry);