lib: more robust signed polygon size#7366
Conversation
|
Since you have some test data here, would it make sense to create a unit test using that to ensure it is kept non-broken in the long-run? |
Done |
wenzeslaus
left a comment
There was a problem hiding this comment.
Can you please turn the comments into sentences with punctuation? It will be much easier to determine what is the intended meaning there. While we don't have that in style guidelines, I recently added that to AGENTS.md.
Also, linking the two places in a comment would mitigate the duplication.
got used to diglib code comment style, short and precise |
|
If anyone is interested in my thinking on the comment style:
Short and precise is something to keep, but since the primary audience of comments is humans and we typically read and otherwise communicate in full sentences, I would go with full sentences. Your comment here is an exception to the rule I would say. It is still perfectly readable because there is clear context. Code comments often lack some of the context we have in our conversations (speaker, chronological ordering of thoughts). |
wenzeslaus
left a comment
There was a problem hiding this comment.
The new comment in the test is strange, but otherwise we may indeed need a more specialized value comparison code. On the other hand, pytest or NumPy may already have something.
wenzeslaus
left a comment
There was a problem hiding this comment.
Please, put the xshift comment somewhere - code or commit message. Otherwise, it looks good to go.
|
The xshift comment is now in the code. BTW, a comment about your usage of Claude: even though I use coding agents myself sometimes, this PR was done by me without any AI assistance, not even PR review, the changes to the code are too minimal and too simple ;-) The amount of new code comment is now a multiple of the new code... Backport also to G84? |
|
Here is a raw re-review by Claude if you are interested (the suggestion it refers to is in #7366 (comment)): PR 7366 — Updated Review The author moved in the opposite direction from the comment-shortening suggestion: both code comments are now substantially longer, adding a paragraph explaining why the shift is applied to x as well as y. I'd push back on this — there are also two real bugs in the new content. Bugs introduced in this revision
Two errors:
Same typo in both area_poly2.c and poly.c. Worth fixing before merge — a wrong formula in a comment about numerical correctness is an unfortunate signal.
vector/v.to.db/testsuite/test_v_to_db.py: msg=f"Comparing mantissas of {d1[k1]} and {d1[k1]}", Both interpolations are d1[k1] — the second should be d2[k1]. As written, a failure prints the same number twice, which defeats the point of the message. The expanded comment Per project guidance (AGENTS.md): "Don't add features, refactor, or introduce abstractions beyond what the task requires. ... Don't design for hypothetical future requirements." The new paragraph — ▎ If for some reason the formula would be changed in the future by adding x and subtracting y, the current shift for both x and y would guarantee that the results stay correct (with reversed sign). — is exactly that: justifying present-day code by a hypothetical future edit. If someone later swaps which axis is summed, they'll re-derive what to shift on the spot; the comment doesn't help them and bloats the file today. The whole "considering the basic formula … in theory only needed for addition" digression also reads as design notes that belong in the PR description, not the source. My concrete suggestion remains close to last time: If the author feels strongly that the symmetric x-shift in poly.c deserves a note (since it cancels algebraically), one short line is enough, e.g.: That states the fact without inventing a future scenario. Other items unchanged from last review
Summary The numerical fix and tests are still correct and worthwhile. Before merge:
|
wenzeslaus
left a comment
There was a problem hiding this comment.
Thanks. Very effective and complete comment for xshift issue.
|
|
A last comment on code duplication: there is an important difference, |
|
Backport to G84 would not be possible with simple cherry-picking because |
|
Thanks for the additional comments here and in the code. Well-targeted, non-verbose comments make a world of difference for maintainability. |
There are two identical simple algorithms in the GRASS libs to calculate the signed size of a polygon in a projected CRS:
G_planimetric_polygon_area()in https://github.com/OSGeo/grass/blob/main/lib/gis/area_poly2.c#L25 anddig_find_area_poly()in https://github.com/OSGeo/grass/blob/main/lib/vector/diglib/poly.c#L97.In the current implementation, the sign of the calculated area becomes unstable for very small polygons. The sign of the area size is needed to determine the orientation of the polygon, which in turn is needed to decide if a polygon is a topological area or isle. This PR makes calculation of the signed area size more robust.
Example polygon with
x|ycoordinates:The calculated size for this polygon is about 5.5e-14 which is on sub-atomar level and in a geographical context not different from zero. However, such tiny polygons can occur with a combination of certain vector operations, e.g.
v.overlay. In these cases, it is important to get the correct sign of the area size, not matter how large it actually is, otherwise rather cryptic topological errors can occur later on.This fix also makes area size calculation for larger polygons more robust if (some of the) adjacent vertices are very close to each other, e.g. a circle approximated with a large number of points.
Backport also to G84?