Repository navigation
fix(geom): switch measurement units for negative distances and areas (#1284) - #1285
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 15 minutes. View limit details
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Magnitude-based unit selection crates/geom/src/lib.rs, crates/ui-egui/src/measure_units.rs |
Metric and imperial distance and area formatters use absolute magnitude to select units. Tests check negative values across unit thresholds. |
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Low
Possibly related PRs
- opengeos/RustGIS#719: Adds the imperial distance and area formatters whose unit-selection thresholds this change updates.
- opengeos/RustGIS#923: Adds sub-metre unit selection and rounding in
format_distance, which this change extends to larger unit thresholds.
Suggested reviewers: mavrick-f, subarna-kandel
Merge Risk: 
Minimal · previous review 33735
-
The negative-value unit promotion changes are mergeable after normal checks. The previously reported sub-millimetre sign limitation is not introduced by this change.
Pre-merge checks: 
5
-
Full details: Title check
-
Explanation
The title names the fix and its scope: negative distances and areas now switch to larger measurement units. It matches the raw summary and the linked objectives. It is specific and readable, and the issue number matches the linked issue. The title is 76 characters, which is slightly long but acceptable.
Full details: Description check
-
Explanation
The description follows the template's sections: Summary, Testing, UI evidence, and Related issue. The Summary explains the cause and the fix. The Testing section lists the added test cases and the commands run with results. The UI evidence section keeps the 'No visible UI change.' line, which the template allows when no visible UI change exists. The Related issue section says 'Fixes
#1284'. The description is complete and specific.
Full details: Linked Issues check
-
Explanation
Issue
#1284requires magnitude-based unit promotion for negative distances and areas while preserving the sign.crates/geom/src/lib.rsnow uses absolute rounded magnitudes for metric distance and area thresholds.crates/ui-egui/src/measure_units.rsnow uses absolute rounded feet and absolute area for imperial thresholds. Existing formatting retains the signed value. Added tests cover negative metric and imperial distance and area thresholds.
Full details: Out of Scope Changes check
-
Explanation
The changes remain within issue
#1284. They modify the four specified formatting functions and add focused automated tests for the required negative-value behavior. No unrelated API or behavior changes are identified in the supplied whole-PR summary.
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 2 files.
-
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.
A rabbit checks the measured mile,
Then keeps the minus sign in style.
From metres small to acres wide,
The thresholds now use magnitude.
The numbers keep their proper sign,
And tests mark every boundary line.
Code reviewBugs: None found. The change makes the unit-promotion thresholds in Security: None. The change adds no input parsing, no I/O and no Performance: None. The extra Quality: Nit, low confidence it matters. AGENTS.md: Compliant. The new literals reuse existing Prior review threads: none on this PR, so no earlier concerns were left alone. Change sizeCompared with
|
📖 Docs preview
GitHub Pages takes a minute or two to publish a new commit. |
abd08e3 to
54fd5fb
Compare
| } else if m != 0.0 && cm.abs() < 100.0 { | ||
| format!("{cm:.0} cm") | ||
| } else if m.round() < 1000.0 { | ||
| } else if m.abs().round() < 1000.0 { |
There was a problem hiding this comment.
The sign-aware thresholds look right. f64::round rounds half away from zero, so m.abs().round() matches the positive path exactly, and the new tests cover the boundaries (999.5, 99.95 km, 1 acre, 1 mi²).
What I couldn't confirm is that anything reaches these functions with a negative value. In the UI, the measure tool's area comes from wgs84_ring_perimeter_area_m2, which returns an unsigned area. The ribbon clip and label distances are fixed positive constants. The elevation formatter has no unit thresholds, so it isn't affected. Could you point to the path behind #1284 (a stored value, a CLI or Python output, or a tool result)? If it goes through format_distance, a test at that call site would show the fix end to end.
There was a problem hiding this comment.
format_distance and format_area (along with measure_units::distance and measure_units::area) are public formatting utilities in rustgis_geom and rustgis_ui_egui. While the interactive polygon/path measurement tools compute non-negative geometric magnitudes, these formatters serve general-purpose display across the library, Python bindings, and downstream analysis/delta workflows (such as buffer/elevation offset deltas, net cut-and-fill area balances, or signed chainage/stationing offsets from a reference alignment).
Prior to this fix, negative values never scaled up to higher units (displaying e.g. -12500 m instead of -12.5 km) because negative numbers are always strictly less than the positive thresholds (< 1000.0, < 5280.0, etc.).
This fix is defensive at the library/public API layer to ensure symmetrical unit promotion based on magnitude while retaining the negative sign. I have also updated the doc comments on format_distance, format_area, and measure_units to document this behavior explicitly, and added sub-foot negative unit test coverage.
Code reviewBugs: None found. Both Security: None. No new parsing or input handling. (high confidence) Performance: None. (high confidence) Quality: Nothing blocking. The two inconsistent threshold styles (imperial distance compares the rounded value, imperial area compares the raw value) predate this PR, so I left them alone. The new tests cover the boundaries well. (medium confidence) AGENTS.md: Compliant. No Open question (posted inline on Prior review threads: none existed, so I left no earlier concerns alone. Change sizeCompared: the whole PR against main (merge base 5f9daf1 to PR head 54fd5fb).
|
54fd5fb to
d04b144
Compare
Code reviewI found no bugs or security issues in this PR. The sign handling is correct for every input I traced. One nit and one open question are below. The nit could not be posted inline because its lines are outside the diff, so it appears here. Bugs: none found. Confidence: high.
Security: none. The change only touches number formatting. Performance: none. The change adds one Quality:
AGENTS.md: no violations. No new user-visible strings, so no Open question (already raised, left alone): the unresolved thread on
So the fix may be defensive rather than fixing a reachable bug. Please either name the path behind #1284 or note that the change is defensive. A test at that call site would show the fix end to end. Earlier concerns left alone: 1, the thread on Change sizeCompared: the whole PR against main (merge base 5f9daf1 to PR head d04b144).
|
…pengeos#1284) Compare magnitude with .abs() against unit thresholds so negative distances and areas scale to km, km², mi, and acres symmetrically with positive values. Signed-off-by: Talha Shafeeq <talha.shafeeq.ts@gmail.com>
d04b144 to
337358b
Compare
| /// Formats a distance in metres as `0.2 mm` / `5 mm` / `20 cm` / `850 m` / `12.5 km`: under a | ||
| /// metre in centimetres or millimetres, so a scale bar on a site plan (#902) doesn't read `0 m`. | ||
| /// Non-finite values display as `—`. | ||
| /// A negative distance keeps its sign and picks its unit by magnitude (-12.5 km). Non-finite |
There was a problem hiding this comment.
Bug (pre-existing branch, sign lost): this doc line says a negative distance keeps its sign, but the sub-millimetre branch at crates/geom/src/lib.rs:430 does not. For m = -0.00001, tenths rounds to -0.0, so the else arm returns "< 0.1 mm" with no sign. The positive 0.00001 case is correct, so the output for the negative input is misleading.
A small fix is to pick the bound by sign at line 430, e.g. else if m < 0.0 { "> -0.1 mm".to_string() } else { "< 0.1 mm".to_string() }, and add assert_eq!(format_distance(-0.00001), "> -0.1 mm"); next to the existing 0.00001 assertion. Confidence: high on the behaviour, low on severity, since no in-app caller passes negatives (labels.distanceRange rejects them).
There was a problem hiding this comment.
Good catch. Added > -0.1 mm for sub-millimetre negative distances in commit f4613ec along with the corresponding test case.
Code reviewBugs
Security
Performance
Quality
AGENTS.md
Prior concern left alone (1)
Change sizeCompared with
|
Display '> -0.1 mm' for negative distances smaller than half a millimetre to preserve sign symmetrically with positive '< 0.1 mm'. Signed-off-by: Talha Shafeeq <talha.shafeeq.ts@gmail.com>
Code reviewVerdict: no bugs or regressions found. The change is small, sign-symmetric, and matches the existing Bugs
Security
Performance
Quality
AGENTS.md
Earlier review threads
Change sizeCompared: the whole PR against main (merge base 5f9daf1 to PR head f4613ec). Sizes use 1 KB = 1024 B.
|
|
To clarify and close out the reachability question: yes, this change is defensive at the |
Summary
When formatting negative distances or areas, unit promotion thresholds were compared against signed positive constants (
m.round() < 1000.0,(m / 100.0).round() < 1000.0,m2.round() < 1.0e6,ft.round() < 5280.0, etc.). Because negative values are always less than positive thresholds, negative measurements never scaled to larger units (such askm,km²,mi, oracres), displaying values like"-12500 m"instead of"-12.5 km"or"-65617 ft"instead of"-12.4 mi".This updates the threshold comparisons to check magnitude with
.abs()(matching howmm.abs()andcm.abs()are already handled), scaling negative distances and areas symmetrically with positive ones while preserving their signs.Testing
crates/geom/src/lib.rsfor negative metric distance thresholds (-850 m,-999 m,-1.0 km,-12.5 km,-99.9 km,-100 km) and area thresholds (-999999 m²,-1.0 km²,-5.0 km²).crates/ui-egui/src/measure_units.rsfor imperial negative distance (-1 ft,-1.0 mi,-12.4 mi) and negative area (-1 ft²,-1.0 acres,-10.0 acres,-1.0 mi²,-3.9 mi²).cargo test -p rustgis-geom(all 17 tests passed).cargo test -p rustgis-ui-egui --lib -- measure_units(passed).cargo clippy -p rustgis-geom -p rustgis-ui-egui --all-targets -- -D warnings(passed, 0 warnings).Related issue
Fixes #1284
Summary by CodeRabbit