Skip to content

[Bug]: Negative distance and area measurements never scale to larger units (km, km², mi, acres) #1284

Closed

Description

@TalhaHunter101

Before you start

[✓] I searched the existing issues https://github.com/opengeos/RustGIS/issues and did not find a duplicate.
[✓] This is a bug report, not a usage question.

What happened?

When measuring or formatting negative distances or areas (e.g. from signed offsets, relative coordinates, elevation or delta calculations, or reverse paths), measurements never scale to larger units. For example:

  • A distance of -12,500.0 m is formatted as "-12500 m" instead of "-12.5 km".
  • A distance of -120,000.0 m is formatted as "-120000 m" instead of "-120 km".
  • An area of -5,000,000.0 m² is formatted as "-5000000 m²" instead of "-5.0 km²".
  • Under imperial measurement units, -20,000.0 m is formatted as "-65617 ft" instead of "-12.4 mi".
  • An imperial area of -10,000,000.0 m² is formatted as "-107639104 ft²" instead of "-3.9 mi²".

Cause

In crates/geom/src/lib.rs (format_distance and format_area) and crates/ui-egui/src/measure_units.rs (distance and area), unit thresholds compare signed values directly against positive constants without taking the absolute magnitude into account:

// crates/geom/src/lib.rs
} else if m.round() < 1000.0 {
    format!("{m:.0} m")
} else if (m / 100.0).round() < 1000.0 {
    format!("{:.1} km", m / 1000.0)
...
if m2.round() < 1.0e6 { format!("{m2:.0} m²") } else { format!("{:.1} km²", m2 / 1.0e6) }
// crates/ui-egui/src/measure_units.rs
if ft.round() < 5280.0 {
...
if m2 < geom::SQUARE_METRES_PER_ACRE {
...
} else if m2 < geom::SQUARE_METRES_PER_SQUARE_MILE {

Because any negative value is algebraically less than positive numbers (-12500.0 < 1000.0 is true), the comparison branches early and never reaches the larger unit branches (km, km², mi, acres). Small negative distances (mm, cm) already correctly use .abs(), but the larger thresholds were missed.

Steps to reproduce

Call format_distance(-12500.0) or format_area(-5000000.0) in rustgis_geom, or measure_units::distance(-20000.0, true) in rustgis_ui_egui.
Observe that values stay in raw metres/square metres or feet/square feet regardless of magnitude.

Expected behavior

Unit switching thresholds should check magnitude (.abs()), preserving sign so that:

  • -12,500.0 m formats as "-12.5 km"
  • -5,000,000.0 m² formats as "-5.0 km²"
  • Imperial -20,000.0 m formats as "-12.4 mi"
  • Imperial -10,000,000.0 m² formats as "-3.9 mi²"

Version

main at 6692b1b

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions