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
442 changes: 423 additions & 19 deletions crates/engine/src/cmd/connection_browse.rs

Large diffs are not rendered by default.

62 changes: 51 additions & 11 deletions crates/engine/src/cmd/portals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn osm_download(s: &mut Session, p: &Value) -> CmdResult {
let url = q.url(ps(p, "url"))?;
let bytes = match ps(p, "answer") {
Some(a) => a.as_bytes().to_vec(),
None => fetch(&url)?,
None => fetch(&url)?.0,
};
let answer = overpass::parse(&bytes)?;
let name = ps(p, "name").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string).unwrap_or_else(|| q.title());
Expand Down Expand Up @@ -92,8 +92,10 @@ fn boundaries_add(s: &mut Session, p: &Value) -> CmdResult {
let (text, title) = match ps(p, "text") {
Some(t) => (t.to_string(), None),
None => {
let meta = boundaries::parse_meta(&fetch(&boundaries::meta_url(&country, &level))?)?;
let bytes = fetch(&meta.geojson)?;
let (meta, reach) = fetch(&boundaries::meta_url(&country, &level))?;
let meta = boundaries::parse_meta(&meta)?;
// The answer names where the boundaries are: they are read no further than it was.
let (bytes, _) = fetch_within(&meta.geojson, reach)?;
(String::from_utf8(bytes).map_err(|_| "the boundaries aren't UTF-8 text".to_string())?, Some(meta.name))
}
};
Expand All @@ -113,7 +115,7 @@ fn boundaries_add(s: &mut Session, p: &Value) -> CmdResult {
}

fn boundaries_countries(_s: &mut Session, _p: &Value) -> CmdResult {
let list = boundaries::parse_countries(&fetch(&boundaries::countries_url())?)?;
let list = boundaries::parse_countries(&fetch(&boundaries::countries_url())?.0)?;
Ok(json!({"count": list.len(), "countries": list.iter().map(|(iso, name)| json!({"iso": iso, "name": name})).collect::<Vec<_>>()}))
}

Expand Down Expand Up @@ -155,7 +157,10 @@ pub fn dataset_json(d: &portal::Dataset) -> Value {
fn portal_search(s: &mut Session, p: &Value) -> CmdResult {
let (portal, query, area, limit) = portal_query(s, p)?;
let url = portal.search_url(&query, area, limit);
let found = portal.parse(&url, &fetch(&url)?, area, &query)?;
let (bytes, reach) = fetch(&url)?;
let mut refused = portal::Refused::default();
let found = portal.parse(reach, &mut refused, &bytes, area, &query)?;
say_refused(s, &refused);
Ok(json!({"portal": portal.label(), "count": found.len(), "datasets": found.iter().map(dataset_json).collect::<Vec<_>>()}))
}

Expand All @@ -168,9 +173,12 @@ pub fn files_query(p: &Value) -> Result<(Portal, String), String> {
Ok((portal, url))
}

fn portal_files(_s: &mut Session, p: &Value) -> CmdResult {
fn portal_files(s: &mut Session, p: &Value) -> CmdResult {
let (portal, url) = files_query(p)?;
let files = portal.parse_listing(&url, &fetch(&url)?)?;
let (bytes, reach) = fetch(&url)?;
let mut refused = portal::Refused::default();
let files = portal.parse_listing(&url, reach, &mut refused, &bytes)?;
say_refused(s, &refused);
let list: Vec<Value> = files
.iter()
.map(|r| match &r.using {
Expand Down Expand Up @@ -250,11 +258,40 @@ fn first_layer(s: &mut Session, url: &str, kind: &str) -> Result<String, String>
.ok_or_else(|| crate::sources::message_with_address(url, "the service lists no layer RustGIS can add"))
}

/// Says how many addresses a portal's answer gave that were left out for pointing where RustGIS
/// won't fetch for it (into the local network from an answer read from the internet, or a
/// forbidden address from any answer), with the first and why.
pub fn refused_notice(refused: &portal::Refused) -> Option<String> {
let first = refused.first.as_deref()?;
Some(match refused.count {
1 => format!("1 address the portal listed is left out: {first}"),
n => format!("{n} addresses the portal listed are left out, such as {first}"),
})
}

fn say_refused(s: &mut Session, refused: &portal::Refused) {
if let Some(notice) = refused_notice(refused) {
s.notify(notice, false);
}
}

/// A fetch the command waits for (the command line and the assistant; the app fetches off the
/// frame and passes the answer).
/// frame and passes the answer), with the reach it was read at: the addresses the answer gives are
/// judged by it.
fn fetch(url: &str) -> Result<(Vec<u8>, crate::render::rustgis_tiles::address::Reach), String> {
fetch_within(url, crate::render::rustgis_tiles::address::Reach::Local)
}

/// [`fetch`], connecting no wider than `ceiling` (the reach of the answer that named `url`).
#[cfg(not(target_arch = "wasm32"))]
fn fetch(url: &str) -> Result<Vec<u8>, String> {
crate::render::rustgis_tiles::cache::download_blocking(url).map_err(|e| {
fn fetch_within(
url: &str,
ceiling: crate::render::rustgis_tiles::address::Reach,
) -> Result<(Vec<u8>, crate::render::rustgis_tiles::address::Reach), String> {
use crate::render::rustgis_tiles::cache;
let fetched = cache::download_blocking_with_origin_within(url, cache::MAX_DOWNLOAD, cache::BLOCKING_TIMEOUT, &[], ceiling)
.map_err(cache::large_file_hint);
fetched.map(|f| (f.bytes, f.origin.reach())).map_err(|e| {
if e.contains("timeout") || e.contains("timed out") {
"the server didn't answer within a minute (it may be busy): try again later, or a smaller area".to_string()
} else if e.contains("504") || e.contains("429") {
Expand All @@ -266,7 +303,10 @@ fn fetch(url: &str) -> Result<Vec<u8>, String> {
}

#[cfg(target_arch = "wasm32")]
fn fetch(_url: &str) -> Result<Vec<u8>, String> {
fn fetch_within(
_url: &str,
_: crate::render::rustgis_tiles::address::Reach,
) -> Result<(Vec<u8>, crate::render::rustgis_tiles::address::Reach), String> {
Err("in the web app, use Home › Add › Web Data › OpenStreetMap…, Boundaries… or Search Open Data…".into())
}

Expand Down
60 changes: 35 additions & 25 deletions crates/engine/src/cmd/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,33 +433,43 @@ fn identify(s: &mut Session, p: &Value) -> CmdResult {
} else {
Vec::new()
};
let mut out = match results.first().cloned() {
Some(first) => first,
// Only the tile layer picked is asked, not a raster's pixel.
None if picked.is_some() && asks.is_empty() => return Ok(Value::Null),
None if picked.is_some() => json!({}),
// Nothing found (Topmost and Visible looked at the rasters already): Selectable and
// Selected in Layers report the pixel of the topmost raster instead.
None if mode == PopupMode::SelectedInContents && active.and_then(|id| map.layer(id)).is_some_and(|l| !l.is_raster()) => {
return Ok(Value::Null);
}
// (Topmost and Visible found no pixel either: no raster has one there.)
None => match if matches!(mode, PopupMode::Topmost | PopupMode::Visible) { None } else { super::raster::identify(s, lon, lat) } {
Some(pixel) => pixel,
None if asks.is_empty() => return Ok(Value::Null),
None => json!({}),
},
};
if let Value::Object(o) = &mut out {
// A raster's pixel found alone answers as before, without a list.
if results.len() > 1 || results.first().is_some_and(|r| r["raster"] != true) {
o.insert("results".into(), Value::Array(results));
}
if !asks.is_empty() {
o.insert("featureInfo".into(), Value::Array(asks));
// Said whichever layer is reported: a WMS layer whose request reaches past it is never asked.
let refused = if web { info::refused(s, lon, lat, picked) } else { None };

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.

Quality, low-medium confidence. info::refused scans every askable WMS layer, but info::requests only asks the topmost one when first_layer is set, and only when identify actually asks. So under Topmost or Visible, a refused layer beneath the reported feature gets a notice saying it "wasn't asked what is here". Identify never intended to ask it.

Suggestion: pass first_layer (and the same "asks" condition used for requests above) into info::refused, so the notice covers only layers identify would have tried. If the broader notice is intended, say so in a comment here.

Category: quality

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.

Left for the release quality pass: this was asked for in an earlier round (say a refused layer whichever layer is reported), and the wording is literally true, since that layer's request reaches past what it may and is never asked. Narrowing the notice to the layers identify would have asked under Topmost is a wording choice, not a fix.

let out = (|| -> CmdResult {
let mut out = match results.first().cloned() {
Some(first) => first,
// Only the tile layer picked is asked, not a raster's pixel.
None if picked.is_some() && asks.is_empty() => return Ok(Value::Null),
None if picked.is_some() => json!({}),
// Nothing found (Topmost and Visible looked at the rasters already): Selectable and
// Selected in Layers report the pixel of the topmost raster instead.
None if mode == PopupMode::SelectedInContents && active.and_then(|id| map.layer(id)).is_some_and(|l| !l.is_raster()) => {
return Ok(Value::Null);
}
// (Topmost and Visible found no pixel either: no raster has one there.)
None => match if matches!(mode, PopupMode::Topmost | PopupMode::Visible) { None } else { super::raster::identify(s, lon, lat) }
{
Some(pixel) => pixel,
None if asks.is_empty() => return Ok(Value::Null),
None => json!({}),
},
};
if let Value::Object(o) = &mut out {
// A raster's pixel found alone answers as before, without a list.
if results.len() > 1 || results.first().is_some_and(|r| r["raster"] != true) {
o.insert("results".into(), Value::Array(results));
}
if !asks.is_empty() {
o.insert("featureInfo".into(), Value::Array(asks));
}
}
Ok(out)
})();
// A WMS layer whose request reaches past it isn't asked, and says so.
if let Some(notice) = refused {
s.notify(notice, false);
}
Ok(out)
out
}

/// Whether identify lists a tile layer's features: archives, vector tiles and overlays added as
Expand Down
Loading
Loading