New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intersection gives wrong result #1216
Comments
Thanks for the report @theroggy. Nothing to be done about this in Shapely, but it will be good to track it. |
OK. I logged an issue in the GEOS issue tracker as well: https://trac.osgeo.org/geos/ticket/1144 FYI: I was also able to reproduce it in the Java version (JTS) using JTS TestBuilder and it seems to have been beeen introduced in OverlayNG. |
Yup. this is a nasty little robustness problem alright. I will open an issue in JTS to track this (and hopefully a fix). |
@dr-jts I have some more examples, but I suppose chances are reasonable that it boils down to the same issue? If it is useful I can chase some more down, but it is always some searching to find the culprit triggering the problem. And the code to reproduce:
|
@theroggy thanks, always good to have more than one example to verify any potential fix. Indeed, the cause looks like the same thing - a very narrow triangle causing a robustness problem. It may be that this problem only occurs with narrow triangles (as opposed to say larger polygons with narrow spikes). If so, this is a much simpler case to check for, which is good. if you are looking for a workaround, you could detect the narrow triangles and take some evasive action (what that might be depends on your use case). |
@dr-jts final example for now: Code:
|
@dr-jts for a workaround: yes, the pattern seems to be quite specific, so indeed no issue to make one. But the current case I'm working on is only calculating statistics and the problem isn't frequent enough to give an impact on those. I only noticed it because I was looking into another issue: my (windows) PC crashes (reboots) when calculating the intersections between some (largish) files. When i noticed the invalid polygons created by this issue I was hopeful they were the trigger for the reboot, but they are not :-(. I found a workaround for it but if you hear something similar from someone else, don't hesitate ;-). Anyway, hopefully a fix will come though because for many other scripts it would be more of a problem if it would occur and it would be quite a pain to put a workaround everywhere, as it feels like the issue isn't quite a one-off... |
Thanks for the third test case. It shows the same pattern - a narrow triangle where the noding causes the apex vertex to cross the noded hypotenuse, thus inverting the triangle topology. Agreed, a fix is important, since this could occur at any time (in data of this pattern), and might cause more of a problem in other situations. |
This is fixed in JTS-812 by locationtech/jts#812. This will get ported to GEOS soon. |
I'm also seeing what I believe is incorrect intersection behaviour. Not sure whether it's the same underlying issue (or if I'm misunderstanding the intended behaviour. Running the code:
Gives the output:
The first of those intersections ( |
That's a different issue. The result is probably due to the snapping heuristics used to produce a robust intersection result. In this the geometry tolerances are so small (as you would expect with a buffer of 1e-6) that the snapping distorts the results on the same scale as the input. The overlay algorithm in JTS/GEOS is intended to produce reasonable results for well-conditioned input, and to provide a "best-effort" approximation in less tractable situations. It's functioning as designed in this case, so I call that a win. :) Is this a real-world situation? If so, is there any way to condition the input to be more suitable for processing? |
I see. Is there some documentation of the snapping behaviour? (Or the intersection behaviour more generally, the only thing I've found is "Returns a representation of the intersection of this object with the other geometric object." which has no reference to snapping or tolerances). I appreciate that tolerances are necessary, but I wouldn't expect them to be at the scale of 1. I also don't understand why a small tolerance would lead to a larger distortion from snapping? I'd naïvely expect the opposite. This seems like it's getting off-track with the issue though. What would be the best place to pose these questions? Open a new issue? For reference, the 1e-6 buffer was introduced because lines that were coincident were producing large numbers of intersections, presumably because of lack of tolerance. That was having a large impact on performance for a common usage. The 1e-6 buffer resolves that within acceptable tolerances most of the time, but this example demonstrates not always. |
The only documentation is the code. It's fairly readable. I agree that the computed result is anomalous. I suspect it's a result of the snapping tolerance being close to the resolution of the input polygon, and the arrangement of the inputs being such that this creates a distorted topology internally. Developing the current robust algorithm took months of work. Unfortunately I don't have the time to initiate a potentially similar-length investigation into new algorithms that might produce a better result for this case. I don't understand the problem with the use case. If you provide more details and an example it might be possible to suggest another work-around. I'll just note that introducing polygonal geometry into operations on lines usually just complicates the situation (since the requirements for topologically correctness are much greater for polygons). |
Thanks for your help. In our use case, we want to find the first point a line is intersected by another (first from the perspective of the first line). To do this we use The problem case above isn't necessarily representative, and was picked up by Hypothesis, but it's concerning to us that it could occur. There are other, arguably better, approaches from our side to resolve the performance issue without the use of a buffer, so we will explore those further when time allows. |
Fix was shipped in version 3.11 of geos on 2022-07-01: https://github.com/libgeos/geos/releases/tag/3.11.0 I retested and the test case now produces a correct result, so the problem is solved. Thanks! |
🎉 What is needed to benefit from this fix on a Shapely 1.8.x in an official |
Yes, for now you will need to ensure GEOS 3.11 is installed, and then install shapely from source (the binary wheel includes GEOS 3.10.3). I think we will probably keep using that GEOS version for Shapely 1.8.x, but Shapely 2.0alpha1 already uses GEOS 3.11 |
Expected behavior and actual behavior.
I ran into a case where an intersection between two (valid) (multi)polygons results in a wrong result. One example results in an "invalid" wrong result, another one results in a "valid" wrong result.
Probably the problem is in geos... but because my test case is using shapely I thought it was more logical to post it here.
Polygon 1 in the example below is the culprit:
Steps to reproduce the problem.
Operating system
Windows 10
Shapely version and provenance
shapely 1.8.0 from conda-forge
geos 3.10.0 from conda-forge
The text was updated successfully, but these errors were encountered: