skip to Main Content

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:

enter image description here

I expected ST_Intersects to return false when two polygons share sides, but it does not:

enter image description here

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


  1. You want ST_Overlaps:

    Returns TRUE if geometry A and B "spatially overlap". Two geometries overlap if they have the same dimension, each has at least one point not shared by the other (or equivalently neither covers the other), and the intersection of their interiors has the same dimension. The overlaps relationship is symmetrical.

    Login or Signup to reply.
  2. 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 can ST_Area this generated polygon and compare it with one of the polygons given to the function, e.g. given these polygons ..

    enter image description here

    … this would be the result of ST_Intersection

    enter image description here

    And this is how you could calculate the percentage of the overlap

    SELECT 
      ST_Area(ST_Intersection(g1,g2))/ST_Area(g1)*100 AS perc_g1_overlap,
      ST_Intersection(g1,g2)
    FROM t;
      perc_g1_overlap   |                                                                                          st_intersection                                                                                           
    --------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     16.555309365086124 | 0103000020E6100000010000000500000054435ACF861E12C02068AC4B11214B4054435ACFD60A12C02068AC4B11214B4054435ACFD60A12C04B266CCD791F4B4054435ACF861E12C04B266CCD791F4B4054435ACF861E12C02068AC4B11214B40
    (1 row)
    

    Demo: db<>fiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search