You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "jiayuasu (via GitHub)" <gi...@apache.org> on 2026/08/10 07:12:24 UTC
[PR] fix(c/sedona-s2geography): Fix inverted geography polygons caused by rings with crossing edges [sedona-db]
jiayuasu opened a new pull request, #1143:
URL: https://github.com/apache/sedona-db/pull/1143
## Description
Fixes #1085.
One polygon in the Overture divisions dataset came out inverted when read as a geography: `ST_Area` returned a negative value and `ST_Within`/`ST_Intersects` matched points anywhere on Earth, silently corrupting every spatial join against the table.
### Root cause
Every geography UDF materializes WKB through `GeoArrowGeography::Init`, which calls `GeoArrowLaxPolygonShape::NormalizeOrientation()` to make shells counterclockwise and holes clockwise. That function derived each ring's winding direction from the sign of its curvature (turning angle). The affected ring is digitized with an out-and-back tail whose return path is offset by ~10 m; interpreted as geodesics, the return path crosses the outgoing path once. The crossing collapses the ring's turning number to zero, making the curvature sign meaningless, so the shell was reversed in place and its interior inverted to cover nearly the entire sphere. (This also explains why `ST_Reverse`/`ST_Normalize` had no effect: orientation is re-derived, and re-broken, on every evaluation.)
### Fix
paleolimbot/s2geography#125 changes `NormalizeOrientation()` to use the sign of the ring's signed area instead. The signed area agrees with the curvature sign for every valid ring (S2 computes it from the surface integral, with a fallback to the curvature sign when the magnitude is within numerical error), and additionally reflects the net winding direction for slightly invalid rings.
This PR bumps the s2geography submodule to that fix and adds a Python regression test with a minimal self-crossing sliver ring.
**Blocked on paleolimbot/s2geography#125** — the submodule currently points at the commit from the PR branch, which does not exist in the submodule's upstream remote yet. I will re-point the submodule to the merged commit once it lands, which should let submodule checkout and CI pass.
### Verification
Against the original 258-point ring from #1085:
| Expression | Before | After |
|---|---|---|
| `ST_Area(geog)` | −11 876 076 760.37 | +11 876 076 760.37 |
| `ST_Perimeter(geog)` | 574 613.63 | 574 613.63 (unchanged) |
| `ST_Within(POINT (-150 0), geog)` | true | false |
| `ST_Within(POINT (33.5 16.5), geog)` | — | true |
- s2geography C++ suite: 946/946 (new regression test fails without the fix, passes with it)
- `cargo test -p sedona-s2geography`: 68/68
- `pytest tests/geography/`: 653 passed, 1 pre-existing local-PostGIS environment failure unrelated to this change
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(c/sedona-s2geography): Make geography ring orientation less surprising for rings with crossing edges [sedona-db]
Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on PR #1143:
URL: https://github.com/apache/sedona-db/pull/1143#issuecomment-5269389170
Yes — .gitmodules still points at https://github.com/paleolimbot/s2geography.git
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(c/sedona-s2geography): Make geography ring orientation less surprising for rings with crossing edges [sedona-db]
Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu merged PR #1143:
URL: https://github.com/apache/sedona-db/pull/1143
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(c/sedona-s2geography): Make geography ring orientation less surprising for rings with crossing edges [sedona-db]
Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on PR #1143:
URL: https://github.com/apache/sedona-db/pull/1143#issuecomment-5259303520
Good call on both counts.
I added `st_area` benchmarks for `Polygon(500)` and `PolygonWithHole(500)` (the existing one only covered `Polygon(10)`) and measured three variants of the orientation check, all against the curvature baseline at the current submodule pin:
| benchmark | curvature (baseline) | signed area only | curvature + signed-area fallback |
|---|---|---|---|
| `st_area` Polygon(10) | 140.4 ms | +36.1% | +3.4% |
| `st_area` Polygon(500) | 6.95 s | +114% (CI +73% … +160%) | +4.4% |
| `st_area` PolygonWithHole(500) | 14.29 s | +55.1% | −0.8% (p = 0.66, no change) |
So it did not make it faster — the signed area is ~3× the cost of the curvature per ring, and using it unconditionally is a real regression. I've updated paleolimbot/s2geography#125 with a hybrid instead: trust the curvature when `|curvature| ≥ π` (a valid ring can only fall inside that band when it encloses between ¼ and ¾ of the sphere), and consult the signed area only within the band. Valid rings get identical decisions and identical cost to today's code; the self-crossing ring from #1085 lands in the band and stays corrected. With that revision the benchmarks are at noise level.
Also retitled the PR per your phrasing note — agreed this is about making the default behaviour less surprising for input whose behaviour is undefined, not fixing defined behaviour.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(c/sedona-s2geography): Fix inverted geography polygons caused by rings with crossing edges [sedona-db]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on PR #1143:
URL: https://github.com/apache/sedona-db/pull/1143#issuecomment-5254398058
Can you check `cargo bench -p s2geography` (possibly adding a new benchmark for Scalar st_area with a large number of vertices / polygon with a hole)? I am worried using area vs curvature will impact that result (although it may even make it faster). Because the affected code gets run for all geography input it will affect all functions (so we should at least measure what happens here).
I would also tweak the phrasing of the title here...this is not fixing anything (invalid input causes undefined behaviour...also true for geometry with crossing edges), but does make geography functions less surprising for the current default behaviour of `ST_ToGeography()`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org