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
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ fn item_output_within(
(name.clone(), super::deeplearning::package_within(s, &json!({"model": name}), out)?)
}
"hostedImagery" => {
let name = ps(p, "layer").unwrap_or("tiles").to_string();
let name = super::given_layer_name(s.map(), p, "layer")?.unwrap_or("tiles").to_string();
(format!("{name}_tiles"), super::deeplearning::hosted_within(s, p, out)?.0)
}
k if super::sharing::KINDS.iter().any(|x| x.0 == k) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub fn commands() -> Vec<C> {
r##""length"?: mm (line, arrow), "##,
r##""image"?: base64, "##,
r##""path"?: picture file (picture: read, and remembered as its file, so a picture whose image goes missing names it and layout.update can read it again; an address is refused, and image and path together; refused in the browser, which has no file paths: give its image; refused for other kinds), "##,
r##""layer"?: (table, chart; colorbar: a raster layer, default the top visible raster with a colour scale, not drawn by classes, which a legend lists; profile: a raster layer held whole, not read in place, default the top one shown), "##,
r##""layer"?: name|id (table, chart; colorbar: a raster layer, default the top visible raster with a colour scale, not drawn by classes, which a legend lists; profile: a raster layer held whole, not read in place, default the top one shown), "##,
r##""field"?: (chart: a numeric field, default the layer's first; any field when it groups and counts, default then the field it groups by), "##,
r##""label"?: a field (chart: each bar's label when not grouped), "##,
r##""group"?: a field (chart: a bar per value of this field, its statistic of field over that value's features; blank values make one "(blank)" bar; default none, a bar per feature), "##,
Expand Down Expand Up @@ -384,7 +384,7 @@ pub fn commands() -> Vec<C> {
r##""extentNumbers"?: bool (map frame: numbers its extent indicators 1, 2, 3… in their order, each in its outline's colour at the outline's centre; none where that centre is off the frame or behind a globe's horizon, for an outline in a clear colour, or for a frame with no outline, one showing the whole globe; refused for other elements), "##,
r##""border"?: {"color"?: "#RRGGBB"|"#RRGGBBAA", "width"?: points, 0 to 20, 0 for none} | null (map frame: the outline along its shape, default #303030 and 0.8, the keys not given keeping theirs; null gives the default back; refused for other elements), "##,
r##""section"?: {"name": the section line it changes, "newName"?, "direction"?, "at"?, "color"?, "width"?, "dash"? ([] or null for solid), as in layout.addElement, the keys not given keeping theirs, "remove"?: true to remove it} (map frame; refused for other elements), "##,
r##""layer"?: (colorbar: "" for the top visible raster with a colour scale, not drawn by classes), "##,
r##""layer"?: name|id (colorbar: "" for the top visible raster with a colour scale, not drawn by classes), "##,
r##""field"?, "##,
r##""label"?, "##,
r##""group"?, "##,
Expand Down
9 changes: 9 additions & 0 deletions crates/engine/src/cmd/layout/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ pub(in crate::cmd::layout) fn given_name<'a>(p: &'a Value, k: &str, names: &str)
}
}

/// The layer given as `layer` in `map`, by its name, its id or its id as text (#3509), when it's
/// given (and not null): an error when it's neither text nor a number.
pub(in crate::cmd::layout) fn given_layer_in<'a>(map: &'a rustgis_map::Map, p: &'a Value, names: &str) -> Result<Option<&'a str>, String> {
match p.get("layer") {
Some(Value::String(_) | Value::Number(_)) => crate::cmd::given_layer_name(map, p, "layer"),
_ => given_name(p, "layer", names),
}
}

/// A choice given as `k` by name, when it's given (and not null): an error when it isn't one of
/// `names`.
pub(in crate::cmd::layout) fn given_choice<T>(p: &Value, k: &str, from: fn(&str) -> Option<T>, names: &str) -> Result<Option<T>, String> {
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/layout/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu
}
"colorbar" => {
let frame = frame_of(l, p)?;
let named = given_name(p, "layer", "a raster layer's name")?;
let named = given_layer_in(frame_map(s, l, frame)?, p, "a raster layer's name")?;
let layer = colorbar_layer(s, l, frame, named)?;
let title = match given_name(p, "title", "text")? {
Some(t) => crate::cmd::within("title", "a colorbar's title", t, 200)?,
Expand Down
16 changes: 11 additions & 5 deletions crates/engine/src/cmd/layout/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ pub(in crate::cmd::layout) fn frame_layer_name(
_ => return Err("no such frame".into()),
};
let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?;
let found = match given_name(p, "layer", "a feature layer's name")? {
let found = match given_layer_in(m, p, "a feature layer's name")? {
Some(n) => m.layer_by_name(n),
None => m.layers.iter().find(|l| !l.is_raster() && !l.graphics),
};
found.map(|l| l.name.clone()).ok_or_else(|| "no such layer in the frame's map".to_string())
}

/// Whether `layer` names a layer: an id, or text that isn't empty.
fn gives_layer(p: &Value) -> bool {
p.get("layer").is_some_and(|v| v.is_number() || v.as_str().is_some_and(|n| !n.trim().is_empty()))
}

/// Resolves a frame's stored map literally; missing frame maps are errors here.
pub(in crate::cmd::layout) fn frame_map<'a>(
input: &'a impl crate::cmd::InputSource,
Expand Down Expand Up @@ -119,7 +124,7 @@ pub(in crate::cmd::layout) fn element_inputs(input: &crate::cmd::InputContext<'_
let name = frame_layer_name(input, layout, frame, p)?;
inputs.extend(crate::cmd::Inputs::named(map, [name.as_str()])?);
} else if kind == "colorbar" {
if let Some(name) = colorbar_layer(input, layout, frame, ps(p, "layer"))? {
if let Some(name) = colorbar_layer(input, layout, frame, given_layer_in(map, p, "a raster layer's name")?)? {
inputs.extend(crate::cmd::Inputs::named(map, [name.as_str()])?);
}
} else {
Expand Down Expand Up @@ -231,13 +236,14 @@ pub(in crate::cmd::layout) fn update_inputs(input: &crate::cmd::InputContext<'_>
}
match l.element(id).map(|e| &e.kind) {
Some(ElementKind::MapFrame { .. }) if ps(p, "map").is_some() => inputs.maps.push(added_frame_map(input, p)?.instance_id()),
Some(ElementKind::Colorbar { frame, .. }) if ps(p, "layer").is_some_and(|n| !n.trim().is_empty()) => {
if let Some(name) = colorbar_layer(input, l, *frame, ps(p, "layer"))? {
Some(ElementKind::Colorbar { frame, .. }) if gives_layer(p) => {
let named = given_layer_in(frame_map(input, l, *frame)?, p, "a raster layer's name")?;
if let Some(name) = colorbar_layer(input, l, *frame, named)? {
inputs.extend(crate::cmd::Inputs::named(frame_map(input, l, *frame)?, [name.as_str()])?);
}
}
// A profile's new raster (#2863 part 2).
Some(ElementKind::Profile(profile)) if ps(p, "layer").is_some_and(|n| !n.trim().is_empty()) => {
Some(ElementKind::Profile(profile)) if gives_layer(p) => {
let map = frame_map(input, l, profile.frame)?;
let name = added_profile_layer(map, p)?;
inputs.extend(crate::cmd::Inputs::named(map, [name.as_str()])?);
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/layout/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn profile_params(s: &Session, l: &Layout, p: &Value, mut base: Profile) -> Resu
}
}
}
if let Some(n) = given_name(p, "layer", "a raster layer's name")? {
if let Some(n) = given_layer_in(map, p, "a raster layer's name")? {
base.layer = profile_layer(map, n)?;
}
if let Some(v) = given_num(p, "band", "a band number from 1")? {
Expand Down Expand Up @@ -333,7 +333,7 @@ pub(in crate::cmd::layout) fn profiles_follow_section(l: &mut Layout, frame: u32
/// The raster layer `layout.addElement` reads for a profile, for its inputs: the one given, else the
/// top one its frame's map shows that it can read.
pub(in crate::cmd::layout) fn added_profile_layer(map: &Map, p: &Value) -> Result<String, String> {
match given_name(p, "layer", "a raster layer's name")? {
match given_layer_in(map, p, "a raster layer's name")? {
Some(n) => profile_layer(map, n),
None => first_profile_layer(map),
}
Expand Down
18 changes: 12 additions & 6 deletions crates/engine/src/cmd/layout/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,15 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult {
let l = s.project.layouts.get(li).ok_or("no layout")?;
match l.element(id).map(|e| &e.kind) {
Some(ElementKind::Colorbar { frame, .. }) => {
let layer = match given_name(p, "layer", "a raster layer's name")? {
Some(n) if n.trim().is_empty() => Some(None),
Some(n) => Some(colorbar_layer(s, l, *frame, Some(n))?),
// The frame's map is read only for a layer given: a colorbar whose map is gone still
// takes its other changes, and empty sets it back to the map's default.
let layer = match p.get("layer").filter(|v| !v.is_null()) {
None => None,
Some(Value::String(n)) if n.trim().is_empty() => Some(None),
Some(_) => {
let named = given_layer_in(frame_map(s, l, *frame)?, p, "a raster layer's name")?;
Some(colorbar_layer(s, l, *frame, named)?)
}
};
Some((layer, orientation(p)?, tick_side(p)?, ticks(p)?))
}
Expand Down Expand Up @@ -1101,8 +1106,8 @@ pub(in crate::cmd::layout) fn checked_update(
let l = s.project.layouts.get(li).ok_or("no layout")?;
let e = l.element(id).ok_or_else(|| format!("no element {id}"))?;
let new_layer = match &e.kind {
ElementKind::Table { .. } | ElementKind::Chart { .. } => given_name(p, "layer", "a feature layer's name")?,
_ => None,
ElementKind::Table { .. } | ElementKind::Chart { .. } => p.get("layer").is_some_and(|v| !v.is_null()),
_ => false,
};
let new_field = match &e.kind {
ElementKind::Chart { .. } => given_name(p, "field", "a field's name")?,
Expand All @@ -1112,13 +1117,14 @@ pub(in crate::cmd::layout) fn checked_update(
let columns = matches!(e.kind, ElementKind::Table { .. }) && (gives(p, "fields") || gives(p, "sort"));
match (&e.kind, new_layer, new_field) {
(ElementKind::Table { frame, layer, .. } | ElementKind::Chart { frame, layer, .. }, nl, nf)
if nl.is_some() || nf.is_some() || columns =>
if nl || nf.is_some() || columns =>
{
let map = match l.element(*frame).map(|f| &f.kind) {
Some(ElementKind::MapFrame { map, .. }) => map.clone(),
_ => return Err("the element's map frame is gone".into()),
};
let m = s.project.maps.iter().find(|m| m.name.eq_ignore_ascii_case(&map)).ok_or_else(|| format!("no map '{map}'"))?;
let nl = if nl { given_layer_in(m, p, "a feature layer's name")? } else { None };
let target = m
.layer_by_name(nl.unwrap_or(layer))
.filter(|l| !l.is_raster())
Expand Down
1 change: 1 addition & 0 deletions crates/engine/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ pub(crate) fn given_layer_name<'a>(map: &'a rustgis_map::Map, p: &'a Value, key:
s.parse::<u32>().ok().and_then(|id| map.layer(id)).map_or(Ok(Some(s.as_str())), |l| own_name(map, l).map(Some))
}
Some(Value::String(s)) => Ok(Some(s)),
Some(v) if !v.is_null() && !v.is_number() => Err(format!("{key} is a layer's name or id")),
_ => given_layer(map, p, key).transpose()?.map(|l| own_name(map, l)).transpose(),
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/engine/src/cmd/sharing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,12 @@ pub fn commands() -> Vec<C> {
}).roles(&[]).effects(crate::policy::Effects::NONE)
.view(),
C::new("share.saveWebMap", "Save Web Map", "Publish › Manage", |s, p| update_item(s, p, "webMap", ps(p, "map"))).inputs(update_web_map_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).when(need_items).params(MANAGE),
C::new("share.replaceWebLayer", "Replace Web Layer", "Publish › Manage", |s, p| update_item(s, p, "webLayer", ps(p, "layer"))).inputs(update_layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files))
C::new("share.replaceWebLayer", "Replace Web Layer", "Publish › Manage", |s, p| {
let layer = super::given_layer_name(s.map(), p, "layer")?.map(str::to_string);
update_item(s, p, "webLayer", layer.as_deref())
}).inputs(update_layer_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files))
.when(need_items)
.params(r#"{"item"?: id|name (default: the last web layer), "layer"?: the layer replacing it (default: the one it was made from)}"#),
.params(r#"{"item"?: id|name (default: the last web layer), "layer"?: name|id of the layer replacing it (default: the one it was made from)}"#),
C::new("share.dataStore", "Data Stores", "Publish › Manage", data_store).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"name"?, "path"?: folder, "remove"?: name (no parameters: list them)}"#),
C::new("share.upload", "Upload File", "Publish › Manage", upload).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params(r#"{"path": file, "store"?: name (default: the first data store)}"#),
C::new("share.saveWebScene", "Save Web Scene", "Publish › Manage", |s, p| update_item(s, p, "webScene", ps(p, "map"))).inputs(update_web_scene_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).when(need_items).params(MANAGE),
Expand Down Expand Up @@ -1869,7 +1872,7 @@ fn update_web_scene_inputs(input: &super::InputContext<'_>, p: &Value) -> Result
}
/// Binds a managed feature layer even when the request omits its source.
fn update_layer_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::Inputs, String> {
update_source_inputs(input, p, "webLayer", ps(p, "layer"))
update_source_inputs(input, p, "webLayer", super::given_layer_name(input.map(), p, "layer")?)
}
/// Binds the managed chart's stored relationship selection and saved path.
fn update_links_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::Inputs, String> {
Expand Down
Loading
Loading