Skip to content

Skip unnecessary longitude normalization on antimeridian-crossing box#1456

Open
tinko92 wants to merge 1 commit intoboostorg:developfrom
tinko92:fix/antimeridian-crossing-box-longitude-normalization
Open

Skip unnecessary longitude normalization on antimeridian-crossing box#1456
tinko92 wants to merge 1 commit intoboostorg:developfrom
tinko92:fix/antimeridian-crossing-box-longitude-normalization

Conversation

@tinko92
Copy link
Copy Markdown
Collaborator

@tinko92 tinko92 commented Apr 7, 2026

The program in issue #1452 triggers some cases in which a box shrinks when being expanded with a point that is already within it, due to rounding errors introduced unnecessarily in coordinate normalization. When a box crosses the antimeridian, the longitude of its max point is first normalized to be within [-180, 180], then increased by half_period() again, which can introduce rounding error, including shrinking the box. the shrinking then seems to produce unstable behaviour in the rtree when the computed area difference between a boxes expansion and itself is negative. This is only a small part of the tree instability in #1452 and does not resolve most of the issue but isolated enough from the other issues to be treated in a separated PR.

This PR contains the following changes:

  1. Adds a flag that detects when a box crosses the antimeridian and has otherwise valid max longitude, and passes this flag down to coordinate normalization, so that this longitude is then not normalized down.
  2. Add a test case with tolerance=0.0, in which the box would shrink without this change.
  3. Update the box_equal code to respect the passed tolerance in longitude comparison rather than using the (more lenient) bg::math::equals unconditionally.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Addresses a rounding-error edge case where expanding a spheroidal box that crosses the antimeridian can unexpectedly shrink due to unnecessary longitude normalization.

Changes:

  • Adds an “allow antimeridian crossing” path to longitude normalization to avoid lossy normalize-then-shift behavior.
  • Updates spheroidal box normalization to pass the new flag when appropriate.
  • Adds a regression test for the shrink-on-expand scenario and tightens box equality checks to respect the provided tolerance.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
test/algorithms/envelope_expand/test_envelope_expand_on_spheroid.hpp Adjusts box equality logic for longitude comparisons under tolerance.
test/algorithms/envelope_expand/expand_on_spheroid.cpp Adds a regression test for antimeridian-crossing box expansion stability.
include/boost/geometry/util/normalize_spheroidal_coordinates.hpp Extends longitude normalization with an option to skip normalization for antimeridian-crossing representation.
include/boost/geometry/util/normalize_spheroidal_box_coordinates.hpp Detects the special antimeridian-crossing representation and forwards the skip-normalization flag.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread include/boost/geometry/util/normalize_spheroidal_coordinates.hpp
Comment thread include/boost/geometry/util/normalize_spheroidal_box_coordinates.hpp Outdated
Comment thread test/algorithms/envelope_expand/expand_on_spheroid.cpp Outdated
@tinko92 tinko92 force-pushed the fix/antimeridian-crossing-box-longitude-normalization branch 2 times, most recently from ec1dc3e to 3f36811 Compare April 7, 2026 15:41
Comment thread include/boost/geometry/util/normalize_spheroidal_box_coordinates.hpp Outdated
Comment thread include/boost/geometry/util/normalize_spheroidal_box_coordinates.hpp Outdated
@tinko92 tinko92 force-pushed the fix/antimeridian-crossing-box-longitude-normalization branch from 3f36811 to 7e9a531 Compare April 11, 2026 06:12
@tinko92 tinko92 changed the title Skip unnecessary longitude normalization on antimridian-crossing box Skip unnecessary longitude normalization on antimeridian-crossing box Apr 12, 2026
Copy link
Copy Markdown
Collaborator

@barendgehrels barendgehrels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! I'm OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants