Skip to content

Analysis: Calculate Geometry stores full-precision area, perimeter and length - #520

Merged
giswqs merged 1 commit into
mainfrom
fix/issue-513-calc-geometry-precision
Oct 18, 2026
Merged

giswqs merged 1 commit into
mainfrom
fix/issue-513-calc-geometry-precision

Conversation

@giswqs

@giswqs giswqs commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Root cause

calculate_geometry in crates/analysis/src/tools.rs rounded area_km2, perimeter_km and length_km to two decimals (0.01 km², 0.01 km) before storing them, so any polygon under about half a hectare got area_km2 = 0 and lines under 5 m got length_km = 0.

A second, smaller loss sat in CSV export: csv_cell wrote Value::Float through Display, which rounds to six decimals for the attribute table, so an area below about 0.5 m² in km² still exported as 0.

Fix

  • Calculate Geometry stores the computed values unrounded (m² / 1e6, m / 1e3).
  • CSV export writes floats with f64::to_string, the shortest text that reads back as the same number (integers still print without a decimal point). The attribute table keeps its six-decimal display.

Tests

  • tools::tests::calculate_geometry_keeps_small_values: a ~50 m square and a ~2.2 m line get non-zero, unrounded area, perimeter and length (fails on main with area 0).
  • io::tests::csv_floats_keep_full_precision: 0.0000004 and 0.002492708 survive a CSV round trip (fails on main).

Verification

Reran the issue's CLI repro (three squares in EPSG:25832, calculateGeometry, share.exportLayer to CSV). squares_out.csv now reads:

side,area_km2,perimeter_km
50 m,0.0024926971032904945,0.19970784105234113
100 m,0.009970787900749664,0.39941567181349114
1 km,0.9970778657733731,3.9941548486965446

The 50 m square matches the expected ~0.0024927 km². The wasm build of rustgis-web passes. Locally, cargo xtask ci passes except the letting_agents_control_rustgis_starts_the_relay_and_shows_how_to_connect UI smoke test, which fails the same way on a clean origin/main checkout on this machine and passes in CI, so it is unrelated to this change.

Fixes #513

calculateGeometry rounded area_km2, perimeter_km and length_km to two
decimals before storing them, so every polygon under about half a
hectare got an area of 0 and short lines a length of 0. Store the
computed values as they are.

CSV export wrote Double values through Display, which rounds to six
decimals, so a sub-square-metre area in km2 still exported as 0. Write
floats as the shortest text that reads back as the same number.

Fixes #513
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2a178f1d-4143-4428-97e8-b41ed5178430
📥 Commits

Reviewing files that changed from the base of the PR and between 67c8cb8 and e706526.

📒 Files selected for processing: 2
  • crates/analysis/src/tools.rs
  • crates/data/src/io.rs

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

📝 Walkthrough

Geometry calculations now retain unrounded area, perimeter, and line-length values. CSV serialization now formats floating-point values with f64::to_string(). Tests cover small geometry measurements and representative floating-point CSV values.

Changes: Precision preservation

Layer / File(s) Summary
Retain geometry measurement precision
crates/analysis/src/tools.rs
Polygon area and perimeter, and polyline length, are stored without rounding. A test checks small measurements.
Preserve floating-point values in CSV
crates/data/src/io.rs
CSV floating-point values use to_string(). A test checks the output for small and fractional values.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: jgregoriorincon

Merge Risk: ⚪ Minimal · up to e7065

Small geometry measurements and their CSV values now retain precision. No actionable merge-blocking risk remains in the reviewed changes.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: storing geometry measurements at full precision.
Description check Passed The description explains the root cause, fix, and tests, and references the related issue with “Fixes #513.” It does not use the template headings exactly, but it provides the required information in …
Linked Issues check Passed Issue #513 requires Calculate Geometry to store area, perimeter, and line length without explicit two-decimal rounding. The diff changes these values to a / 1e6, per / 1e3, and len / 1e3. The ne…
Out of Scope Changes check Passed The changes remain within issue #513. The calculate_geometry changes remove precision loss from the requested fields. The csv_cell change prevents the same stored precision from being lost during …
Docstring Coverage Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files.
Full details: Description check

Explanation

The description explains the root cause, fix, and tests, and references the related issue with “Fixes #513.” It does not use the template headings exactly, but it provides the required information in equivalent sections.

Full details: Linked Issues check

Explanation

Issue #513 requires Calculate Geometry to store area, perimeter, and line length without explicit two-decimal rounding. The diff changes these values to a / 1e6, per / 1e3, and len / 1e3. The new calculate_geometry_keeps_small_values test checks nonzero, unrounded polygon and line results. The CSV change also preserves these stored float values during export, and csv_floats_keep_full_precision tests round trips for small values.

Full details: Out of Scope Changes check

Explanation

The changes remain within issue #513. The calculate_geometry changes remove precision loss from the requested fields. The csv_cell change prevents the same stored precision from being lost during CSV export. The added tests cover both behaviors. No unrelated production or test changes appear in the reviewed diff.

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • 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 checks the measured square,
No rounded crumbs are hiding there.
Small lines keep their length in view,
CSV carries decimals through.
The bunny hops, precise and true.

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

Code review

I read the full diff (two files) and found nothing blocking. I did not build the code or run the tests.

Bugs

  • None found. calculate_geometry now stores a / 1e6 (m² to km²) and per / 1e3 and len / 1e3 (m to km). That matches the _km2 and _km field names and the old formulas, minus the rounding. The new test's expected values check out: a 0.00045° square is about 50 m, so about 0.0025 km² with a perimeter near 0.2002 km, and 0.00002° is about 2.22 m. (confidence: high)

Security

  • None. csv_cell still applies the formula-injection guard to text values before the new Float arm. (confidence: high)

Performance

  • None.

Quality

  • CSV float noise (low confidence, about 30%). f.to_string() gives the shortest string that round-trips, so values computed elsewhere, such as 0.1 + 0.2, export as 0.30000000000000004. That is more accurate but noisier in a spreadsheet. It is probably the intended trade-off for [Bug]: Calculate Geometry stores area_km2 and perimeter_km rounded to 2 decimals, so small polygons get 0.0 #513, and the comment explains it. A one-line note in the CSV docs could help.
  • NaN and infinity in CSV (low confidence, about 25%). f64::to_string writes NaN or inf for these values. I did not check whether the old Display path did the same. Consider a test covering that, or confirm it is unchanged.
  • Test coverage (minor). csv_floats_keep_full_precision reads the CSV back as floats. A direct Value::Float(4e-7) and a very large or very small float (for example 1e-300) would exercise csv_cell more directly, because Rust Display never uses exponent notation.

AGENTS.md

Change size

Compared: the whole PR against main (merge base 67c8cb8 to PR head e706526)

Metric Value
Files changed 2 (added 0, modified 2, removed 0, renamed 0)
Size before 169.8 KB
Size after 171.6 KB
Total size change +1.9 KB
File Before After Change
crates/analysis/src/tools.rs 67.7 KB 69.1 KB +1.4 KB
crates/data/src/io.rs 102.0 KB 102.5 KB +479 B

@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-520/
Web app https://preview.rustgis.app/pr-520/app/
Commit e706526

GitHub Pages takes a minute or two to publish a new commit.

@giswqs
giswqs merged commit 90b9f43 into main Oct 18, 2026
17 checks passed
@giswqs
giswqs deleted the fix/issue-513-calc-geometry-precision branch October 18, 2026 21:37
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.

[Bug]: Calculate Geometry stores area_km2 and perimeter_km rounded to 2 decimals, so small polygons get 0.0

1 participant