Assuming that I have a point on a map and a multipolygon on a map. When I query with ST_Intersects
or ST_Within
I get the same result. I want to know what is different between these two methods?
PostGIS is well documented. Doing some research gives you following results.
ST_Intersects: https://postgis.net/docs/ST_Intersects.html If a geometry or geography shares any portion of space then they intersect.
ST_Within: https://postgis.net/docs/ST_Within.html Returns TRUE if geometry A is completely inside geometry B
For points, there is no difference. When it is inside the polygon, it also shares a portion.
On the other hand, when you do the same with lines. A line that is not entirely within the polygon will not be selected with st_within.
-
4There is a difference for points. If a point is on the polygon edge (ex: a polygon vertex), the point and the polygons intersect each other but the point is not within the polygon – JGH Dec 16 '19 at 16:00
-
@PieterB thank you for response. But what about performance? When you have point and the result is same, which one has better performance? intersects or within? – Saber Solooki Dec 16 '19 at 17:23
-
@SaberSolooki Both ST_Within and ST_Intersects make use of bounding boxes and spatial indexes. As long as your geometries and spatial indices are setup correctly, there should be little performance difference. Choose the method that provides more accurate results based on JGH's comment. – Nate Wanner Aug 28 '20 at 15:59
-