Skip to content
Merged
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
24 changes: 20 additions & 4 deletions crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:<code> or WKT; longitude/latitude (EPSG:4326) when empty.",
},
P { name: "output", label: "Output Name", kind: K::Text, required: false, default: "", help: "" },
Expand Down Expand Up @@ -4690,14 +4690,20 @@ fn crs_with_compatibility_note(text: &str) -> Result<(Crs, Option<String>), 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<ToolResult, String> {
let ds = input(inp, p, "input")?;
let field = |k: &str| {
let name = s(p, k).trim();
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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A numeric crs (for example "crs": 3857) is read by s() as empty, so it is silently ignored and the points are placed as longitude/latitude. The new notice then says "no coordinate system was given", which is wrong for this input: one was given and dropped. raster_crs_of already refuses this case with "crs must be text"; the same check belongs here, before crs_text is read. A regression case would go in the junk-parameter list of places_rows_by_their_coordinates, e.g. {"input": "stops", "x": "lon", "y": "lat", "crs": 3857} expected to be an error.

Suggested change
let crs_text = 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();

Category: bug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9962be862f. XY Table To Point now rejects a non-null, non-string crs with "crs must be text", matching the existing raster guard. The regression using numeric 3857 failed before the guard and passes with it. Missing/null/blank values retain the default path; explicit text coordinate systems are unchanged. The named regression, formatting, test hygiene, normal hooks and fork CI passed.

let (crs, crs_note) = match crs_text {
"" => (Crs::LonLat, None),
text => crs_with_compatibility_note(text)?,
};
Expand Down Expand Up @@ -4738,6 +4744,9 @@ fn xy_table_to_point(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String> {
});
}
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<rustgis_data::loss::LossMessage> = 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
Expand Down Expand Up @@ -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"
]
Expand Down Expand Up @@ -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();
Expand Down
Loading