Skip to content

Only treat an axis-aligned rectangle as a bounding box - #52

Merged
drewda merged 2 commits into
masterfrom
fix-is-rectangle
Oct 1, 2026
Merged

drewda merged 2 commits into
masterfrom
fix-is-rectangle

Conversation

@drewda

@drewda drewda commented Oct 1, 2026

Copy link
Copy Markdown
Member

Found by Copilot's review of #51. Feature.is_rectangle() counted distinct longitudes and latitudes, so a right triangle such as (0,0) (1,0) (0,1), which has only two of each, passed as a rectangle. osm_planet_extract --toolchain=osmium then cut the triangle's whole bounding box instead of the polygon. (#51 works around this for SliceOSM by sending every polygon as GeoJSON. This fixes the underlying check.)

New rule

  • Polygon: a rectangle only if its one ring has exactly the four corners of its bounding box (closed or not, either direction, any starting corner). Each step around the ring must move along one axis, which rules out a self-intersecting bowtie. It must have no holes.
  • MultiPolygon: always cut as a polygon. Osmium handles it, and the result is the same for a multipolygon that happens to be one rectangle.
  • Any other geometry: stands for its bounding box, as before. That includes the LineString that set_bbox() builds for --bbox and CSV rows, and a Point.

A side effect for other geometries: a LineString with three or more distinct coordinates used to fail the old count, which produced an osmium config key (linestring) that osmium doesn't accept. It now uses its bounding box.

Errors only go one way now. A rectangle drawn with an extra vertex on an edge is treated as a polygon, which is correct, just not the bbox shortcut. A non-rectangle can no longer be cut as a box.

Testing

  • New cases in tests/test_bbox.py:
    • rectangles: closed or unclosed, clockwise, other starting corners, mixed int and float
    • not rectangles: both right triangles, a bowtie, an extra vertex, a rotated square, a rectangle with a hole
    • a multipolygon
    • a Point and a 3-point LineString
  • The existing is_rectangle tests are unchanged and still pass.
  • Full suite: 263 passed, 2 skipped. ruff check is clean.
  • Real osmium run on examples/san-francisco-downtown.osm.pbf with a right-triangle extent: 19,929 nodes, against 30,165 before the fix, when the bounding box was cut.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hv2rprxPj2ScEMwrNt7U7Y

Feature.is_rectangle() counted distinct longitudes and latitudes, so a
right triangle such as (0,0) (1,0) (0,1) -- two of each -- passed as a
rectangle. osm_planet_extract --toolchain=osmium then cut its whole
bounding box instead of the polygon.

A Polygon is now a rectangle only if its single ring has exactly the four
corners of its bounding box, walked edge by edge (not a bowtie), with no
holes. A MultiPolygon is always cut as a polygon. Other geometries,
including the LineString that set_bbox() builds, still stand for their
bounding box; a LineString with three or more distinct coordinates used to
fall through to an osmium config key osmium doesn't accept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hv2rprxPj2ScEMwrNt7U7Y
Copilot AI balanced review requested due to automatic review settings October 1, 2026 23:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation matches the stated behavior and has focused coverage; only a minor docstring correction remains.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Fixes rectangle detection so osmium preserves non-rectangular polygon boundaries.

Changes:

  • Validates exact axis-aligned polygon corners and traversal.
  • Treats multipolygons as polygons and other geometries as bounding boxes.
  • Adds comprehensive geometry tests.
File Description
planetutils/​bbox.py Implements robust rectangle detection.
tests/​test_bbox.py Covers rectangle and non-rectangle geometries.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread planetutils/bbox.py Outdated
@drewda
drewda merged commit fdd768c into master Oct 1, 2026
27 checks passed
@drewda
drewda deleted the fix-is-rectangle branch October 1, 2026 23:58
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.

2 participants