Skip to content

feat(labels): try more places inside a polygon whose name fits - #3503

Merged
jgregoriorincon merged 4 commits into
opengeos:mainfrom
jgregoriorincon:feat/3500-polygon-labels-inside
Dec 1, 2026
Merged

jgregoriorincon merged 4 commits into
opengeos:mainfrom
jgregoriorincon:feat/3500-polygon-labels-inside

Conversation

@jgregoriorincon

@jgregoriorincon jgregoriorincon commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With Try other places inside and around the polygon on, a polygon's label whose name fits the box of its polygon on the screen was left out once both places inside (where its shape fits it best, then where it goes without the setting) were taken. It now tries up to 16 more places inside: a text box (plus the class's clearance) away from the first place in eight directions, in the order a point's label tries them (corners first), then two boxes away.

  • A place is tried only where the whole name is inside the polygon, holes kept out, and in view, upright in the view shown. The test is the circle round the box once the outline is narrowed to the text's shape, as the first place is searched, so a box that only just fits can be refused. As the first place, it is tested against the outline as thinned for the search: past about 20,000 points near the view, a thinned edge can pass over a narrow notch.
  • A name wider or taller than the box goes round it, as before ([Feature]: Polygon labels try places around the polygon after the ones inside (label placement step 4b) #3484). The two kinds of later places never apply to the same label.
  • With the label point out of view, the places are counted in the part of the polygon in view, as its first place is ([Bug]: A polygon in view gets no label when its label point is out of view #3276).
  • As with points and lines, a name that now finds a place can take the room a less important label had.
  • Lock keeps the place it took by its map anchor, as the other later places.
  • Unchanged: labels moved by hand, curved labels, labels along a guide, and labels turned by an angle expression.

Surfaces: placement is in rustgis-render, so the CPU and GPU maps, turned and tilted views, the globe, export, the browser and phone builds, and scripts, CLI, Python and plugins that set otherPositions all get it. The labels.placement parameter help, the checkbox's tooltip and the user guide say so. No new setting, command, parameter or project format change; undo is unchanged. The new interface string waits for the release translation pass.

Cost: no label thins its rings more than before. A label that fits its box now measures up to 16 distances to those thinned rings, whether or not its first place is free, since the places are listed before placement. The distances are measured on a copy of that one label's thinned rings (at most about 20,000 points), freed with the label: no multiple of the layer. Its candidate holds those places, counted by Candidate::bytes in the label cache's budget, so a very large polygon layer with the setting on reaches that budget sooner.

Not in this PR: fitting a name that doesn't fit inside.

Testing

  • New tests in crates/render/src/polygon_labels_tests.rs, written first and seen failing: order and spacing (north up, turned, tilted), same result twice, setting off, angle expression, a polygon with no room for another place, holes and multipolygons, places off the view, label point out of view, globe, and Lock. One more, added in review, for the class's position and offset.
  • One test that pinned "two places and no more" for a name that fits now checks that none of its places leaves the polygon's box.
  • cargo nextest run -p rustgis-render -E 'test(label)': 219 passed.
  • fmt, clippy, doctests, layers, schema --check: green. Workspace tests: 11708 passed, 8 failed, all in rustgis-geolibre and rustgis-ui-egui::smoke line_strokes_editor_edits_one_class, which fail on this machine without this change (run before the last commits, which add that test and change comments only).

UI evidence

World countries labelled with the setting on, under heavier city labels (before / after). Poland, Lithuania and Serbia now show:

Europe before and after

Tanzania now shows, and takes the room Malawi's label had:

Africa before and after

Related issue

Closes #3500. Part of #995.

Summary by CodeRabbit

  • Improvements
    • Polygon labels can now try up to 16 additional positions inside a polygon when the label fits, before trying positions around it or omitting it.
    • When the preferred inside position is out of view, labels can use visible portions of the polygon. Labels with an angle expression do not use these additional placement attempts.
  • Documentation
    • Updated the placement guidance and tooltip to explain the additional label positions.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Polygon labels now try additional inside positions when their preferred inside positions are occupied. The renderer checks that fitting labels remain inside the polygon and view. Oversized labels continue to use positions around the polygon. Tests and placement descriptions cover these rules.

Changes: Polygon Label Placement

Layer / File(s) Summary
Polygon geometry checks
crates/render/src/area_labels.rs, crates/tiles/src/symbols.rs
Ring normalization is factored into a helper, and a new predicate checks whether a centered box fits inside polygon rings. signed_distance is now public.
Candidate search and placement
crates/render/src/lib.rs, crates/render/src/polygon_labels_tests.rs, crates/engine/src/cmd/labels.rs, crates/ui-egui/src/labelpane.rs, docs/user-guide/cartography.md
For fitting labels, the renderer adds inside candidates and checks their visibility and containment. Oversized labels use candidates around the polygon. Tests and descriptions cover candidate ordering, view bounds, holes, multipart polygons, angle expressions, and locks.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Possibly related issues

  • Issue 3268 defines the best-fit inside position and label-point fallback that precede the additional candidates.
  • Issue 3484 defines around-polygon placements in the same candidate sequence.
  • Issue 995 describes the broader polygon label-placement plan, which includes trying inside positions before outside positions.
  • Issue 3276 reports missing labels when the label point is out of view; this change searches the visible polygon portion in that case.

Possibly related PRs

  • opengeos/RustGIS#3492: Adds around-polygon candidates in the same ordered placement sequence.
  • opengeos/RustGIS#3275: Adds the preferred inside position and label-point fallback used before the new candidates.

Suggested reviewers: elpidioassinaturas, giswqs

Merge Risk: Low · up to 03d57

The new inside label positions work as described, but on rotated maps with a nonzero label offset they may be offset differently from the first position. The effect is limited to label placement and can be fixed or accepted as a follow-up.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat(labels): try more places inside a polygon whose name fits" matches the main change. The PR adds up to 16 more inside-polygon label positions for polygon labels whose name fits, when "T…
Description check Passed The description follows the repository template. It has Summary, Testing, UI evidence, and Related issue sections. The Summary explains the change, the reasons, the affected surfaces, and the cost. Te…
Linked Issues check Passed [#3500] The requirement is an extra inside-polygon placement step for names that fit. The reviewed head adds this step in crates/render/src/lib.rs (+83/-41). It runs after the best-fit place and the…
Out of Scope Changes check Passed Every changed file connects to the placement feature in [#3500]. crates/render/src/lib.rs and area_labels.rs hold the placement logic and the box-fit helper. `crates/render/src/polygon_labels_test…
Full details: Title check

Explanation

The title "feat(labels): try more places inside a polygon whose name fits" matches the main change. The PR adds up to 16 more inside-polygon label positions for polygon labels whose name fits, when "Try other places inside and around the polygon" is enabled. The title is concise, specific, and follows a conventional commit style.

Full details: Description check

Explanation

The description follows the repository template. It has Summary, Testing, UI evidence, and Related issue sections. The Summary explains the change, the reasons, the affected surfaces, and the cost. Testing lists new tests and the commands run, with failing workspace tests explained. UI evidence has before and after screenshots of Europe and Africa, which fits the rule that labels are visible UI. The template's placeholder line for no visible UI change was replaced with screenshots, which is correct because the change alters label placement. Related issue cites Closes #3500 and Part of #995.

Full details: Linked Issues check

Explanation

[#3500] The requirement is an extra inside-polygon placement step for names that fit. The reviewed head adds this step in crates/render/src/lib.rs (+83/-41). It runs after the best-fit place and the label-point place are taken. It tries up to 16 candidates: one text box plus class clearance away in eight directions, corners first, then two boxes away. Each candidate must keep the whole name inside the polygon, outside holes, in view, and upright in the displayed view. The same file keeps Lock on the saved map anchor and slot. Names that do not fit still go around the polygon, as in [#3484]. The new candidates do not apply to those labels. crates/render/src/area_labels.rs adds holds, the box-fit predicate used by the candidate test. crates/render/src/polygon_labels_tests.rs adds tests for order and spacing (north-up, turned, tilted), repeat determinism, setting off, angle expressions, no-room cases, holes and multipolygons, out-of-view candidates, label point out of view [#3276], globe, and Lock. Hand-moved, curved, guide-following, and expression-angled labels are excluded as the issue requires. The PR adds no setting, command, parameter, or project-format change. The stated test results are from the author's report. Source inspection was limited to the summary; the ordering and bounds logic were not read line by line.

Full details: Out of Scope Changes check

Explanation

Every changed file connects to the placement feature in [#3500]. crates/render/src/lib.rs and area_labels.rs hold the placement logic and the box-fit helper. crates/render/src/polygon_labels_tests.rs holds tests for that logic, and one existing test is changed so it checks each candidate stays inside the polygon instead of pinning a count. crates/tiles/src/symbols.rs changes only the visibility of signed_distance so the render crate can use it. crates/ui-egui/src/labelpane.rs changes only the tooltip text for the existing checkbox. crates/engine/src/cmd/labels.rs changes only the otherPositions help text. docs/user-guide/cartography.md describes the new behavior. No unrelated behavior, setting, command, or format change appears in the summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit maps a path inside,
Past occupied spots, it hops with pride.
It checks each box stays in the view,
And skirts each hole as labels queue.
If there’s no room, around it goes,
Then tucks its map beneath its nose.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 9f84b7a9-e15c-40d6-868b-4705e2a93d5d
Commits

Reviewing files that changed from the base of the PR and between 44d7e19 and 03d57a0.

Files selected for processing: 7

  • crates/engine/src/cmd/labels.rs
  • crates/render/src/area_labels.rs
  • crates/render/src/lib.rs
  • crates/render/src/polygon_labels_tests.rs
  • crates/tiles/src/symbols.rs
  • crates/ui-egui/src/labelpane.rs
  • docs/user-guide/cartography.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

Reviewed the labels change (render lib, area_labels, symbols, the tests, the param help, the tooltip and the user guide) against pr-context/AGENTS.md. The prior-review file has no threads, so no earlier concerns were left alone.

Bugs

None found. I checked these paths and they hold:

  • The search_origin, first, own and locked_later interplay, including the early continue.
  • Lock slot indexing: place 2 onward maps to later[0..], and the NaN placeholders keep the numbering.
  • Coordinate frames: pole and candidates are in shown-view points, and slot converts them to the frame's points.
  • holds reach math, including the clamped aspect.
  • The ways/along refactor is equivalent to the old code.
  • Clearance spacing (w + 2·clearance) gives a gap of 2·clearance, which matches the param's meaning.
  • Holes are handled by even-odd in signed_distance.
  • seen tests two opposite corners, which is sufficient because on_shown is a rectangle.

Confidence: high.

Security

None found.

Performance

  • Eager per-label work on the frame path (crates/render/src/lib.rs:6916). Every polygon label that fits its box copies its thinned rings and runs up to 16 signed_distance passes over all edges, whether or not its first place is free. For a dense layer of small polygons zoomed in, that adds up each time labels are laid out. The PR gives no timing. Confidence: medium.

Quality

  • Doc promise vs. approximation (crates/render/src/area_labels.rs:411). The guide says a name goes further inside "only where the whole name is inside the polygon". For outlines above MAX_VERTICES, the test runs on the thinned ring, which can pass over a notch. The PR body discloses this, but the user guide doesn't. Confidence: medium.
  • crates/engine/src/cmd/labels.rs:690: the doc comment still describes the old behaviour and doesn't mention the places further inside.
  • The old tl! help string stays in the i18n/*.tsv catalogs as orphaned translations, and the new string shows in English until the release pass. That is acceptable under AGENTS.md, but worth noting.
  • signed_distance is now pub in rustgis-tiles, which widens that crate's public API. Fine, but intentional.

AGENTS.md

No breaks found. The param help, the tooltip and the guide were updated together. The command surface is unchanged (help text only). No wasm, thread or cancel paths are touched. tl! wraps the new string.

Change size

Compared with the base branch (the whole PR, merge base fdf25d1 to head 03d57a0).

Measure Value
Files changed 7 (0 added, 7 modified, 0 removed, 0 renamed)
Size before 1.32 MB
Size after 1.33 MB
Total size change +12.5 KB
Path Before After Change
crates/render/src/polygon_labels_tests.rs 41.4 KB 49.4 KB +8.0 KB
crates/render/src/lib.rs 937.7 KB 940.8 KB +3.1 KB
crates/render/src/area_labels.rs 36.9 KB 37.8 KB +927 B
docs/user-guide/cartography.md 80.5 KB 80.9 KB +345 B
crates/engine/src/cmd/labels.rs 73.1 KB 73.2 KB +115 B
crates/ui-egui/src/labelpane.rs 142.4 KB 142.4 KB +47 B
crates/tiles/src/symbols.rs 41.5 KB 41.5 KB +4 B

Change size

Compared with the base branch: the whole PR, merge base fdf25d1 to head 03d57a0.

Measure Value
Files changed 7 (0 added, 7 modified, 0 removed, 0 renamed)
Size before 1.32 MB
Size after 1.33 MB
Total size change +12.5 KB
Path Before After Change
crates/render/src/polygon_labels_tests.rs 41.4 KB 49.4 KB +8.0 KB
crates/render/src/lib.rs 937.7 KB 940.8 KB +3.1 KB
crates/render/src/area_labels.rs 36.9 KB 37.8 KB +927 B
docs/user-guide/cartography.md 80.5 KB 80.9 KB +345 B
crates/engine/src/cmd/labels.rs 73.1 KB 73.2 KB +115 B
crates/ui-egui/src/labelpane.rs 142.4 KB 142.4 KB +47 B
crates/tiles/src/symbols.rs 41.5 KB 41.5 KB +4 B

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3503/
Web app https://preview.rustgis.app/pr-3503/app/
Commit 03d57a0

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

@jgregoriorincon
jgregoriorincon added this pull request to the merge queue Dec 1, 2026
Merged via the queue into opengeos:main with commit 5453f49 Dec 1, 2026
28 checks passed
@jgregoriorincon
jgregoriorincon deleted the feat/3500-polygon-labels-inside branch December 1, 2026 01:11
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.

[Feature]: Polygon labels try more places inside the polygon when its best two are taken (label placement step 4b, part 2)

1 participant