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
30 changes: 26 additions & 4 deletions crates/data/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ pub fn to_kml(ds: &Dataset, name_field: Option<usize>) -> crate::Result<String>
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!("<Data name=\"{}\"><value>{}</value></Data>", xml(&fd.name), xml(&v.to_string())))
.map(|(fd, v)| format!("<Data name=\"{}\"><value>{}</value></Data>", xml(&fd.name), xml(&v.to_file_text())))
.collect();
out.push_str(&format!(
"<Placemark><name>{}</name><ExtendedData>{data}</ExtendedData>{}</Placemark>\n",
Expand All @@ -94,7 +94,7 @@ pub fn to_gpx(ds: &Dataset, name_field: Option<usize>) -> crate::Result<String>
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!("<wpt {}><name>{name}</name></wpt>\n", pt(c))),
Geometry::MultiPoint(v) => v.iter().for_each(|c| out.push_str(&format!("<wpt {}><name>{name}</name></wpt>\n", pt(c)))),
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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, `<Null>`).
#[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)]);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let k = to_kml(&ds, Some(0)).unwrap();
assert!(k.contains("<value>0.1234567891</value>") && k.contains("<value>0.000000001</value>"), "{k}");
assert!(k.contains("<Placemark><name></name>") && !k.contains("Null"), "{k}");
let g = to_gpx(&ds, Some(0)).unwrap();
assert!(g.contains("<name></name>") && g.contains("<name>B</name>") && !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("<name>0.1234567891</name>"));
assert!(to_gpx(&numbered, Some(0)).unwrap().contains("<name>0.1234567891</name>"));
let c = to_wkt_csv(&ds);
assert!(c.contains("POINT (1 2),,0.1234567891\n") && c.contains(",B,0.000000001\n"), "{c}");
}
}
20 changes: 15 additions & 5 deletions crates/data/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions crates/data/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading