Finding truly intersected polygons
Uf you want to find overlaps of two polygons, then your best fried is st_intersection in conjunction with st_intersects (it's not the same function). You should firstly join polygons using st_intersects. Then get intersected geometries with st_intersection. This will produce different geometry collections based on type of interaction. Points and Lines in the case of touch and polygons in the case of true overlap. So you can then pretty simple pick what you want. BTW PostGIS performs lanient geometry marching, thus there shouldn't be requirements of common points to recognize spatial relation of two geometries...
If you want implement some sort of auto-complete polygon function, then I would do this:
- Collect polygons from user
- Find intersection of all existing polygons as described above
- Union all intersected geometris to one geometry
- Find difference between unioned and original geometry using st_difference.
I thing this should produce want you want...
To find polygons which "truly" intersect (ie. have some shared area in common) then ST_Relate
must be used with an Intersection Matrix pattern of T********
. There is no standard named predicate which provides this semantics. (It would be nice if there was a named predicate called something like "interiorIntersects".) The PostGIS doc has an example.
Another option is to compute the actual intersection geometry (using ST_Intersection
) and check whether it has dimension = 2. However, this will be slower than using the predicate. Also, the intersection computation is subject to numerical round-off, which means that in certain cases the intersection may not contain an area even if the overlaps predicate returns true.
Note also that if the input data is not a true polygonal coverage, it may be that "adjacent" polygons actually have a non-zero intersection area (and hence ST_Relate(a, b, 'T********') = true
). In this case it is necessary to compute the intersection area and then filter out cases with a small tolerance value on the intersection area.