JamesLMilner / terra-draw

A library for drawing on maps that supports Mapbox, MapLibre, Google Maps, OpenLayers and Leaflet out the box
https://terradraw.io
MIT License
447 stars 50 forks source link

[Potential Bug] Unexpected result of polygon editing #255

Closed risusinf closed 3 weeks ago

risusinf commented 3 months ago

Not sure if that's a bug, but i'll report it.

I'm using turf.difference to track edits i make to polygons, and noticed unusual geometries that i receive under certain circumstances. Here is a picture, in question is bottom right red polygon outline.

Screenshot_20240527_233405

Here is the same polygons over initial geometries.

Screenshot_20240527_233457

The edit was that i added more vertices by clicking midpoints (red circled) and then pulled central vertice up.

Screenshot_20240527_233550

I expected result to look like other polygons in the first picture, but it seems like adding more vertices slightly changed geometry of the bottom part of the polygon.

JamesLMilner commented 3 months ago

Hey @risusinf, apologies about the delayed response. I always appreciate people raising what they think might be bugs with Terra Draw, so that you for that. I've read through your issue, but in honesty am struggling to fully understand the issue you are describing. Would you kindly be able to simplify this down to a single geometry/image so I can follow it a little more easily?

The other thing to say, without obviously being able to say with strict confidence, is that this could also be a bug with @turf/difference.

Look forward to hearing from you.

risusinf commented 3 months ago

Case 1.

Left to right:

This is the result i expected, clean triangles representing changes i made.

1

============ Case 2.

There is now a ghost line in place where the edited sides were.

2

============ Case 3.

Doing the same thing as before now on every side of the initial polygon, moving central vertices inward or outwards. Outcome is inconsistent. Sometimes it's just a triangle, sometimes a triangle with a "ghost line".

3

I'm just trying to understand what are those artifacts and where are they come from. If you say the culprit is turf.difference i believe you, but i'm not feeling like bringing it over there honestly.

PS. It is probably turf, because when i run pane 1 and 2 from case 1 through it -- the result (it is written to database) is not empty, but the map doesn't show anything. Going to close this after your follow up.

JamesLMilner commented 3 weeks ago

Hey @risusinf sorry about taking so long to respond here - appreciate your detailed reply. I saw that you concluded this is probably turf, which look at your screenshots leads me to think the same. It may be some spurious floating point precision issue where it doesn't consider some of those coordinates to be identical?

I'm going to close this just in the interests of keeping on top of the issues board, but feel free to write more comments or open a new issue if you think you have identified a reproducible issue. Thanks again for your interest in the project!

risusinf commented 3 weeks ago

@JamesLMilner I haven't worked on my project throughout the summer, so nothing new on this one. Not sure how exactly turf works, but my last guess at the time was that adding new vertices to a polygon makes it 'different' from the original for turf, despite area coverage may remain the same, so when it tries to calculate difference the output is a pseudo polygon made of new vertices. Never got to check if that's the case, and if so i don't think it's fixable.