1

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 crosses 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?

ABDULLOKH MUKHAMMADJONOV
  • 4,249
  • 3
  • 22
  • 40

2 Answers2

2

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.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
1

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

Jim Jones
  • 18,404
  • 3
  • 35
  • 44