diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index 384218bd9e..c67c69181d 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -3355,7 +3355,9 @@ fn dissolve(inp: &dyn Inputs, p: &Json) -> Result { "Dissolve: {not_polygons} feature(s) left out, with their values: no geometry, an empty polygon, or a point or line rather than a polygon" ) }); } - messages.extend(rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default())); + let (said, rounded) = rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default()); + messages.extend(said); + losses.extend(rounded); Ok(ToolResult { losses, output: Output::Layer(out), messages }) } @@ -6054,8 +6056,9 @@ fn count_points_on(inp: &dyn Inputs, p: &Json, threads: Option) -> Result } } let mut messages = vec![format!("Summarize Within: {total} points counted")]; - messages.extend(rounded_notice(&rounded, out.fields.get(first..).unwrap_or_default())); - Ok(ToolResult { losses: Vec::new(), output: Output::Layer(out), messages }) + let (said, losses) = rounded_notice(&rounded, out.fields.get(first..).unwrap_or_default()); + messages.extend(said); + Ok(ToolResult { losses, output: Output::Layer(out), messages }) } /// A lon/lat line or ring as a `geo` line string, in degrees. @@ -6471,8 +6474,9 @@ fn summary_statistics(inp: &dyn Inputs, p: &Json) -> Result } let n = out.features.len(); let mut messages = vec![format!("Summary Statistics: {n} rows")]; - messages.extend(rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default())); - Ok(ToolResult { losses: Vec::new(), output: Output::Table(out), messages }) + let (said, losses) = rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default()); + messages.extend(said); + Ok(ToolResult { losses, output: Output::Table(out), messages }) } /// The output fields of a statistics list over `ds`: `_` for each request, typed by @@ -6581,17 +6585,23 @@ fn statistic_values( /// naming their output fields `fields` (one per request, as `rounded` counts them), as a decimal /// field holds whole numbers exactly only up to 2^53; and the whole numbers written as text past /// the i64 range that were read as decimals, whose statistics aren't checked, naming their input -/// fields. Empty when nothing was rounded. -fn rounded_notice(rounded: &Rounded, fields: &[Field]) -> Vec { +/// fields. The first is a loss, with its code; the second says they may be rounded, so it is a +/// plain notice. Both empty when nothing was rounded. +fn rounded_notice(rounded: &Rounded, fields: &[Field]) -> (Vec, Vec) { let mut notices = Vec::new(); + let mut losses = Vec::new(); + // Each counted result came out rounded: a value changed. let total: usize = rounded.results.iter().sum(); if total > 0 { let names: Vec<&str> = rounded.results.iter().zip(fields).filter(|(n, _)| **n > 0).map(|(_, f)| f.name.as_str()).collect(); let results = if total == 1 { "result" } else { "results" }; - notices.push(format!( - "{total} {results} rounded in {}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)", - names.join(", ") - )); + losses.push(rustgis_data::loss::LossMessage { + code: rustgis_data::loss::LossCode::ChangedRounded, + text: format!( + "{total} {results} rounded in {}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)", + names.join(", ") + ), + }); } let total: usize = rounded.texts.iter().map(|(_, n)| n).sum(); if total > 0 { @@ -6603,7 +6613,7 @@ fn rounded_notice(rounded: &Rounded, fields: &[Field]) -> Vec { names.join(", ") )); } - notices + (notices, losses) } /// The statistics list of a tool that adds statistics to its own output (Dissolve, Summarize @@ -6810,8 +6820,9 @@ fn pivot_table(inp: &dyn Inputs, p: &Json) -> Result { } let n = out.features.len(); let mut messages = vec![format!("Pivot Table: {n} rows, {} pivot fields", columns.len())]; - messages.extend(rounded_notice(&rounded, out.fields.get(first_pivot_field..).unwrap_or_default())); - Ok(ToolResult { losses: Vec::new(), output: Output::Table(out), messages }) + let (said, losses) = rounded_notice(&rounded, out.fields.get(first_pivot_field..).unwrap_or_default()); + messages.extend(said); + Ok(ToolResult { losses, output: Output::Table(out), messages }) } /// Whether a pivot value is missing: null, or a number that isn't one. @@ -8380,7 +8391,9 @@ fn spatial_join_on(inp: &dyn Inputs, p: &Json, threads: Option) -> Result "{total} matched {numbers} in {names} {were} left out: a whole-number sum takes whole numbers in the 64-bit range, the other rules numbers a decimal holds" ) }); } - messages.extend(rounded_notice(&rounded, &merged_fields)); + let (said, rounded) = rounded_notice(&rounded, &merged_fields); + messages.extend(said); + losses.extend(rounded); Ok(ToolResult { losses, output: Output::Layer(out), messages }) } diff --git a/crates/analysis/tests/integration/spatial_join_merge.rs b/crates/analysis/tests/integration/spatial_join_merge.rs index 9154463434..adca815c01 100644 --- a/crates/analysis/tests/integration/spatial_join_merge.rs +++ b/crates/analysis/tests/integration/spatial_join_merge.rs @@ -106,7 +106,7 @@ fn join(layers: &Layers, merge: Json) -> ToolResult { const JOINED: &str = "Spatial Join: 4 features joined (8 matches)"; fn rounded(n: &str, fields: &str) -> String { - format!("{n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)") + format!("ChangedRounded: {n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)") } #[test] @@ -299,7 +299,7 @@ fn a_number_too_large_for_a_decimal_is_left_out_and_counted() { }; let res = join(&layers, json!([{"field": "n", "rule": "mean"}, {"field": "d", "rule": "sum"}])); // D's mean of n leaves out "1e309", and its sum of d "-1E400"; C's mean of n is still rounded. - assert_eq!(said(&res), [JOINED.to_string(), rounded("1 result", "join_n"), notice("2 matched numbers", "join_n, join_d")]); + assert_eq!(said(&res), [JOINED.to_string(), notice("2 matched numbers", "join_n, join_d"), rounded("1 result", "join_n")]); let out = layer(res); assert_eq!((column(&out, "join_n")[3].clone(), column(&out, "join_d")[3].clone()), (Value::Float(3.0), Value::Float(4.5))); for (n_rule, d_rule) in [("max", "min"), ("min", "max")] { diff --git a/crates/analysis/tests/integration/summary_fields.rs b/crates/analysis/tests/integration/summary_fields.rs index 1a584c0994..1a0141fdb9 100644 --- a/crates/analysis/tests/integration/summary_fields.rs +++ b/crates/analysis/tests/integration/summary_fields.rs @@ -42,6 +42,11 @@ fn column<'a>(ds: &'a Dataset, name: &str) -> Vec<&'a Value> { fn names(ds: &Dataset) -> Vec<&str> { ds.fields.iter().map(|f| f.name.as_str()).collect() } +/// What a run says: its messages, then its losses, each after its code. +fn said(r: &ToolResult) -> Vec { + r.messages.iter().cloned().chain(r.losses.iter().map(|l| format!("{:?}: {}", l.code, l.text))).collect() +} + fn layer(r: ToolResult) -> Dataset { match r.output { Output::Layer(d) => d, @@ -280,7 +285,7 @@ fn dissolve_without_statistics_is_unchanged() { base["field"] = json!(f); } let res = rustgis_analysis::run("dissolve", &layers, &base).unwrap(); - let messages = res.messages.clone(); + let messages = said(&res); let plain = layer(res); let expected: &[&str] = if field.is_some() { &["zone", "count"] } else { &["count"] }; assert_eq!(names(&plain), expected); @@ -290,14 +295,14 @@ fn dissolve_without_statistics_is_unchanged() { // A separator isn't read without statistics. p["separator"] = json!(5); let res = rustgis_analysis::run("dissolve", &layers, &p).unwrap(); - assert_eq!(res.messages, messages, "{empty}"); + assert_eq!(said(&res), messages, "{empty}"); assert_eq!(layer(res), plain, "{empty}"); } // Statistics add fields after the others and leave them as they were. let mut p = base.clone(); p["statistics"] = json!([{"field": "pop", "statistic": "SUM"}]); let res = rustgis_analysis::run("dissolve", &layers, &p).unwrap(); - assert_eq!(res.messages, messages); + assert_eq!(said(&res), messages); let with = layer(res); assert_eq!(with.fields[..plain.fields.len()], plain.fields[..]); for (a, b) in with.features.iter().zip(plain.features.iter()) { @@ -423,10 +428,10 @@ fn summarize_within_adds_statistics_of_the_points_in_each_polygon() { .unwrap(); // SUM and MAX of `big` near i64::MAX are rounded in west and east, and the tool says so. assert_eq!( - res.messages, + said(&res), [ "Summarize Within: 8 points counted", - "4 results rounded in SUM_big, MAX_big: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)" + "ChangedRounded: 4 results rounded in SUM_big, MAX_big: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)" ] ); let out = layer(res); @@ -510,14 +515,14 @@ fn summarize_within_statistics_take_only_selected_points_in_record_order() { fn summarize_within_without_statistics_is_unchanged() { let layers = Layers::default().with(zones()).with(sites()); let res = rustgis_analysis::run("countPoints", &layers, &json!({"polygons": "zones", "points": "sites"})).unwrap(); - let messages = res.messages.clone(); + let messages = said(&res); let plain = layer(res); assert_eq!(names(&plain), ["name", "point_count"]); assert_eq!(column(&plain, "point_count"), [&int(4), &int(4), &int(0), &int(0)]); for empty in [json!([]), json!(""), Json::Null] { let p = json!({"polygons": "zones", "points": "sites", "statistics": empty, "separator": []}); let res = rustgis_analysis::run("countPoints", &layers, &p).unwrap(); - assert_eq!(res.messages, messages, "{empty}"); + assert_eq!(said(&res), messages, "{empty}"); assert_eq!(layer(res), plain, "{empty}"); } // From JSON text, the same as from a list. @@ -604,7 +609,7 @@ fn a_record_selected_twice_counts_once() { let statistics = json!([{"field": "val", "statistic": "COUNT"}, {"field": "val", "statistic": "SUM"}]); let res = rustgis_analysis::run("countPoints", &layers, &json!({"polygons": "zones", "points": "sites", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Summarize Within: 3 points counted"]); + assert_eq!(said(&res), ["Summarize Within: 3 points counted"]); let out = layer(res); assert_eq!(column(&out, "point_count"), [&int(2), &int(1), &int(0), &int(0)]); assert_eq!(column(&out, "COUNT_val"), [&int(2), &int(1), &int(0), &int(0)]); @@ -643,28 +648,28 @@ fn rounded_statistics_are_reported_and_exact_ones_are_not() { {"field": "small", "statistic": "MEAN"}, ]); let note = |n: &str, fields: &str| { - format!("{n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)") + format!("ChangedRounded: {n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)") }; // Dissolve: zone A rounds SUM_n and MAX_n (its MIN is 2), zone B rounds SUM_m (2^53 + 1); // MAX_m, 2^53, fits. CONCATENATE writes the whole numbers as they are. let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "large", "field": "zone", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Dissolve: 2 features".to_string(), note("3 results", "SUM_n, MAX_n, SUM_m")]); + assert_eq!(said(&res), ["Dissolve: 2 features".to_string(), note("3 results", "SUM_n, MAX_n, SUM_m")]); let out = layer(res); assert_eq!(column(&out, "CONCATENATE_n"), [&text("9007199254740993, 2"), &text("3, 4")]); assert_eq!(column(&out, "MIN_n"), [&float(2.0), &float(3.0)]); // One group: SUM_m of 1, 2, 2^53 and 1 in record order passes 2^53 but comes out exact, // 2^53 + 4, so only SUM_n and MAX_n are rounded. let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "large", "statistics": statistics})).unwrap(); - assert_eq!(res.messages[1], note("2 results", "SUM_n, MAX_n")); + assert_eq!(said(&res)[1], note("2 results", "SUM_n, MAX_n")); assert_eq!(column(&layer(res), "SUM_m"), [&float(9_007_199_254_740_996.0)]); // Only zone B: SUM_m alone, one value. let only_b = Layers::default().with(large_numbers()).select("large", &[2, 3]); let res = rustgis_analysis::run("dissolve", &only_b, &json!({"input": "large", "statistics": statistics})).unwrap(); - assert_eq!(res.messages[1], note("1 result", "SUM_m")); + assert_eq!(said(&res)[1], note("1 result", "SUM_m")); // Fields within 2^53: no notice. let clean = json!([{"field": "small", "statistic": "SUM"}, {"field": "n", "statistic": "MIN"}, {"field": "n", "statistic": "COUNT"}]); let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "large", "field": "zone", "statistics": clean})).unwrap(); - assert_eq!(res.messages, ["Dissolve: 2 features"]); + assert_eq!(said(&res), ["Dissolve: 2 features"]); // Summarize Within over the same records as points: a zone holding all of them. let points = run("featureToPoint", &layers, json!({"input": "large", "output": "large_points"})); @@ -674,20 +679,20 @@ fn rounded_statistics_are_reported_and_exact_ones_are_not() { let res = rustgis_analysis::run("countPoints", &layers, &json!({"polygons": "area", "points": "large_points", "statistics": statistics})) .unwrap(); - assert_eq!(res.messages, ["Summarize Within: 4 points counted".to_string(), note("2 results", "SUM_n, MAX_n")]); + assert_eq!(said(&res), ["Summarize Within: 4 points counted".to_string(), note("2 results", "SUM_n, MAX_n")]); let res = rustgis_analysis::run("countPoints", &layers, &json!({"polygons": "area", "points": "large_points", "statistics": clean})).unwrap(); - assert_eq!(res.messages, ["Summarize Within: 4 points counted"]); + assert_eq!(said(&res), ["Summarize Within: 4 points counted"]); // Summary Statistics, by zone and from its single-field shortcut. let layers = Layers::default().with(large_numbers()); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "large", "case": "zone", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 2 rows".to_string(), note("3 results", "SUM_n, MAX_n, SUM_m")]); + assert_eq!(said(&res), ["Summary Statistics: 2 rows".to_string(), note("3 results", "SUM_n, MAX_n, SUM_m")]); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "large", "field": "n"})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows".to_string(), note("2 results", "SUM_n, MAX_n")]); + assert_eq!(said(&res), ["Summary Statistics: 1 rows".to_string(), note("2 results", "SUM_n, MAX_n")]); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "large", "field": "small"})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows"]); + assert_eq!(said(&res), ["Summary Statistics: 1 rows"]); // The shortcut reads a text field's values as numbers: whole numbers written as text past 2^53 // are rounded and reported too. The MEAN, (2^53 + 1 + 1) / 2, is whole and rounded as well. let csv = "code\n9007199254740993\nn/a\n1\n"; @@ -695,7 +700,7 @@ fn rounded_statistics_are_reported_and_exact_ones_are_not() { assert_eq!(field_ty(&codes, "code"), FieldType::Text, "premise: a column with a word in it is text"); let layers = Layers::default().with(codes); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "codes", "field": "code"})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows".to_string(), note("3 results", "SUM_code, MEAN_code, MAX_code")]); + assert_eq!(said(&res), ["Summary Statistics: 1 rows".to_string(), note("3 results", "SUM_code, MEAN_code, MAX_code")]); } #[test] @@ -706,7 +711,7 @@ fn rounding_check_survives_whole_numbers_past_the_i64_range() { assert_eq!(field_ty(&codes, "code"), FieldType::Text, "premise: a column with a word in it is text"); let layers = Layers::default().with(codes); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "codes", "field": "code"})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows"], "decimals past the i64 range are decimals, not whole numbers"); + assert_eq!(said(&res), ["Summary Statistics: 1 rows"], "decimals past the i64 range are decimals, not whole numbers"); // A whole number written as text in digits past the i64 range is read as a decimal too, past // what the checks take exactly: the tool says so once for the field, and its statistics aren't // checked one by one. One the decimal holds (10^20) is counted as well, as its statistics @@ -717,7 +722,7 @@ fn rounding_check_survives_whole_numbers_past_the_i64_range() { let layers = Layers::default().with(codes); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "codes", "field": "code"})).unwrap(); assert_eq!( - res.messages, + said(&res), [ "Summary Statistics: 1 rows".to_string(), "3 whole numbers written as text in code are past the 64-bit integer range and were read as decimals, so statistics of them may be rounded" @@ -734,7 +739,7 @@ fn rounding_check_survives_whole_numbers_past_the_i64_range() { let layers = Layers::default().with(codes); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "codes", "field": "code"})).unwrap(); assert_eq!( - res.messages, + said(&res), [ "Summary Statistics: 1 rows".to_string(), "1 whole number written as text in code is past the 64-bit integer range and was read as a decimal, so statistics of it may be rounded" @@ -751,19 +756,19 @@ fn rounding_check_survives_whole_numbers_past_the_i64_range() { let statistics = json!([{"field": "code", "statistic": "MAX"}, {"field": "code", "statistic": "FIRST"}]); let p = json!({"input": "codes", "case": "k", "statistics": statistics}); assert_eq!( - rustgis_analysis::run("summaryStatistics", &layers, &p).unwrap().messages, + said(&rustgis_analysis::run("summaryStatistics", &layers, &p).unwrap()), ["Summary Statistics: 3 rows"], "a text field's statistics are text, read as they are" ); let p = json!({"input": "codes", "case": "k", "field": "code"}); let res = rustgis_analysis::run("summaryStatistics", &layers, &p).unwrap(); assert_eq!( - res.messages, + said(&res), [ "Summary Statistics: 3 rows".to_string(), - "1 result rounded in MIN_code: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)".to_string(), "2 whole numbers written as text in code are past the 64-bit integer range and were read as decimals, so statistics of them may be rounded" - .to_string() + .to_string(), + "ChangedRounded: 1 result rounded in MIN_code: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)".to_string() ] ); // One such value. @@ -771,7 +776,7 @@ fn rounding_check_survives_whole_numbers_past_the_i64_range() { let res = rustgis_analysis::run("summaryStatistics", &Layers::default().with(codes), &json!({"input": "codes", "field": "code"})).unwrap(); assert_eq!( - res.messages[1], + said(&res)[1], "1 whole number written as text in code is past the 64-bit integer range and was read as a decimal, so statistics of it may be rounded" ); // An Integer field holding such decimals beside a whole number, through every statistic. @@ -787,7 +792,7 @@ fn rounding_check_survives_whole_numbers_past_the_i64_range() { .collect(); let layers = Layers::default().with(ds); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "huge", "statistics": all})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows"]); + assert_eq!(said(&res), ["Summary Statistics: 1 rows"]); } /// Whole numbers written as text past the i64 range are counted per record: a point in two @@ -826,11 +831,12 @@ fn a_rounded_text_in_overlapping_polygons_is_counted_once() { let layers = Layers::default().with(zones()).with(pts.clone()).select("sites", &[1, 0]); let res = rustgis_analysis::run("countPoints", &layers, &p("sites")).unwrap(); assert_eq!( - res.messages, + said(&res), [ "Summarize Within: 4 points counted".to_string(), - "2 results rounded in MIN_n: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)".to_string(), - one(1) + one(1), + "ChangedRounded: 2 results rounded in MIN_n: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)" + .to_string() ] ); let out = layer(res); @@ -842,12 +848,12 @@ fn a_rounded_text_in_overlapping_polygons_is_counted_once() { // All sites: site 2's HUGE is a second value, and site 3's LOW lies in no polygon and isn't read. let layers = Layers::default().with(zones()).with(pts.clone()); let res = rustgis_analysis::run("countPoints", &layers, &p("sites")).unwrap(); - assert_eq!(res.messages[0], "Summarize Within: 5 points counted"); - assert_eq!(res.messages[2], many(2)); + assert_eq!(said(&res)[0], "Summarize Within: 5 points counted"); + assert_eq!(said(&res)[1], many(2)); assert_eq!(column(&layer(res), "COUNT_n"), [&int(3), &int(2), &int(0), &int(0)]); // Without statistics nothing is read, and nothing said. let res = rustgis_analysis::run("countPoints", &layers, &json!({"polygons": "zones", "points": "sites"})).unwrap(); - assert_eq!(res.messages, ["Summarize Within: 5 points counted"]); + assert_eq!(said(&res), ["Summarize Within: 5 points counted"]); // Dissolve: records 0 and 2 in zone A, 1 and 3 in zone B, with the same values; each is in one // group, and the count is the same by zone and as one group. Zone B's MAX, 2^53 + 1, is the one @@ -867,19 +873,20 @@ fn a_rounded_text_in_overlapping_polygons_is_counted_once() { let layers = Layers::default().with(ds.clone()); let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "parcels", "field": "zone", "statistics": statistics})).unwrap(); assert_eq!( - res.messages, + said(&res), [ "Dissolve: 2 features".to_string(), - "1 result rounded in MAX_n: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)".to_string(), - many(3) + many(3), + "ChangedRounded: 1 result rounded in MAX_n: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)" + .to_string() ] ); let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "parcels", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Dissolve: 1 features".to_string(), many(3)]); + assert_eq!(said(&res), ["Dissolve: 1 features".to_string(), many(3)]); // Only zone A, selected with a repeat: its two values. let layers = Layers::default().with(ds).select("parcels", &[2, 0, 2]); let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "parcels", "field": "zone", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Dissolve: 1 features".to_string(), many(2)]); + assert_eq!(said(&res), ["Dissolve: 1 features".to_string(), many(2)]); } /// A Double field's values are decimals already: statistics of them round as decimal arithmetic @@ -904,7 +911,7 @@ fn decimal_fields_get_no_rounding_notice() { .collect(); let layers = Layers::default().with(ds); let res = rustgis_analysis::run("dissolve", &layers, &json!({"input": "decimals", "field": "zone", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Dissolve: 2 features"]); + assert_eq!(said(&res), ["Dissolve: 2 features"]); let out = layer(res); assert_eq!(column(&out, "SUM_d"), [&float(9_007_199_254_740_996.0), &float(0.5)], "rounded, as decimals are"); assert_eq!(column(&out, "MAX_d"), [&float(BIG), &float(0.5)]); @@ -915,7 +922,7 @@ fn decimal_fields_get_no_rounding_notice() { let layers = Layers::default().with(points).with(area); let p = json!({"polygons": "area", "points": "decimal_points", "statistics": statistics}); let res = rustgis_analysis::run("countPoints", &layers, &p).unwrap(); - assert_eq!(res.messages, ["Summarize Within: 3 points counted"]); + assert_eq!(said(&res), ["Summarize Within: 3 points counted"]); assert_eq!(column(&layer(res), "SUM_d"), [&float(9_007_199_254_740_996.0)]); // CSV: a column of whole numbers with a decimal among them reads as Double as well. let csv = "d\n9007199254740994\n1\n0.5\n"; @@ -924,9 +931,9 @@ fn decimal_fields_get_no_rounding_notice() { assert_eq!(column(&ds, "d"), [&float(BIG), &float(1.0), &float(0.5)], "premise"); let layers = Layers::default().with(ds); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "decimals", "statistics": statistics})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows"]); + assert_eq!(said(&res), ["Summary Statistics: 1 rows"]); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "decimals", "field": "d"})).unwrap(); - assert_eq!(res.messages, ["Summary Statistics: 1 rows"], "through the shortcut too"); + assert_eq!(said(&res), ["Summary Statistics: 1 rows"], "through the shortcut too"); } #[test] @@ -948,15 +955,15 @@ fn whole_numbers_written_as_text_are_counted_and_joined_exactly() { ]); let layers = Layers::default().with(ds); let res = rustgis_analysis::run("summaryStatistics", &layers, &json!({"input": "codes", "statistics": statistics})).unwrap(); - let Output::Table(out) = res.output else { panic!("a table") }; - assert_eq!(column(&out, "UNIQUE_n"), [&int(3)]); + let Output::Table(out) = &res.output else { panic!("a table") }; + assert_eq!(column(out, "UNIQUE_n"), [&int(3)]); // 2^53 + 1 twice is the mode; as decimals all four would be 2^53 or 2^53 + 4. - assert_eq!(column(&out, "MODE_n"), [&float(9_007_199_254_740_992.0)], "2^53 + 1, written as a decimal"); - assert_eq!(column(&out, "MINORITY_n"), [&float(9_007_199_254_740_992.0)], "2^53, the lowest of the rarest"); - assert_eq!(column(&out, "CONCATENATE_n"), [&text("9007199254740993, 9007199254740992, 9007199254740993, 9007199254740995")]); + assert_eq!(column(out, "MODE_n"), [&float(9_007_199_254_740_992.0)], "2^53 + 1, written as a decimal"); + assert_eq!(column(out, "MINORITY_n"), [&float(9_007_199_254_740_992.0)], "2^53, the lowest of the rarest"); + assert_eq!(column(out, "CONCATENATE_n"), [&text("9007199254740993, 9007199254740992, 9007199254740993, 9007199254740995")]); // MODE, 2^53 + 1, comes out rounded, and the tool says so; MINORITY, 2^53, fits. - assert_eq!(res.messages.len(), 2, "{:?}", res.messages); - assert!(res.messages[1].starts_with("1 result rounded in MODE_n:"), "{:?}", res.messages); + assert_eq!(said(&res).len(), 2, "{:?}", said(&res)); + assert!(said(&res)[1].starts_with("ChangedRounded: 1 result rounded in MODE_n:"), "{:?}", said(&res)); } /// Pivot Table's statistics, rounded from whole numbers past 2^53, are reported as Summary @@ -966,7 +973,7 @@ fn whole_numbers_written_as_text_are_counted_and_joined_exactly() { fn pivot_table_reports_rounded_cells() { const BIG: i64 = (1 << 53) + 1; let note = |n: &str, fields: &str| { - format!("{n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)") + format!("ChangedRounded: {n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)") }; let table = |res: ToolResult| match res.output { Output::Table(d) => d, @@ -979,15 +986,15 @@ fn pivot_table_reports_rounded_cells() { // Zone A's SUM of n, 2^53 + 1 + 2, comes out 2^53 + 2 (2^53, then 2 added); zone B's, 3 + 4, is // exact. let res = by_zone("n", "SUM"); - assert_eq!(res.messages, [clean[0].to_string(), note("1 result", "A")]); + assert_eq!(said(&res), [clean[0].to_string(), note("1 result", "A")]); let out = table(res); assert_eq!((column(&out, "A"), column(&out, "B")), (vec![&float(9_007_199_254_740_994.0)], vec![&float(7.0)])); - assert_eq!(by_zone("n", "MAX").messages, [clean[0].to_string(), note("1 result", "A")]); + assert_eq!(said(&by_zone("n", "MAX")), [clean[0].to_string(), note("1 result", "A")]); // Zone B's SUM of m, 1 + 2^53, is rounded; its MAX, 2^53, fits. - assert_eq!(by_zone("m", "SUM").messages, [clean[0].to_string(), note("1 result", "B")]); + assert_eq!(said(&by_zone("m", "SUM")), [clean[0].to_string(), note("1 result", "B")]); // Statistics within 2^53, and CONCATENATE, which writes whole numbers as they are: no notice. for (value, stat) in [("m", "MAX"), ("small", "SUM"), ("small", "MEAN"), ("n", "MIN"), ("n", "COUNT"), ("n", "CONCATENATE")] { - assert_eq!(by_zone(value, stat).messages, clean, "{value} {stat}"); + assert_eq!(said(&by_zone(value, stat)), clean, "{value} {stat}"); } // Rounded cells counted in each pivot field, over every row, and named in field order. @@ -1003,10 +1010,10 @@ fn pivot_table_reports_rounded_cells() { let layers = Layers::default().with(ds.clone()); let pivot = |p: Json| rustgis_analysis::run("pivotTable", &layers, &p).unwrap_or_else(|e| panic!("{p}: {e}")); let res = pivot(json!({"input": "years", "fields": "r", "pivot": "y", "value": "v", "statistic": "SUM"})); - assert_eq!(res.messages, ["Pivot Table: 2 rows, 3 pivot fields".to_string(), note("3 results", "y2020, y2021")]); + assert_eq!(said(&res), ["Pivot Table: 2 rows, 3 pivot fields".to_string(), note("3 results", "y2020, y2021")]); // Without a statistic each value is placed as it is, an Integer, exactly. let res = pivot(json!({"input": "years", "fields": "r", "pivot": "y", "value": "v"})); - assert_eq!(res.messages, ["Pivot Table: 2 rows, 3 pivot fields"]); + assert_eq!(said(&res), ["Pivot Table: 2 rows, 3 pivot fields"]); let out = table(res); assert_eq!(field_ty(&out, "y2020"), FieldType::Integer); assert_eq!(column(&out, "y2020"), [&int(BIG), &int(BIG)]); @@ -1020,15 +1027,15 @@ fn pivot_table_reports_rounded_cells() { let pivot = |p: Json| rustgis_analysis::run("pivotTable", &layers, &p).unwrap_or_else(|e| panic!("{p}: {e}")); let res = pivot(json!({"input": "years", "fields": "r", "pivot": "y", "value": "v", "statistic": "MIN"})); assert_eq!( - res.messages, + said(&res), [ "Pivot Table: 2 rows, 3 pivot fields".to_string(), - note("3 results", "y2020, y2021"), "1 whole number written as text in v is past the 64-bit integer range and was read as a decimal, so statistics of it may be rounded" - .to_string() + .to_string(), + note("3 results", "y2020, y2021") ] ); let res = pivot(json!({"input": "years", "fields": "r", "pivot": "y", "value": "v"})); - assert_eq!(res.messages, ["Pivot Table: 2 rows, 3 pivot fields"]); + assert_eq!(said(&res), ["Pivot Table: 2 rows, 3 pivot fields"]); assert_eq!(column(&table(res), "y2022"), [&text("123456789012345678901"), &Value::Null]); } diff --git a/crates/data/src/loss.rs b/crates/data/src/loss.rs index a781eb61e2..d77a2a97a3 100644 --- a/crates/data/src/loss.rs +++ b/crates/data/src/loss.rs @@ -118,7 +118,7 @@ loss_codes! { ChangedRounded => ( "changed.rounded", Changed, - "Keep the source file where finer values matter, or write to a format that holds them (times finer than the format keeps, or numbers past a dBase field's precision)." + "Keep the source file where finer values matter, or write to a format that holds them (times finer than the format keeps, numbers past a dBase field's precision, or whole numbers past 2^53 in a decimal field)." ), ChangedPosition => ( "changed.position", diff --git a/crates/engine/src/cmd/share.rs b/crates/engine/src/cmd/share.rs index 4d0a400a9d..23e32916bb 100644 --- a/crates/engine/src/cmd/share.rs +++ b/crates/engine/src/cmd/share.rs @@ -902,7 +902,7 @@ pub(crate) fn query_export_note( /// Reports numeric loss after the file has been written, keeping the output path literal in /// translated interface text. A clean export emits no loss notice. -pub(crate) fn notify_null_numbers(s: &mut Session, path: &str, count: usize) { +pub fn notify_null_numbers(s: &mut Session, path: &str, count: usize) { if let Some(note) = null_numbers_note(path, count) { let mark = s.notice_mark(); // Each such value is lost from the file. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 55c5770cb1..1599245eda 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -5631,6 +5631,8 @@ impl RustGisApp { dbf_notes.extend(rustgis_engine::cmd::share::read_notes(&name, &l.name, &lost)); // CSV holds a point a row, GPX a name for each feature and no areas; reading text back, CSV reads blanks as null and KML and GPX trim it. dbf_notes.extend(io::export_losses_notes(&ds, &ext, io::name_field(&ds), &name).map_err(|e| e.to_string())?); + // NaN and infinities, which GeoJSON can't hold, written as null. + let null_numbers = rustgis_engine::data::export::numbers_written_as_null(&ds.features, &ds.fields, &ext); drop(ds); // Said only once the download is taken, as the other notes are. dbf_notes.extend(io::left_out_note(&name, &ext, left_out)); @@ -5642,6 +5644,7 @@ impl RustGisApp { } if self.download(&name, bytes) { rustgis_engine::cmd::share::notify_export_query(&mut self.session, &name, &layer_name, &query_report); + rustgis_engine::cmd::share::notify_null_numbers(&mut self.session, &name, null_numbers); for note in notes .into_iter() .chain(dbf_notes) @@ -9812,6 +9815,33 @@ mod download_service_tests { } } + /// The web download says the numbers GeoJSON can't hold, written as null, with their code, once + /// the browser takes the file; a refused download says nothing of them. + #[test] + fn feature_download_says_numbers_written_as_null() { + use rustgis_engine::data::{Dataset, Field, FieldType, Geometry, GeometryType, Value}; + use rustgis_engine::geom::Coord; + // A NaN accepted, a NaN refused, and finite numbers only (nothing to say). + for (accept, first) in [(true, f64::NAN), (false, f64::NAN), (true, 2.5)] { + let download: Box) -> Result<(), String>> = + if accept { Box::new(|_, _| Ok(())) } else { Box::new(|_, _| Err("browser rejected download".into())) }; + let mut app = RustGisApp::with_sample().with_services(Services { download: Some(download), ..Default::default() }); + let mut ds = Dataset::new("Gauges", GeometryType::Point, vec![Field::new("level", FieldType::Double)]); + ds.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![Value::Float(first)]); + ds.push(Some(Geometry::Point(Coord::new(3.0, 4.0))), vec![Value::Float(1.5)]); + let id = app.session.map_mut().add_dataset(ds); + let mark = app.session.notice_mark(); + let run = app.run("ui.exportLayer", json!({"layer": id, "format": "geojson"})); + if accept { + run.unwrap(); + } + let said: Vec<_> = + app.session.notices_since(mark).iter().filter(|n| n.text.contains("written as null")).map(|n| n.loss()).collect(); + let expected = if accept && first.is_nan() { vec![Some(rustgis_engine::data::loss::LossCode::DroppedValue)] } else { vec![] }; + assert_eq!(said, expected, "accepted: {accept}, first: {first}"); + } + } + /// The web app's Shapefile download says which field names it changed, once the browser takes /// the file; names that fit, other formats and a refused download say nothing of them. #[test] diff --git a/docs/cli.md b/docs/cli.md index f9e48c7cba..13f1edf69f 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -172,7 +172,7 @@ An `--allow` that names no kind or code is refused with status 2, as is `--allow | `changed.cut` | Shorten the text to what the format holds (254 bytes for a Shapefile; nothing past a NUL character), or write to a format without the limit, such as GeoPackage. | | `changed.body` | Export in the layer's own coordinate system, which keeps its body. | | `changed.encoding` | Put a .cpg file naming the file's own encoding beside it. | -| `changed.rounded` | Keep the source file where finer values matter, or write to a format that holds them (times finer than the format keeps, or numbers past a dBase field's precision). | +| `changed.rounded` | Keep the source file where finer values matter, or write to a format that holds them (times finer than the format keeps, numbers past a dBase field's precision, or whole numbers past 2^53 in a decimal field). | | `changed.position` | Export in a coordinate system that holds the positions, such as WGS 84. | | `approximated.datum` | Export in the layer's own coordinate system, or give that system with a known transformation to WGS 84. | | `approximated.curve` | Keep the source file where exact curves matter: RustGIS draws curves as straight segments. |