diff --git a/crates/analysis/src/tools.rs b/crates/analysis/src/tools.rs index b51b34681e..54b806a507 100644 --- a/crates/analysis/src/tools.rs +++ b/crates/analysis/src/tools.rs @@ -550,7 +550,7 @@ pub static TOOLS: &[ToolSpec] = &[ label: "Coordinate System", kind: K::Text, required: false, - default: "\"EPSG:4326\"", + default: "\"\"", help: "The coordinates' system, as EPSG: or WKT; longitude/latitude (EPSG:4326) when empty.", }, P { name: "output", label: "Output Name", kind: K::Text, required: false, default: "", help: "" }, @@ -4690,6 +4690,8 @@ fn crs_with_compatibility_note(text: &str) -> Result<(Crs, Option), Stri Ok((crs, rustgis_data::raster::wkt::typed_crs_note(text, crs))) } +/// Places points from X/Y fields in the requested system, or longitude/latitude when empty. +/// The input layer's geometry system does not determine these coordinates. fn xy_table_to_point(inp: &dyn Inputs, p: &Json) -> Result { let ds = input(inp, p, "input")?; let field = |k: &str| { @@ -4697,7 +4699,11 @@ fn xy_table_to_point(inp: &dyn Inputs, p: &Json) -> Result { find_field_case_insensitive(ds, name) }; let (xi, yi) = (field("x")?, field("y")?); - let (crs, crs_note) = match s(p, "crs").trim() { + if p.get("crs").is_some_and(|v| !v.is_null() && !v.is_string()) { + return Err("crs must be text".into()); + } + let crs_text = s(p, "crs").trim(); + let (crs, crs_note) = match crs_text { "" => (Crs::LonLat, None), text => crs_with_compatibility_note(text)?, }; @@ -4738,6 +4744,9 @@ fn xy_table_to_point(inp: &dyn Inputs, p: &Json) -> Result { }); } let mut messages = vec![format!("XY Table To Point: {placed} points")]; + if crs_text.is_empty() { + messages.push("XY Table To Point: WGS 84 (EPSG:4326) was assumed because no coordinate system was given".into()); + } let mut losses: Vec = Vec::new(); messages.extend(crs_note.map(|note| format!("XY Table To Point: its coordinate system: {note}"))); // A row without numbers had no place to lose; one whose numbers have no place in the system @@ -13965,11 +13974,14 @@ mod tests { let m = Map(HashMap::from([("stops".to_string(), stops), ("metres".to_string(), metres)])); let run = |p: serde_json::Value| crate::run("xyTableToPoint", &m, &p); + assert_eq!(run(serde_json::json!({"input": "stops", "x": "lon", "y": "lat", "crs": 3857})).unwrap_err(), "crs must be text"); + let r = run(serde_json::json!({"input": "stops", "x": "lon", "y": "lat"})).unwrap(); // Rows without numbers lose nothing; the one off the globe loses its place. assert_eq!( said(&r)[1..], [ + "XY Table To Point: WGS 84 (EPSG:4326) was assumed because no coordinate system was given", "3 of 7 rows have no numbers in both fields; they are in the table without a point", "DroppedGeometry: 1 of 7 rows have coordinates with no place in EPSG:4326; they are in the table without a point" ] @@ -14011,8 +14023,12 @@ mod tests { assert!(said(&r).iter().any(|m| m.starts_with("DroppedGeometry: 1 of 2 rows")), "{:?}", said(&r)); assert!(layer(r).features[1].geometry.is_none()); // Points in longitude/latitude are held there, with no system of their own, as before. - let lonlat = layer(run(serde_json::json!({"input": "stops", "x": "lon", "y": "lat", "crs": "EPSG:4326"})).unwrap()); - assert_eq!((lonlat.coord_crs(), lonlat.crs), (Crs::LonLat, None)); + for (crs, assumed) in [("", true), (" ", true), ("EPSG:4326", false)] { + let r = run(serde_json::json!({"input": "stops", "x": "lon", "y": "lat", "crs": crs})).unwrap(); + assert_eq!(r.messages.iter().filter(|m| m.contains("WGS 84 (EPSG:4326) was assumed")).count(), usize::from(assumed), "{crs:?}"); + let lonlat = layer(r); + assert_eq!((lonlat.coord_crs(), lonlat.crs), (Crs::LonLat, None)); + } // Metres read as longitude/latitude, fields without numbers, and junk parameters. let e = run(serde_json::json!({"input": "metres", "x": "e", "y": "n"})).unwrap_err();