Intro
Short version: My goal is to find geometries intersected by a target polygon.
Long version: We are building an interface for the user to upload new geometries (political maps) and allow him to solve 4D overlaps in a semi-automatic way. Steps: Find all intersecting polygons; Based on user review, modify polygons by using ST_Difference function.
To find overlaps - the most straightforward choice is to use ST_Overlaps or ST_Intersects. But this method will give us an unlimited amount of false-positive results. Probably because of the tolerance, borders located next to each other would be treated as an intersection.
SELECT *, ST_Intersects(
( SELECT territory FROM diff_test WHERE id = 1 ),
( territory )
) as intersects
FROM diff_test
WHERE diff_test.id != 1
The other method I can think of is to select every polygon limited by bounding box. But then there is a need to filter out all the "touching" borders, and ST_Touching is not helpful if you don't have common points. It will also not work.
Back to ST_Intersects:
Let's say we have two overlapped polygons - id 1 (1430)
and id 2 (1487)
.
We've calculated two new polygons.
id 3 (1430 - 1487)
ST_Difference(id1, id2)
id 4 (1487 - 1430)
ST_Difference(id2, id1)
# this one can be ignored
What did I expect:
id 3
is insideid 1
(ST_Overlaps = false)id 3
is touchingid 2
(ST_Touches = true)id 3
does not intersect withid 2
(ST_Intersects = false)
Nothing from this was as I've expected.
The query was executed targeting id 3
Problem and Questions
- What is the best solution to find truly intersect polygons?
- What would be the best practice of avoiding intersections from newly created polygon by ST_Difference and his counterpart?
Thoughts
- I can think of multiple ST_Difference operations, but it will not guarantee anything and it is slow. Next query still returns true instead of false
SELECT ST_Overlaps(t1mask, origt2) FROM ( SELECT
(SELECT territory FROM diff_test WHERE id = 2) as origt2
,(SELECT ST_Difference(mask, intersects) FROM (
SELECT
( SELECT territory FROM diff_test WHERE id = 3) as mask
,(
SELECT ST_Intersection(
( SELECT territory FROM diff_test WHERE id = 3 )
,( SELECT territory FROM diff_test WHERE id = 2 ))
) as intersects
) as t ) as t1mask
) as t
As another option - cut a little bit more, to prevent this from happening.
Or maybe this problem can be reproduced only on my data and can be solved by ST_SnapToGrid or something similar
diff_test
table as is. I can publish more polygons tomorrow. ST_SnapToGrid looks like a good idea. I am thinking of processing the whole dataset with it even before ST_Difference