Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,10 @@ fn calculate_geometry(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String>
}
}
if let Some(v) = f.attrs.get_mut(fa) {
*v = Value::Float((a / 1e4).round() / 100.0);
*v = Value::Float(a / 1e6);
}
if let Some(v) = f.attrs.get_mut(fp) {
*v = Value::Float((per / 10.0).round() / 100.0);
*v = Value::Float(per / 1e3);
}
}
}
Expand All @@ -792,7 +792,7 @@ fn calculate_geometry(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String>
_ => 0.0,
};
if let Some(v) = f.attrs.get_mut(fl) {
*v = Value::Float((len / 10.0).round() / 100.0);
*v = Value::Float(len / 1e3);
}
}
}
Expand Down Expand Up @@ -954,6 +954,27 @@ mod tests {
}
}

/// Calculate Geometry stores full-precision values (#513): rounding to 0.01 km² gave every
/// polygon under half a hectare an area of 0, and lines under 5 m a length of 0.
#[test]
fn calculate_geometry_keeps_small_values() {
// A square of about 50 m (0.00045°) at the equator, and one of its sides as a line.
let s = 0.00045;
let mut polys = Dataset::new("p", GeometryType::Polygon, vec![]);
polys.push(Some(sq(0.0, 0.0, s)), vec![]);
let mut lines = Dataset::new("l", GeometryType::Polyline, vec![]);
lines.push(Some(Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(0.00002, 0.0)]])), vec![]);
let m = Map(HashMap::from([("p".to_string(), polys), ("l".to_string(), lines)]));
let p = layer(crate::run("calculateGeometry", &m, &serde_json::json!({"input": "p"})).unwrap());
let area = p.value(0, p.field_index("area_km2").unwrap()).as_f64().unwrap();
let per = p.value(0, p.field_index("perimeter_km").unwrap()).as_f64().unwrap();
assert!((area - 0.0025).abs() < 0.0001, "≈ 50 m × 50 m: {area}");
assert!((per - 0.2).abs() < 0.001 && per != 0.2, "≈ 200 m, unrounded: {per}");
let l = layer(crate::run("calculateGeometry", &m, &serde_json::json!({"input": "l"})).unwrap());
let len = l.value(0, l.field_index("length_km").unwrap()).as_f64().unwrap();
assert!((len - 0.00222).abs() < 0.00001, "≈ 2.2 m: {len}");
}

/// A choice outside the declared ones is an error naming them, never a guess (#406): "meter"
/// was a buffer in kilometres. A choice in another case is the declared one.
#[test]
Expand Down
10 changes: 10 additions & 0 deletions crates/data/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,8 @@ fn csv_cell(v: &Value) -> String {
match v {
Value::Null => String::new(),
Value::Text(t) if t.starts_with(FORMULA_START) => format!("'{t}"),
// The shortest text that reads back as the same number: `Display` rounds to 6 decimals.
Value::Float(f) => f.to_string(),
other => other.to_string(),
}
}
Expand Down Expand Up @@ -1995,6 +1997,14 @@ mod tests {
assert_eq!(to_csv(&back).unwrap(), once, "export/import is stable");
}

/// CSV export writes numbers as stored (#513): through `Display` a 0.4 m² area in km² was 0.
#[test]
fn csv_floats_keep_full_precision() {
let (ds, _) = read_csv("v\n0.0000004\n0.002492708\n1\n", "t").unwrap();
let out = to_csv(&ds).unwrap();
assert_eq!(out, "v\n0.0000004\n0.002492708\n1\n");
}

#[test]
fn csv_bom_injection_and_column_names() {
let (ds, _) = read_csv("\u{feff}lon,lat,longitude,note\n1,2,9,=SUM(A1)\n", "t").unwrap();
Expand Down
Loading