Fix intersection computation of slightly overlapping polygons - #1499
Conversation
…ntersection) Signed-off-by: Petr Belohlavek <me@petrbel.cz>
…rDirection Signed-off-by: Petr Belohlavek <me@petrbel.cz>
|
@dr-jts you are right. I got confused by the y-axis. However, the behavior is still flawed, isn't it? The intersection area is most definitely not 0.25. What do you think? edit: I updated the initial description so that it's more clear |
Looks like a bug in GEOS. JTS gives the expected intersection result (with area = 2.7755575615628914E-1 ):
|
Yes and no, I think. When I pull this up in the debugger, JTS and GEOS return the same result for all invocations of I think the fix here is OK, though I would be more comfortable if we also asserted that |
…iginate in the same point Signed-off-by: Petr Belohlavek <me@petrbel.cz>
|
@dbaston I think your exaplanation is correct. My explanation is that if we use the coordinate-wise float comparison and the difference is less than what floats can handle, the points are considered the same. Then, the quadrands tie-breaker never runs. If that makes sense.
Good idea. I added the asserts Please let me know if you have any further remarks |
|
Agreed, it's always better to use more robust logic that avoids arithmetic operations. |
|
Mpre notes:
|


Problem: The polygon intersection computation doesn't work well when two polygons
almost share an edgeoverlap very slightly. One of the symptoms is that intersection operation isn't commutative (as described in #1405)Content: This PR consists of two commits: one adding a trivial not-working example (a unit test), two a proposed fix using
equals2Dinstead of trivial independent coord-by-coord comparison. What i propose is to actually compare the dst point instead of float comparison of subtraction. That, should the points be very close, can produce 0 even for non-equivalent edges due to float arithmetics.Example description: Below I enclose the problem description. It's similar to #1405 but, I believe, more straightforward and debuggable.
POLYGON ((1 1, 0 0.5, 0 0, 1 1))- a triangle with area 0.25POLYGON ((1 1, 0 0.49999999999999994, 0 1, 1 1))- another triangle with area (almost) 0.25 which shares a common vertex1 1and a tiny overlap close to one of the edges with Aand almost shares a second vertex0 0.49999999999999994with A (0 0.5)Apart from point(1 1)these polygons don't share any point. Their intersection is a single point and the intersection area is mathematically0. Their union area is (almost)0.5(1 1) -> (0 0.5)with area close to00.25as well as the union area. The newly proposed unit tests demonstrate this fact.Example visualization: I used dbeaver with postgis only for the visualization purposes. I know this postgis isn't up-to-date and linked geos is only
3.13.1, however, the same behavior is replicated directly in the tests.postgis_full_version()The source sql in case anybody would like to replicate:
Disclaimer: while I found out about the problem myself and basically crafted the examples, I admit using a LLM for some hints and coding assistence. In case it's not allowed feel free to close the PR, however, please consider the revealed/described bug. I believe the code sheds some light to it.
Closes #1405