# [postgis-users] Difference between ST_Intersects and && operator

Nicklas Avén nicklas.aven at jordogskog.no
Thu Sep 23 03:40:19 PDT 2010

```As said before the && operator only calculates which bounding boxes that
intersects. The bounding box is the smallest box that can contain the
geometry. It is a box made of the maximum and minimum x any y values
found in the geometry.

So, when the bounding boxes intersect it says nothing about if the
geometries inside the box intersects. But it is a fast way to sort away
geometries that NOT intersect since intersections between geometries is
impossible if the bounding boxes doesn't intersect.

The bounding boxes is also possible to structure in an index. That makes
it really fast to find intersection between bounding boxes if you have a
working index on the geometries.

This is what ST_Intersects (and a lot of other functions) does. First it
finds intersecting bounding boxes with help of the index if present
(this is done with the && operator inside ST_Intersects function). Then
it checks the real geometries inside the boxes if they really
intersects.

HTH

Nicklas

On Thu, 2010-09-23 at 09:08 +0200, Andreas Forø Tollefsen wrote:
> Ok. I see. But why is the results so different. When using
> ST_Intersects the result is correct, while using && it seems there is
> no order in which vector grid cells it selects. It is not the cells
> intersecting with the country polygon, but rather a mesh of random
> cells also outside of the country polygons.
>
> 2010/9/22 Paragon Corporation <lr at pcorp.us>
>         && does a bounding box intersects check where as ST_Intersects
>         does a more intensive actual geometry intersect check.  Which
>         is why its slower.
>
>
>
>
>         ______________________________________________________________
>         From: postgis-users-bounces at postgis.refractions.net
>         [mailto:postgis-users-bounces at postgis.refractions.net] On
>         Behalf Of Andreas Forø Tollefsen
>         Sent: Wednesday, September 22, 2010 11:00 AM
>         To: PostGIS Users Discussion
>         Subject: [postgis-users] Difference between ST_Intersects and
>         && operator
>
>
>
>
>         Hi all.
>         I am working on a huge vector grid project where i need to
>         intersect country polygon data and vector grid cells.
>         For this i need to first select the grid cells which
>         intersects with the country polygons.
>
>
>         SELECT DISTINCT testgrid.gid, xcoord, ycoord, cell INTO
>         testgrid2 FROM testgrid, cshaperef WHERE
>         ST_Intersects(cshaperef.the_geom, testgrid.cell)=true
>
>
>         I tried both the ST_Intersects and the && operator, but i
>         cannot really understand the difference.
>         After looking at my results the ST_Intersects gave exactly the
>         cells which intersected with the country polygons, while the
>         && gave many additional cells.
>         Another interesting observation is the the && is way faster
>         than the ST_Intersects which in my case 188 polygons and
>         212000 cells takes a lot of time.
>
>
>         Could anyone please elaborate on the difference?
>
>
>
>         _______________________________________________
>         postgis-users mailing list
>         postgis-users at postgis.refractions.net
>         http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users

```