diff --git a/crates/data/src/interop.rs b/crates/data/src/interop.rs index 7cce4e92a0..86cbcce8d3 100644 --- a/crates/data/src/interop.rs +++ b/crates/data/src/interop.rs @@ -65,13 +65,13 @@ pub fn to_kml(ds: &Dataset, name_field: Option) -> crate::Result for (r, f) in ds.features.iter().enumerate() { crate::progress::step(1)?; let Some(g) = &f.geometry else { continue }; - let name = name_field.map(|fi| ds.value(r, fi).to_string()).unwrap_or_else(|| f.id.to_string()); + let name = name_field.map(|fi| ds.value(r, fi).to_file_text()).unwrap_or_else(|| f.id.to_string()); let data: String = ds .fields .iter() .zip(&f.attrs) .filter(|(_, v)| !v.is_null()) - .map(|(fd, v)| format!("{}", xml(&fd.name), xml(&v.to_string()))) + .map(|(fd, v)| format!("{}", xml(&fd.name), xml(&v.to_file_text()))) .collect(); out.push_str(&format!( "{}{data}{}\n", @@ -94,7 +94,7 @@ pub fn to_gpx(ds: &Dataset, name_field: Option) -> crate::Result for (r, f) in ds.features.iter().enumerate() { crate::progress::step(1)?; let Some(g) = &f.geometry else { continue }; - let name = xml(&name_field.map(|fi| ds.value(r, fi).to_string()).unwrap_or_else(|| f.id.to_string())); + let name = xml(&name_field.map(|fi| ds.value(r, fi).to_file_text()).unwrap_or_else(|| f.id.to_string())); match g { Geometry::Point(c) => out.push_str(&format!("{name}\n", pt(c))), Geometry::MultiPoint(v) => v.iter().for_each(|c| out.push_str(&format!("{name}\n", pt(c)))), @@ -482,7 +482,7 @@ pub fn to_wkt_csv(ds: &Dataset) -> String { out.push_str(&q(&f.geometry.as_ref().map(wkt).unwrap_or_default())); for v in &f.attrs { out.push(','); - out.push_str(&q(&if v.is_null() { String::new() } else { v.to_string() })); + out.push_str(&q(&v.to_file_text())); } out.push('\n'); } @@ -526,4 +526,26 @@ mod tests { assert_eq!(wkt(&Geometry::Line(vec![vec![Coord::new(0.0, 0.0), Coord::new(1.0, 1.0)]])), "LINESTRING (0 0, 1 1)"); assert!(to_wkt_csv(&ds).starts_with("wkt,name\nPOINT (2.35 48.85),Paris & co")); } + + /// Numbers are written in full and a null name as nothing, not as they're shown (rounded to + /// 6 decimals, ``). + #[test] + fn kml_gpx_and_wkt_csv_write_values_as_stored() { + let fields = vec![Field::new("name", FieldType::Text), Field::new("v", FieldType::Double)]; + let mut ds = Dataset::new("t", GeometryType::Point, fields); + ds.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![Value::Null, Value::Float(0.1234567891)]); + ds.push(Some(Geometry::Point(Coord::new(3.0, 4.0))), vec![Value::Text("B".into()), Value::Float(1e-9)]); + let k = to_kml(&ds, Some(0)).unwrap(); + assert!(k.contains("0.1234567891") && k.contains("0.000000001"), "{k}"); + assert!(k.contains("") && !k.contains("Null"), "{k}"); + let g = to_gpx(&ds, Some(0)).unwrap(); + assert!(g.contains("") && g.contains("B") && !g.contains("Null"), "{g}"); + // Named by a number: in full too. + let mut numbered = Dataset::new("n", GeometryType::Point, vec![Field::new("id", FieldType::Double)]); + numbered.push(Some(Geometry::Point(Coord::new(1.0, 2.0))), vec![Value::Float(0.1234567891)]); + assert!(to_kml(&numbered, Some(0)).unwrap().contains("0.1234567891")); + assert!(to_gpx(&numbered, Some(0)).unwrap().contains("0.1234567891")); + let c = to_wkt_csv(&ds); + assert!(c.contains("POINT (1 2),,0.1234567891\n") && c.contains(",B,0.000000001\n"), "{c}"); + } } diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index c6c1e78514..36d61680bc 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -1359,7 +1359,8 @@ pub(crate) fn coerce(v: Value, ty: FieldType) -> Value { (_, Value::Null) => Value::Null, (FieldType::Double, Value::Int(i)) => Value::Float(i as f64), (FieldType::Text, v @ Value::Text(_)) => v, - (FieldType::Text, v) => Value::Text(v.to_string()), + // A number in full, as a file writes it: `Display` rounds to 6 decimals. + (FieldType::Text, v) => Value::Text(v.to_file_text()), (_, v) => v, } } @@ -1862,11 +1863,8 @@ const FORMULA_START: [char; 6] = ['=', '+', '-', '@', '\t', '\r']; /// A cell's text; text that a spreadsheet would run as a formula is prefixed with `'`. 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(), + other => other.to_file_text(), } } @@ -3932,6 +3930,18 @@ mod tests { assert!(read_bytes("t.csv", b"a,b\n\x00\xff\x01,2\n").is_err()); } + /// A GeoJSON column of text and numbers is read as text, its numbers in full rather than + /// rounded as they're shown. + #[test] + fn a_mixed_geojson_column_keeps_its_numbers_in_full() { + let text = r#"{"type":"FeatureCollection","features":[ + {"type":"Feature","properties":{"v":"a"},"geometry":{"type":"Point","coordinates":[1,2]}}, + {"type":"Feature","properties":{"v":0.1234567891},"geometry":{"type":"Point","coordinates":[3,4]}}]}"#; + let (ds, _) = read_geojson(text, "t").unwrap(); + assert_eq!(ds.fields[0].ty, FieldType::Text); + assert_eq!(ds.value(1, 0), &Value::Text("0.1234567891".into())); + } + #[test] fn csv_points() { let (ds, rep) = read_csv("name,Lat,Lon,n\nA,10,20,1\nB,bad,1,2\n", "t").unwrap(); diff --git a/crates/data/src/value.rs b/crates/data/src/value.rs index 5ae55b0cf9..4e734a8760 100644 --- a/crates/data/src/value.rs +++ b/crates/data/src/value.rs @@ -101,6 +101,16 @@ impl Value { pub fn is_null(&self) -> bool { matches!(self, Value::Null) } + /// The value as a file writes it in text: a number in full, as the shortest text that reads + /// back as the same number, and nothing for null. [`Display`](std::fmt::Display) is for + /// showing a value, and rounds numbers to 6 decimals. + pub fn to_file_text(&self) -> String { + match self { + Value::Null => String::new(), + Value::Float(f) => f.to_string(), + other => other.to_string(), + } + } /// Converts a JSON value to an attribute value. pub fn from_json(v: &serde_json::Value) -> Value { match v {