Repository navigation
A project coordinate system: new maps and layers start in it (#2032 C3a part 1, C3) #2667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ pub fn commands() -> Vec<C> { | |
| C::new("project.save", "Save Project", "Project", save).inputs(save_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).key("Mod+S").view().params(r#"{"path"?: string}"#), | ||
| C::new("project.saveAs", "Save Project As", "Project", save_as).inputs(save_as_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).key("Mod+Shift+S").view().params(r#"{"path": string}"#), | ||
| C::new("project.rename", "Rename Project", "Project", rename).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"name": string}"#), | ||
| C::new("project.coordinateSystem", "Project Coordinate System", "Project", coordinate_system).roles(&[]).effects(crate::policy::Effects::NONE) | ||
| .params(r#"{"crs"?: the system new maps are drawn in and new empty layers are held in: EPSG:<code>, ESRI:<code>, WKT or a PROJ string ("" or null: none, as a project starts; omitted: kept), "fromFirstLayer"?: bool (while the project has none, the first layer added with a coordinate system gives it; omitted: kept)}"#), | ||
| C::new("edit.undo", "Undo", "Quick Access Toolbar", |s, _| if s.undo() { Ok(Value::Null) } else { Err("nothing to undo".into()) }).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::History)) | ||
| .key("Mod+Z") | ||
| .view() | ||
|
|
@@ -822,6 +824,155 @@ fn rename(s: &mut Session, p: &Value) -> CmdResult { | |
| Ok(Value::Null) | ||
| } | ||
|
|
||
| /// Sets the project's coordinate system and whether the first layer added gives it one. Maps | ||
| /// and layers already in the project keep their own systems: it is what new ones start in. | ||
| fn coordinate_system(s: &mut Session, p: &Value) -> CmdResult { | ||
| let given = p.get("crs"); | ||
| let from_first = p.get("fromFirstLayer").filter(|v| !v.is_null()); | ||
| if given.is_none() && from_first.is_none() { | ||
| return Err("give crs, fromFirstLayer or both".into()); | ||
| } | ||
| let crs = match given { | ||
| None => s.project.coordinate_system, | ||
| Some(Value::Null) => None, | ||
| Some(Value::String(t)) if t.trim().is_empty() => None, | ||
| Some(Value::String(t)) => { | ||
| Some(rustgis_data::raster::Crs::parse(super::within("crs", "a coordinate system", t, 64 * 1024)?.as_str())?) | ||
| } | ||
| Some(_) => return Err("crs is a coordinate system's code, WKT or PROJ string, or null".into()), | ||
| }; | ||
| let from_first = match from_first { | ||
| None => s.project.crs_from_first_layer, | ||
| Some(v) => v.as_bool().ok_or("fromFirstLayer is true or false")?, | ||
| }; | ||
| s.project.coordinate_system = crs; | ||
| s.project.crs_from_first_layer = from_first; | ||
| // A system chosen, or the option off: rasters waiting for their pixels were added before it, | ||
| // and give none later (cleared again, or the option on again). | ||
| if crs.is_some() || !from_first { | ||
| s.project.first_layer.settle(); | ||
| } | ||
| Ok(project_crs_json(&s.project)) | ||
| } | ||
|
|
||
| /// Text `crs` reads back as the same system: an EPSG or ESRI code, else a custom system's WKT when | ||
| /// it was given one (its PROJ definition alone can leave out what places points, such as angles in | ||
| /// grads), else its PROJ definition. | ||
| pub fn crs_text(crs: rustgis_data::raster::Crs) -> String { | ||
| let label = crs.label(); | ||
| if label.starts_with("ESRI:") { label } else { crs.definition_text() } | ||
| } | ||
|
|
||
| /// The project's coordinate system as commands give it: text that `crs` reads back as the same | ||
| /// system ([`crs_text`]), its name, whether a map can be drawn in it, and whether the first layer | ||
| /// added gives it one. | ||
| pub(crate) fn project_crs_json(project: &crate::project::Project) -> Value { | ||
| let crs = project.coordinate_system; | ||
| json!({ | ||
| "crs": crs.map(crs_text), | ||
| "label": crs.map(|c| c.label()), | ||
| "mapsCanUse": crs.map(|c| super::navigate::map_projection_for(c).is_ok()), | ||
| "fromFirstLayer": project.crs_from_first_layer, | ||
| }) | ||
| } | ||
|
|
||
| /// Commands that add data from outside the project without being under Home › Add. | ||
| const ADDS_DATA: &[&str] = &["map.import", "layer.paste", "offline.open"]; | ||
|
|
||
| /// The layers in the project before a step that adds data (a command of Home › Add: a file, a | ||
| /// URL, a service, a database; or [`ADDS_DATA`]), when the project is to take its coordinate | ||
| /// system from the first layer added ([`Project::crs_from_first_layer`]) and has none yet; else | ||
| /// `None`, and nothing is looked at. A tool's output isn't a layer added: a raster made from one | ||
| /// whose system was assumed would name WGS 84 as if it had been given. Undo and Redo change | ||
| /// nothing (they are views of history), and a project opened or started brings its own setting. | ||
| /// A raster added with its pixels still to come (read in place, or from a server) says no system | ||
| /// until they land: [`take_first_layer_system`] keeps it waiting, and any later step that changes | ||
| /// the project, not only one adding data, looks at those waiting. | ||
| /// | ||
| /// [`Project::crs_from_first_layer`]: crate::project::Project::crs_from_first_layer | ||
| pub(crate) fn first_layer_watch(s: &Session, spec: &crate::CommandSpec) -> Option<FirstLayerWatch> { | ||
| let p = &s.project; | ||
| if !spec.mutates || !p.crs_from_first_layer || p.coordinate_system.is_some() { | ||
| return None; | ||
| } | ||
| let adds_data = spec.location.starts_with("Home › Add") || ADDS_DATA.contains(&spec.id); | ||
| if !adds_data && p.first_layer.pending.is_empty() { | ||
| return None; | ||
| } | ||
| let waiting = &p.first_layer.pending; | ||
| let before = | ||
| p.maps.iter().flat_map(|m| m.layers.iter()).map(rustgis_map::Layer::instance_id).filter(|id| !waiting.contains(id)).collect(); | ||
| Some(FirstLayerWatch { opened: s.opened, before, adds_data }) | ||
| } | ||
|
|
||
| /// What [`first_layer_watch`] saw before a step, for [`take_first_layer_system`] after it. | ||
| pub(crate) struct FirstLayerWatch { | ||
| opened: u64, | ||
| before: std::collections::HashSet<u64>, | ||
| /// The step adds data: the layers it added are looked at, besides the rasters waiting. | ||
| adds_data: bool, | ||
| } | ||
|
|
||
| /// The coordinate system a layer says it is in: a raster's, or the one a feature layer was read | ||
| /// in. `None` for a layer whose file named none: a feature layer taken for longitude/latitude, a | ||
| /// raster read as WGS 84 because its coordinates look like degrees, a scan not yet placed. | ||
| pub fn layer_system(l: &rustgis_map::Layer) -> Option<rustgis_data::raster::Crs> { | ||
| match &l.raster { | ||
| Some(r) => r.data.as_ref().filter(|d| d.georeferenced && !d.crs_assumed).map(|d| d.crs), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A raster derived from an assumed-WGS 84 raster names WGS 84 as if the file had. Suggested fix: in Category: bug
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 45c1eae, differently from the suggestion: tools build rasters in many places and their outputs often come back as GeoTIFF bytes that name EPSG:4326, so the flag wouldn't survive. Instead only steps that add data (Home › Add, Import Map, Paste Layers, Open Offline Map) look at new layers; a tool's output gives no system. Docs say so. Test: |
||
| None => l.dataset.crs, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A feature layer that names WGS 84 never gives the project its system, against the docs. For features, The docs in this PR say the opposite. Failure scenario: option on, no project system. Add Fix either side. Make the feature path keep a named WGS 84 (for example, carry "named LonLat" separately from "no system"), or correct the docs and the "takes it from the first layer" wording to say features naming WGS 84 give none. Add a test next to Category: bug |
||
| } | ||
| } | ||
|
|
||
| /// After a step: the project takes the coordinate system of the first layer it added that has | ||
| /// one, in the maps' order, and says so ([`first_layer_watch`]). Each layer is looked at once, by | ||
| /// the innermost step that added it, so a step around it (a script and its lines) doesn't take it | ||
| /// again; rasters whose pixels are still to come wait. What was looked at is kept with the project | ||
| /// ([`crate::project::FirstLayerSeen`]), so Undo and a rolled-back command restore it. | ||
| pub(crate) fn take_first_layer_system(s: &mut Session, FirstLayerWatch { opened, before, adds_data }: &FirstLayerWatch) { | ||
| if s.opened != *opened || !s.project.crs_from_first_layer || s.project.coordinate_system.is_some() { | ||
| return; | ||
| } | ||
| let p = &mut s.project; | ||
| let (mut found, mut waiting) = (None, Vec::new()); | ||
| for l in p.maps.iter().flat_map(|m| m.layers.iter()) { | ||
| let id = l.instance_id(); | ||
| let candidate = if *adds_data { !before.contains(&id) } else { p.first_layer.pending.contains(&id) }; | ||
| if !candidate || p.first_layer.considered.contains(&id) { | ||
| continue; | ||
| } | ||
| if l.raster.as_ref().is_some_and(|r| r.data.is_none()) { | ||
| waiting.push(id); | ||
| continue; | ||
| } | ||
| p.first_layer.considered.insert(id); | ||
| if found.is_none() { | ||
| found = layer_system(l).map(|crs| (crs, l.name.clone())); | ||
| } | ||
| } | ||
| // Waiting: those still to come, and only layers still in the project (Undo restores the list | ||
| // with a layer it brings back). | ||
| let present: std::collections::HashSet<u64> = | ||
| p.maps.iter().flat_map(|m| m.layers.iter()).map(rustgis_map::Layer::instance_id).collect(); | ||
| let considered = &p.first_layer.considered; | ||
| p.first_layer.pending.retain(|id| present.contains(id) && !considered.contains(id)); | ||
| for id in waiting { | ||
| if !p.first_layer.pending.contains(&id) { | ||
| p.first_layer.pending.push(id); | ||
| } | ||
| } | ||
| if let Some((crs, name)) = found { | ||
| p.coordinate_system = Some(crs); | ||
| p.first_layer.settle(); | ||
| s.notify( | ||
| format!( | ||
| "The project's coordinate system is now {}, from layer '{name}': new maps are drawn in it and new layers held in it", | ||
| crs.label() | ||
| ), | ||
| false, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Picks the map tool: a Create tool needs a layer of its kind selected, and the tools that | ||
| /// change features (Edit Vertices, Cut Polygon, Reshape) need editing on. | ||
| fn set_tool(s: &mut Session, p: &Value) -> CmdResult { | ||
|
|
@@ -953,6 +1104,7 @@ fn info(s: &mut Session, p: &Value) -> CmdResult { | |
| Ok(json!({ | ||
| "name": s.project.name, | ||
| "path": s.path.as_ref().map(|p| p.display().to_string()), | ||
| "coordinateSystem": project_crs_json(&s.project), | ||
| "unsaved": s.dirty(), | ||
| "maps": s.project.maps.iter().map(|m| m.name.clone()).collect::<Vec<_>>(), | ||
| "activeMap": m.name, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A raster that names no coordinate system can give the project WGS 84.
layer_systemreturns the raster'scrs, but a reader that finds no georeferencing assumes WGS 84 and still storesCrs::LonLat(e.g.crates/data/src/raster/ascii.rs:98-100, and the GDAL path withassumed_lonlatingdal.rs). The assumption is only recorded inr.lostas a note. So with "take it from the first layer" on, adding an ASCII grid with no.prjsets the project to EPSG:4326 and the notice intake_first_layer_systemreads as if the layer named it. The docs in this PR say "a layer that names no system … gives none", so this contradicts them.Suggested fix: carry an "assumed" flag on the raster data (or check for the assumed note) and return
Nonefromlayer_systemwhen the system was assumed. Add a test next toa_layer_without_a_system_of_its_own_gives_nonewith an ASCII grid that has no.prjinside lon/lat bounds: the project staysNone, and the clean case with a.prjstill gives the system. Please also check unreferenced rasters (crates/data/src/raster/mod.rs:1175area) for the same default.Confidence: high for the ASCII path, medium for the other readers.
Category: bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in dd5dba8: rasters record when their system was assumed (
Raster::crs_assumed, set by the ASCII, GeoTIFF, GDAL-converted, JPEG 2000 and Zarr readers), andlayer_systemgives none for those and for unplaced scans. Test:a_raster_read_as_wgs84_by_assumption_gives_none_and_one_that_names_its_system_gives_it(an ASCII grid without a.prjgives none; with an LV95.prjit gives LV95).