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
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,10 @@ pub fn commands() -> Vec<C> {
}).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
.params(r#"{"layout"?, "element": id}"#),
C::new("layout.frameZoom", "Zoom Frame", "Publish › Map Frame", frame_zoom).inputs(frame_zoom_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(
r#"{"layout"?, "element"?: frame id, "frame"?: (as element), "to"?: "view" (the map view's extent, bearing and pitch; the default)|"full"|"selection"|"layer", "layer"?}"#,
r#"{"layout"?, "element"?: frame id, "frame"?: (as element), "to"?: "view" (the map view's extent, bearing and pitch; the default)|"full"|"selection"|"layer", "layer"?: name|id}"#,
),
C::new("layout.series", "Map Series", "Publish › Map Frames", series).inputs(series_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(
r#"{"layout"?, "layer": index layer, "nameField"?: string, "frame"?: id, "margin"?: 0..1, "off"?: bool}"#,
r#"{"layout"?, "layer": index layer name|id, "nameField"?: string, "frame"?: id, "margin"?: 0..1, "off"?: bool}"#,
),
C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).inputs(series_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layout"?, "page": number|"next"|"previous"}"#),
C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"}))).inputs(series_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
Expand Down
3 changes: 1 addition & 2 deletions crates/engine/src/cmd/layout/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,7 @@ pub(in crate::cmd::layout) fn frame_zoom(s: &mut Session, p: &Value) -> CmdResul
set_frame(s, li, frame, &e)?;
}
"layer" => {
let n = ps(p, "layer").ok_or("layer is required")?;
let e = m.layer_by_name(n).ok_or_else(|| format!("no layer '{n}'"))?.shown_extent();
let e = crate::cmd::given_layer(m, p, "layer").ok_or("layer is required")??.shown_extent();
set_frame(s, li, frame, &e)?;
}
o => return Err(format!("unknown zoom '{o}'")),
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/layout/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(in crate::cmd::layout) fn frame_zoom_inputs(input: &crate::cmd::InputContext
let frame = zoom_frame(layout, p)?;
let map = frame_map(input, layout, frame)?;
let mut inputs = match ps(p, "to").unwrap_or("view") {
"layer" => crate::cmd::Inputs::named(map, [ps(p, "layer").ok_or("layer is required")?])?,
"layer" => crate::cmd::Inputs::given(map, p, "layer")?.ok_or("layer is required")?,
"full" | "selection" => crate::cmd::Inputs::layers(map.layers.iter().map(|l| (map, l))),
_ => crate::cmd::Inputs::layers(std::iter::empty()),
};
Expand All @@ -61,7 +61,7 @@ pub(in crate::cmd::layout) fn series_inputs(input: &crate::cmd::InputContext<'_>
let layout = s.project.layouts.get(index(input, p)?).ok_or("no layout")?;
let frame = frame_of(layout, p)?;
let map = frame_map(input, layout, frame)?;
let mut inputs = crate::cmd::Inputs::named(map, [ps(p, "layer").ok_or("layer is required")?])?;
let mut inputs = crate::cmd::Inputs::given(map, p, "layer")?.ok_or("layer is required")?;
inputs.maps.push(map.instance_id());
inputs.state = json!({"layout":index(input,p)?,"frame":frame});
Ok(inputs)
Expand Down
6 changes: 4 additions & 2 deletions crates/engine/src/cmd/layout/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ pub(in crate::cmd::layout) fn series(s: &mut Session, p: &Value) -> CmdResult {
Some(ElementKind::MapFrame { map, .. }) => map.clone(),
_ => return Err("no such frame".into()),
};
let n = ps(p, "layer").ok_or("layer is required")?;
let m = frame_map(s, l, frame)?;
let layer = m.layer_by_name(n).filter(|l| !l.is_raster()).ok_or_else(|| format!("no feature layer '{n}' in {map}"))?;
let layer = crate::cmd::given_layer(m, p, "layer").ok_or("layer is required")??;
if layer.is_raster() {
return Err(format!("no feature layer '{}' in {map}", layer.name));
}
rustgis_map::layout::require_series_index(layer)?;
if layer.dataset.features.is_empty() {
return Err("the index layer has no features".into());
Expand Down
38 changes: 26 additions & 12 deletions crates/engine/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,9 @@ impl<'a> InputContext<'a> {
/// Resolves numeric, name-before-id and default layer choices for both planner and executor.
pub fn layer_id(&self, p: &Value) -> Result<u32, String> {
let map = self.map();
match p.get("layer") {
Some(Value::Number(n)) => n
.as_u64()
.and_then(|n| u32::try_from(n).ok())
.filter(|id| map.layer(*id).is_some())
.ok_or_else(|| format!("no layer with id {n}")),
Some(Value::String(s)) => map
.layer_by_name(s)
.map(|l| l.id)
.or_else(|| s.parse::<u32>().ok().filter(|id| map.layer(*id).is_some()))
.ok_or_else(|| format!("no layer named '{s}'")),
_ => {
match given_layer(map, p, "layer") {
Some(layer) => layer.map(|l| l.id),
None => {
self.active_layer.filter(|id| map.layer(*id).is_some()).ok_or_else(|| "select a layer in the Layers pane first".to_string())
}
}
Expand All @@ -619,6 +610,20 @@ impl<'a> InputContext<'a> {
self.map().layers.iter().map(|l| l.id).filter(|id| picked.contains(id) || self.active_layer == Some(*id)).collect()
}
}
/// The layer of `map` that `p[key]` gives by its id, its name, or its id as text, as
/// [`InputContext::layer_id`] reads `layer` (#3509); `None` when `key` gives neither.
pub(crate) fn given_layer<'a>(map: &'a rustgis_map::Map, p: &Value, key: &str) -> Option<Result<&'a rustgis_map::Layer, String>> {
Some(match p.get(key)? {
Value::Number(n) => {
n.as_u64().and_then(|n| u32::try_from(n).ok()).and_then(|id| map.layer(id)).ok_or_else(|| format!("no layer with id {n}"))
}
Value::String(s) => map
.layer_by_name(s)
.or_else(|| s.parse::<u32>().ok().and_then(|id| map.layer(id)))
.ok_or_else(|| format!("no layer named '{s}'")),
_ => return None,
})
}
/// Only scalar choices a pure owner transition can model without running a handler.
#[derive(Clone, Copy)]
pub(crate) struct Selection {
Expand Down Expand Up @@ -684,6 +689,15 @@ impl Inputs {
Ok(inputs)
}

/// Binds the layer `p[key]` gives, as [`given_layer`] reads it: a name may also be a layer
/// a batch has yet to add, as [`Inputs::named`] binds it; an id names a layer already there.
pub fn given(map: &rustgis_map::Map, p: &serde_json::Value, key: &str) -> Result<Option<Self>, String> {
match (given_layer(map, p, key), p.get(key)) {
(Some(Err(_)), Some(serde_json::Value::String(name))) => Self::named(map, [name.as_str()]).map(Some),
(found, _) => found.transpose().map(|l| l.map(|l| Self::layers([(map, l)]))),
}
}

/// The ordinary primary selector uses Session's exact numeric/name/default semantics.
pub fn primary(s: &impl InputSource, p: &serde_json::Value) -> Result<Self, String> {
let context = s.input_context();
Expand Down
40 changes: 27 additions & 13 deletions crates/engine/src/cmd/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ fn need_report(s: &Session) -> Option<&'static str> {
pub fn commands() -> Vec<C> {
vec![
C::new("report.new", "New Report", "Publish › Report", new_report).inputs(new_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(
r#"{"layer"?: name (default: the selected layer), "name"?, "title"?, "fields"?: [names] (default: all), "where"?: sql, "selectedOnly"?: bool, "groups"?: [fields], "sort"?: field, "descending"?: bool, "page"?: "letter"|"a4"|…, "orientation"?: "portrait"|"landscape"}"#,
r#"{"layer"?: name|id (default: the selected layer), "name"?, "title"?, "fields"?: [names] (default: all), "where"?: sql, "selectedOnly"?: bool, "groups"?: [fields], "sort"?: field, "descending"?: bool, "page"?: "letter"|"a4"|…, "orientation"?: "portrait"|"landscape"}"#,
),
C::new("report.update", "Report Properties", "Report", update).inputs(update_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_report).params(
r#"{"report"?: name|index, "name"?, "title"?, "layer"?, "where"?: sql, "selectedOnly"?, "sort"?: field|null, "descending"?, "page"?, "orientation"?, "width"?, "height"?: mm}"#,
r#"{"report"?: name|index, "name"?, "title"?, "layer"?: name|id, "where"?: sql, "selectedOnly"?, "sort"?: field|null, "descending"?, "page"?, "orientation"?, "width"?, "height"?: mm}"#,
),
C::new("report.activate", "Open Report", "Browser", |s, p| {
let i = index(s, p)?;
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn commands() -> Vec<C> {
.when(need_report)
.params(r#"{"report"?, "path": "*.rgisrpttemplate"}"#),
C::new("report.open", "Import Report", "Publish › Report", open).inputs(open_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(
r#"{"path"?: "*.rgisreport|*.rgisrpttemplate", "text"?: report JSON, "data"?: base64 file, "layer"?: the layer a template reports on (default: the selected layer)}"#,
r#"{"path"?: "*.rgisreport|*.rgisrpttemplate", "text"?: report JSON, "data"?: base64 file, "layer"?: name|id of the layer a template reports on (default: the selected layer)}"#,
),
]
}
Expand Down Expand Up @@ -648,8 +648,8 @@ fn update(s: &mut Session, p: &Value) -> CmdResult {
let old_name = r.name.clone();
let mut renamed = None;
let mut fitted = Fitted::default();
if let Some(n) = ps(p, "layer") {
let l = find_layer(s, n).ok_or_else(|| format!("no layer named '{n}'"))?;
if let Some(found) = given_layer_map(s, p) {
let l = found?.1;
if l.is_raster() {
return Err("reports list a feature layer's records, not a raster's".into());
}
Expand Down Expand Up @@ -1562,8 +1562,8 @@ fn say_report_clipped(s: &mut Session, clipped: &ClippedTexts) {
fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Layer, String> {
let context = input.input_context();
let s = &context;
Ok(match ps(p, "layer") {
Some(n) => find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?,
Ok(match given_layer_map(input, p) {
Some(found) => found?.1,
None => s.active_layer.and_then(|id| s.map().layer(id)).ok_or("select a layer, or pass layer")?,
})
}
Expand All @@ -1572,8 +1572,8 @@ fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a La
fn import_layer(input: &impl super::InputSource, p: &Value, r: &mut Report) -> Result<(), String> {
let context = input.input_context();
let s = &context;
if let Some(n) = ps(p, "layer") {
r.layer = find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?.name.clone();
if let Some(found) = given_layer_map(input, p) {
r.layer = found?.1.name.clone();
} else if find_layer(input, &r.layer).is_none() {
// A template (or a report on a layer this project lacks) reports on the selected layer.
r.layer = s
Expand All @@ -1597,6 +1597,20 @@ pub(super) fn named_elsewhere(s: &Session, name: &str) -> bool {
s.project.maps.iter().filter(|m| !std::ptr::eq(*m, s.map())).any(|m| m.layer_by_name(name).is_some())
}

/// The layer `layer` gives: by name as [`find_layer_map`] finds it, else by its id (or its id as
/// text) in the active map, as `Session::layer_id` reads it (#3509); `None` when not given.
fn given_layer_map<'a>(input: &'a impl super::InputSource, p: &Value) -> Option<Result<(&'a rustgis_map::Map, &'a Layer), String>> {
let context = input.input_context();
let map = context.map();
match p.get("layer")? {
Value::String(name) => {
find_layer_map(input, name).map(Ok).or_else(|| super::given_layer(map, p, "layer").map(|l| l.map(|l| (map, l))))
}
Value::Number(_) => super::given_layer(map, p, "layer").map(|l| l.map(|l| (map, l))),
_ => None,
}
}

/// Finds a literal feature layer active-map first, then in project order.
fn find_layer_map<'a>(input: &'a impl super::InputSource, name: &str) -> Option<(&'a rustgis_map::Map, &'a Layer)> {
let context = input.input_context();
Expand Down Expand Up @@ -1691,8 +1705,8 @@ fn update_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::In
let s = input;
let report = s.project.reports.get(index_input(input, p)?).ok_or("no report")?;
let mut inputs = object_inputs(input, p)?;
if let Some(name) = ps(p, "layer") {
let (map, layer) = find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?;
if let Some(found) = given_layer_map(input, p) {
let (map, layer) = found?;
inputs.layers.push((map.instance_id(), layer.id));
for sec in &report.sections {
if let Section::Subreport { relationship, .. } = sec
Expand All @@ -1709,8 +1723,8 @@ fn update_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::In
}
/// Uses inline data/text precedence for pure import planning; path-held selectors remain unresolved.
fn open_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::Inputs, String> {
if let Some(name) = ps(p, "layer") {
return Ok(super::Inputs::layers([find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?]));
if let Some(found) = given_layer_map(input, p) {
return Ok(super::Inputs::layers([found?]));
}
let Some(text) = super::items::inline_text(p)? else {
return Ok(super::Inputs::unresolved());
Expand Down
28 changes: 18 additions & 10 deletions crates/engine/src/cmd/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub fn commands() -> Vec<C> {
profile(s, p, &pts)
}).roles(&[]).effects(crate::policy::Effects::NONE)
.view()
.params(r#"{"layer": elevation raster, "points": [[lon, lat], …], "samples"?: number}"#),
.params(r#"{"layer": elevation raster name|id, "points": [[lon, lat], …], "samples"?: number}"#),
C::new("profile.autoComplete", "Auto-complete Profile", "View › Profile Viewing", |s, p| {
// Continue the line from its first point through its second to the raster's edge.
let pts = points(p)?;
Expand All @@ -204,7 +204,7 @@ pub fn commands() -> Vec<C> {
profile(s, p, &[a, Coord::new(a.x + dx * t, a.y + dy * t)])
}).roles(&[]).effects(crate::policy::Effects::NONE)
.view()
.params(r#"{"layer": elevation raster, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (a start and a direction)}"#),
.params(r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (a start and a direction)}"#),
C::new("profile.crossSection", "Cross Section", "View › Profile Viewing", |s, p| {
// Across a line at a point: perpendicular, `width` metres long.
let pts = points(p)?;
Expand All @@ -221,15 +221,15 @@ pub fn commands() -> Vec<C> {
}).roles(&[]).effects(crate::policy::Effects::NONE)
.view()
.params(
r#"{"layer": elevation raster, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (the line crossed), "at"?: 0..1 along it, "width"?: metres}"#,
r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (the line crossed), "at"?: 0..1 along it, "width"?: metres}"#,
),
C::new("profile.alongLine", "Profile Along a Line", "View › Profile Viewing", |s, p| {
let (lines, _) = selected_lines(s, p, false)?;
let first = lines.into_iter().next().ok_or("no line")?;
profile(s, p, &first)
}).inputs(profile_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
.view()
.params(r#"{"layer": elevation raster, "samples"?: number (default 200), "lines": line layer, "row"?: number (the first selected when omitted)}"#),
.params(r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "lines": line layer, "row"?: number (the first selected when omitted)}"#),
C::new("profile.alongSelected", "Profile Along Selected Lines", "View › Profile Viewing", |s, p| {
let (lines, unplaced) = selected_lines(s, p, true)?;
// The lines past the first 50 get no profile, and that is said (#1896).
Expand All @@ -248,11 +248,11 @@ pub fn commands() -> Vec<C> {
Ok(Value::Array(out))
}).inputs(profile_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
.view()
.params(r#"{"layer": elevation raster, "samples"?: number (default 200), "lines": line layer}"#),
.params(r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "lines": line layer}"#),
// Infographics and diagnostics.
C::new("info.infographic", "Infographics", "Home › Inspect", infographic).roles(&[]).effects(crate::policy::Effects::NONE)
.view()
.params(r#"{"layer"? (every visible feature layer when omitted)}"#),
.params(r#"{"layer"?: name|id (every visible feature layer when omitted)}"#),
C::new("info.diagnostics", "Diagnostic Monitor", "Help › Performance", |s, _| Ok(diagnostics(s))).roles(&[]).effects(crate::policy::Effects::NONE).view(),
// Offline areas.
C::new("offline.download", "Download Map", "Data › Offline", offline_download).roles(&[]).effects(crate::policy::Effects::NONE)
Expand Down Expand Up @@ -561,8 +561,8 @@ fn points(p: &Value) -> Result<Vec<Coord>, String> {
}

fn dem(s: &Session, p: &Value) -> Result<std::sync::Arc<rustgis_data::raster::Raster>, String> {
let n = ps(p, "layer").ok_or("layer (an elevation raster) is required")?;
s.map().layer_by_name(n).and_then(|l| l.raster.as_ref()).and_then(|r| r.data.clone()).ok_or_else(|| format!("no raster layer '{n}'"))
let l = super::given_layer(s.map(), p, "layer").ok_or("layer (an elevation raster) is required")??;
l.raster.as_ref().and_then(|r| r.data.clone()).ok_or_else(|| format!("no raster layer '{}'", l.name))
}

/// An elevation profile with its summary.
Expand Down Expand Up @@ -714,7 +714,13 @@ pub fn diagnostics(s: &Session) -> Value {
}

fn infographic(s: &mut Session, p: &Value) -> CmdResult {
Ok(infographic_of(s, ps(p, "layer")))
// A layer's id, or its id as text, reads as its name (#3509); a name the map lacks still
// gives no cards.
let only = match (super::given_layer(s.map(), p, "layer"), ps(p, "layer")) {
(Some(Err(_)), Some(name)) => Some(name.to_string()),
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
(found, _) => found.transpose()?.map(|l| l.name.clone()),
};
Ok(infographic_of(s, only.as_deref()))
}

/// Cards summarising the features in view, for every visible feature layer or just `only`.
Expand Down Expand Up @@ -1436,7 +1442,9 @@ pub(crate) fn area_map_index(project: &Project, ai: usize) -> Option<usize> {
/// Binds the elevation raster and literal selected-line source.
fn profile_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::Inputs, String> {
let s = input;
super::Inputs::named(s.map(), [super::imagery::raster_name(p)?, profile_lines(p)?])
let mut inputs = super::Inputs::given(s.map(), p, "layer")?.ok_or("layer is required")?;
inputs.extend(super::Inputs::named(s.map(), [profile_lines(p)?])?);
Ok(inputs)
}

/// Parses the same required line-layer name used by profile extraction.
Expand Down
8 changes: 4 additions & 4 deletions crates/engine/src/policy/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,10 +1113,10 @@ impl<'a> Builder<'a> {
{
Err(error)
}
Err(_) => value
.as_str()
.map(|n| Target::MadeLayer { map: self.context().map().instance_id(), name: n.into() })
.ok_or("unresolved layer target".into()),
// An id names a layer already there: its error is the resolver's (#3509).
Err(error) => {
value.as_str().map(|n| Target::MadeLayer { map: self.context().map().instance_id(), name: n.into() }).ok_or(error)
}
}
}
/// An approved rename retains the original incarnation and adds its source when later read.
Expand Down
Loading
Loading