I am using ST_Intersects to check if two polygons intersect. Relevant part of my query is:
SELECT entity_number
FROM coordinates
WHERE ST_INTERSECTS($1, location)
It works well to determine if one polygon scrosses the other’s surface:
I expected ST_Intersects to return false when two polygons share sides, but it does not:
I read about other methods like ST_Covers, ST_Contains, ST_ContainsProperly, ST_Within ,ST_DWithin
. But i am not sure which one suits my needs.
Is there any method that allows two polygons to share sides?
2
Answers
You want
ST_Overlaps
:As pointed out by Laurenz,
ST_Overlaps
is what you’re looking for. However, quite often it does not suffice to simply check if polygons do overlap, but also "how much" they overlap. In other words, how much of geometry a overlaps with geometry b?ST_Intersection
returns a polygon that is the result of the intersection of two geometries, so you canST_Area
this generated polygon and compare it with one of the polygons given to the function, e.g. given these polygons ..… this would be the result of
ST_Intersection
And this is how you could calculate the percentage of the overlap
Demo:
db<>fiddle