diff --git a/crates/engine/src/cmd/service.rs b/crates/engine/src/cmd/service.rs index 022648a150..84fc34d5b9 100644 --- a/crates/engine/src/cmd/service.rs +++ b/crates/engine/src/cmd/service.rs @@ -478,9 +478,15 @@ fn added_layers(r: &Value) -> Vec { /// otherwise (#2080). pub(crate) fn apply_drawing_info(s: &mut Session, r: &Value, name: &str, drawing: &Value) { let mut notes = rustgis_map::arcgis_style::Notes::default(); + // As `layer.marker` checks an image: one kept already decodes into the drawing's cache, + // a new one is decoded once before it is kept. + let readable = |b64: &str| match rustgis_map::Picture::kept(b64) { + Some(kept) => rustgis_render::picture_marker_draws(kept), + None => rustgis_render::picture_marker_readable(b64), + }; for id in added_layers(r) { if let Some(l) = s.map_mut().layer_mut(id) { - rustgis_map::arcgis_style::apply(l, drawing, &mut notes); + rustgis_map::arcgis_style::apply(l, drawing, &readable, &mut notes); } } if !notes.is_empty() { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index fd60d9b549..0930485ab7 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -22964,6 +22964,42 @@ fn a_service_download_is_added_with_the_drawing_info_given() { assert!(sym.default_seed(other.geometry_type()).is_some(), "RustGIS's own symbol: {sym:?}"); } +/// An ArcGIS service's picture markers draw their images, checked as `layer.marker` checks one: +/// a PNG class draws it and the project then asks for picture markers; a GIF class, which the +/// renderer refuses, is a circle in a ramp's colour, said (#2266 part C). +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn an_arcgis_services_picture_markers_draw_their_images() { + use base64::Engine; + use rustgis_map::symbology::Marker; + let mut png = Vec::new(); + image::RgbaImage::from_pixel(6, 4, image::Rgba([0, 120, 200, 255])) + .write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png) + .unwrap(); + let (png, gif) = (base64::engine::general_purpose::STANDARD.encode(&png), gif_base64()); + let picture = |b64: &str| json!({"type": "esriPMS", "imageData": b64, "contentType": "image/png", "width": 12, "height": 8}); + let drawing = json!({"renderer": {"type": "uniqueValue", "field1": "KIND", "uniqueValueInfos": [ + {"value": "school", "symbol": picture(&png)}, {"value": "stop", "symbol": picture(&gif)}]}}); + let answer = r#"{"type": "FeatureCollection", "features": [ + {"type": "Feature", "properties": {"KIND": "school"}, "geometry": {"type": "Point", "coordinates": [0, 0]}}, + {"type": "Feature", "properties": {"KIND": "stop"}, "geometry": {"type": "Point", "coordinates": [1, 1]}}]}"#; + let mut s = Session::new(Project::default()); + let id = s.download_started(); + assert!(s.download_landed(id, answer.as_bytes().to_vec())); + let url = "https://x.org/arcgis/rest/services/P/FeatureServer/0/query?f=geojson"; + s.run("layer.addDownload", &json!({"id": id, "url": url, "name": "Places", "service": true, "drawingInfo": drawing})).unwrap(); + let places = s.map().layer_by_name("Places").unwrap(); + let rustgis_map::Renderer::Unique { classes, .. } = &places.renderer else { panic!("{:?}", places.renderer) }; + let [(_, school), (_, stop)] = classes.as_slice() else { panic!("{classes:?}") }; + assert!(matches!(school.marker, Marker::Picture(p) if p.base64() == png), "{school:?}"); + assert_eq!((school.size, stop.marker), (12.0, Marker::Circle)); + let texts: Vec<_> = s.notices.iter().map(|n| n.text.as_str()).collect(); + let said = + "Places: the service's symbology came in, except: picture markers whose image isn't a PNG or JPEG RustGIS reads, drawn as circles"; + assert!(texts.contains(&said), "{texts:?}"); + assert_eq!(serde_json::to_value(&s.project).unwrap()["requires"], json!(["picture-markers"])); +} + /// An ArcGIS layer's description, read before its features, doesn't count against their budget: /// features that fit it alone are read whole (#1326). #[test] diff --git a/crates/map/src/arcgis_style.rs b/crates/map/src/arcgis_style.rs index 64244d3210..ef9a5b7395 100644 --- a/crates/map/src/arcgis_style.rs +++ b/crates/map/src/arcgis_style.rs @@ -2,9 +2,9 @@ //! it) translated onto a feature layer just added from its service (#2080). use serde_json::Value; -use std::collections::{BTreeMap, BTreeSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap}; -use crate::symbology::{ClassMethod, Dash, LineStroke, MAX_STROKE_WIDTH, Marker, OTHER_FILL, normalize_label}; +use crate::symbology::{ClassMethod, Dash, LineStroke, MAX_PICTURE_BYTES, MAX_STROKE_WIDTH, Marker, OTHER_FILL, Picture, normalize_label}; use crate::{Layer, Ramp, Renderer, Rgba, Symbol}; use rustgis_data::{FieldType, GeometryType}; @@ -14,7 +14,7 @@ const MAX_MARKER_SIZE: f32 = 80.0; /// The most classes a service's renderer brings, as many as a QGIS style's. const MAX_CLASSES: usize = 4096; -/// The colour a picture marker is drawn in, which RustGIS can't draw yet. +/// The colour of a circle drawn for a picture marker whose image RustGIS can't draw. const PICTURE_FILL: Rgba = Rgba::rgb(0x80, 0x80, 0x80); /// What of a service's symbology was drawn differently or left out, each with how many symbols it @@ -40,12 +40,53 @@ impl Notes { } } +/// Picture markers' images (`imageData`) as a service gives them, each checked and kept once, +/// however many classes show it. +struct Pictures<'r> { + /// Whether an image decodes as a picture marker's must (a PNG or JPEG): the check + /// `layer.marker` makes, which is the renderer's, so that what draws here draws there. + readable: &'r dyn Fn(&str) -> bool, + /// Each image met, with its picture or why it isn't drawn. + met: HashMap>, +} + +impl Pictures<'_> { + /// A picture marker's image as a picture, or the note saying why it is drawn as a circle. + fn of(&mut self, s: &Value) -> Result { + let Some(b64) = s.get("imageData").and_then(Value::as_str).map(str::trim).filter(|b| !b.is_empty()) else { + // RustGIS doesn't fetch a marker's image from its `url`. + return Err(if s.get("url").and_then(Value::as_str).is_some_and(|u| !u.trim().is_empty()) { + "picture markers whose image is only at a URL, drawn as circles" + } else { + "picture markers with no image, drawn as circles" + }); + }; + // Looked up first, so that classes sharing an image don't each copy it. + if let Some(met) = self.met.get(b64) { + return *met; + } + *self.met.entry(b64.to_owned()).or_insert_with(|| { + // Too long to keep is said before decoding it. What is decoded is bounded by the + // description's own cap (`DESCRIPTION_MAX`, 4 MiB), as `layer.marker` decodes one. + if b64.len() > MAX_PICTURE_BYTES { + return Err("picture markers past 2 MiB of base64, drawn as circles"); + } + if !(self.readable)(b64) { + return Err("picture markers whose image isn't a PNG or JPEG RustGIS reads, drawn as circles"); + } + // Past the images a run keeps (from a crafted service), it isn't kept. + Some(Picture::new(b64)).filter(|p| !p.is_default()).ok_or("picture markers past the images RustGIS keeps, drawn as circles") + }) + } +} + /// Gives `layer`, a feature layer just added from an ArcGIS service, the symbology its /// description's `drawingInfo` sets: its renderer, when RustGIS draws its kind (a simple one, by -/// unique values of a field or by class breaks), and its transparency. What can't be drawn as the +/// unique values of a field or by class breaks), and its transparency. A picture marker draws its +/// image when `readable` (the renderer's check) says it decodes. What can't be drawn as the /// service draws it is noted, its labels too; a renderer RustGIS doesn't read leaves the layer's /// own. Anything but an object changes nothing. -pub fn apply(layer: &mut Layer, drawing_info: &Value, notes: &mut Notes) { +pub fn apply(layer: &mut Layer, drawing_info: &Value, readable: &dyn Fn(&str) -> bool, notes: &mut Notes) { if !drawing_info.is_object() || layer.is_raster() { return; } @@ -76,18 +117,19 @@ pub fn apply(layer: &mut Layer, drawing_info: &Value, notes: &mut Notes) { if g == GeometryType::None { return; } + let pictures = &mut Pictures { readable, met: HashMap::new() }; match renderer.get("type").and_then(Value::as_str) { Some("simple") if renderer.get("symbol").is_none_or(Value::is_null) => { notes.add("its renderer names no symbol, drawn with RustGIS's single symbol"); } Some("simple") => { - if let Some((symbol, dash)) = symbol(renderer.get("symbol").unwrap_or(&Value::Null), g, None, notes) { + if let Some((symbol, dash)) = symbol(renderer.get("symbol").unwrap_or(&Value::Null), g, None, pictures, notes) { layer.renderer = Renderer::Single(symbol); layer.line_strokes = dash.map(strokes).unwrap_or_default(); } } - Some("uniqueValue") => unique(layer, renderer, g, notes), - Some("classBreaks") => class_breaks(layer, renderer, g, notes), + Some("uniqueValue") => unique(layer, renderer, g, pictures, notes), + Some("classBreaks") => class_breaks(layer, renderer, g, pictures, notes), _ => notes.add("its renderer, of a kind RustGIS doesn't read, drawn with RustGIS's single symbol"), } if renderer.get("visualVariables").and_then(Value::as_array).is_some_and(|v| !v.is_empty()) { @@ -102,7 +144,7 @@ pub fn apply(layer: &mut Layer, drawing_info: &Value, notes: &mut Notes) { /// for each value it lists, with its label, and its default symbol (`defaultSymbol`, with /// `defaultLabel`) for the rest, which draw nothing without one, as ArcGIS draws them. One by /// several fields or by an expression leaves the layer's own renderer, noted. -fn unique(layer: &mut Layer, r: &Value, g: GeometryType, notes: &mut Notes) { +fn unique(layer: &mut Layer, r: &Value, g: GeometryType, pictures: &mut Pictures, notes: &mut Notes) { let Some((fi, field)) = class_field(layer, r, "field1", notes) else { return }; if ["field2", "field3"].iter().any(|k| r.get(k).and_then(Value::as_str).is_some_and(|f| !f.trim().is_empty())) { notes.add("its renderer by the values of several fields, drawn with RustGIS's single symbol"); @@ -130,7 +172,7 @@ fn unique(layer: &mut Layer, r: &Value, g: GeometryType, notes: &mut Notes) { } seen.insert(value.clone()); let color = ramp.pick(classes.len(), n); - let (symbol, dash) = class_symbol(info.get("symbol"), g, Some(color), color, notes); + let (symbol, dash) = class_symbol(info.get("symbol"), g, Some(color), color, pictures, notes); if let Some(label) = label(info, "label", notes).filter(|l| *l != value) { class_labels.insert(value.clone(), label); } @@ -145,9 +187,10 @@ fn unique(layer: &mut Layer, r: &Value, g: GeometryType, notes: &mut Notes) { } // Its label only with a symbol to show it by. let (other, other_strokes, other_label) = match r.get("defaultSymbol").filter(|s| !s.is_null()) { - // A picture marker of its own is grey, as a simple renderer's: there is no ramp to take. + // A picture marker of its own that can't be drawn is grey, as a simple renderer's: there + // is no ramp to take. Some(s) => { - let (symbol, dash) = class_symbol(Some(s), g, None, OTHER_FILL, notes); + let (symbol, dash) = class_symbol(Some(s), g, None, OTHER_FILL, pictures, notes); (symbol, dash.map(strokes).unwrap_or_default(), label(r, "defaultLabel", notes)) } None => { @@ -189,7 +232,7 @@ fn unique(layer: &mut Layer, r: &Value, g: GeometryType, notes: &mut Notes) { /// outside every class or none (ArcGIS draws them with its default symbol, or not at all), gaps /// between classes, a background fill. One by a normalised value or an expression leaves the /// layer's own renderer, noted. -fn class_breaks(layer: &mut Layer, r: &Value, g: GeometryType, notes: &mut Notes) { +fn class_breaks(layer: &mut Layer, r: &Value, g: GeometryType, pictures: &mut Pictures, notes: &mut Notes) { if r.get("normalizationType").and_then(Value::as_str).is_some_and(|t| t != "esriNormalizeByNone") { notes.add("its renderer by class breaks of a normalised value, drawn with RustGIS's single symbol"); return; @@ -259,7 +302,7 @@ fn class_breaks(layer: &mut Layer, r: &Value, g: GeometryType, notes: &mut Notes let (mut symbols, mut class_labels, mut class_strokes) = (Vec::new(), BTreeMap::new(), BTreeMap::new()); for (i, (_, info)) in infos.iter().enumerate() { let color = ramp.pick(i, n); - let (symbol, dash) = class_symbol(info.get("symbol"), g, Some(color), color, notes); + let (symbol, dash) = class_symbol(info.get("symbol"), g, Some(color), color, pictures, notes); if let Some(label) = label(info, "label", notes) { class_labels.insert(i, label); } @@ -335,10 +378,17 @@ fn label(v: &Value, key: &str, notes: &mut Notes) -> Option { Some(label).filter(|l| !l.is_empty()) } -/// A class's symbol for a `g` layer and its line's dash pattern: its own; a picture marker as a -/// circle in `picture`, a ramp's colour so that its classes tell apart (grey without one); one -/// RustGIS can't draw, or none, as its own symbol in `color` (noted). -fn class_symbol(s: Option<&Value>, g: GeometryType, picture: Option, color: Rgba, notes: &mut Notes) -> (Symbol, Option) { +/// A class's symbol for a `g` layer and its line's dash pattern: its own; a picture marker whose +/// image can't be drawn as a circle in `picture`, a ramp's colour so that its classes tell apart +/// (grey without one); one RustGIS can't draw, or none, as its own symbol in `color` (noted). +fn class_symbol( + s: Option<&Value>, + g: GeometryType, + picture: Option, + color: Rgba, + pictures: &mut Pictures, + notes: &mut Notes, +) -> (Symbol, Option) { let own = || { let base = Symbol::default_for(g, 0); let symbol = match g { @@ -352,7 +402,7 @@ fn class_symbol(s: Option<&Value>, g: GeometryType, picture: Option, color notes.add("classes with no symbol, drawn in a ramp's colours"); return own(); }; - symbol(s, g, picture, notes).unwrap_or_else(own) + symbol(s, g, picture, pictures, notes).unwrap_or_else(own) } /// A dashed line's stacked stroke, which takes its colour and width from the renderer. @@ -361,9 +411,10 @@ fn strokes(dash: Dash) -> Vec { } /// An ArcGIS symbol as a RustGIS symbol for a `g` layer, with a line's dash pattern; `None` (noted) -/// for a symbol that isn't for that geometry or that RustGIS can't draw. A picture marker is a -/// circle in `picture`, a ramp's colour, or grey without one. -fn symbol(s: &Value, g: GeometryType, picture: Option, notes: &mut Notes) -> Option<(Symbol, Option)> { +/// for a symbol that isn't for that geometry or that RustGIS can't draw. A picture marker draws +/// its image at its width; one whose image can't be drawn is a circle in `picture`, a ramp's +/// colour, or grey without one. +fn symbol(s: &Value, g: GeometryType, picture: Option, pictures: &mut Pictures, notes: &mut Notes) -> Option<(Symbol, Option)> { let kind = s.get("type").and_then(Value::as_str).unwrap_or_default(); let base = Symbol::default_for(g, 0); match (kind, g) { @@ -425,17 +476,32 @@ fn symbol(s: &Value, g: GeometryType, picture: Option, notes: &mut Notes) Some((Symbol { fill, stroke, stroke_width, size: size(s.get("size"), base.size, notes), marker }, None)) } ("esriPMS", GeometryType::Point) => { - let fill = match picture { - Some(c) => { - notes.add("picture markers, drawn as circles in a ramp's colours"); - c - } - None => { - notes.add("picture markers, drawn as grey circles"); - PICTURE_FILL + if offset_or_angle(s) { + notes.add("markers' angles and offsets, left out"); + } + // The fill is the circle drawn should the image not decode after all. + let fill = picture.unwrap_or(PICTURE_FILL); + match pictures.of(s) { + // Sized by its width, as a picture marker is: its height follows the image's own + // proportion, which a service draws its symbol at. Without a width above 0, its + // height stands in as its width, said, since an image that isn't square is then + // drawn that wide, not that tall; with neither, RustGIS's size does. + Ok(p) => { + let wide = s.get("width").and_then(Value::as_f64).is_some_and(|w| w.is_finite() && w > 0.0); + if !wide { + notes.add(if s.get("height").is_some_and(|h| !h.is_null()) { + "picture markers with no width, drawn with their height as their width" + } else { + "picture markers with no size, drawn at RustGIS's" + }); + } + let size = size(s.get(if wide { "width" } else { "height" }), base.size, notes); + return Some((Symbol { fill, size, marker: Marker::Picture(p), ..base }, None)); } - }; - // The larger of its width and height, so that one past the largest is said once. + Err(why) => notes.add(why), + } + // As a circle, the larger of its width and height, so that one past the largest is + // said once. let points = |k: &str| s.get(k).and_then(Value::as_f64).filter(|v| v.is_finite()).unwrap_or(f64::NEG_INFINITY); let larger = if points("height") > points("width") { s.get("height") } else { s.get("width") }; let size = size(larger, base.size, notes); @@ -586,10 +652,15 @@ mod tests { Layer::new(1, Dataset::new("l", g, vec![Field::new("NAME", FieldType::Text)])) } + /// The renderer's check stood in for (`map` doesn't decode images): a PNG's base64 begins so. + fn png(b64: &str) -> bool { + b64.starts_with("iVBORw0KGgo") + } + fn styled(g: GeometryType, info: Value) -> (Layer, Notes) { let mut l = layer(g); let mut notes = Notes::default(); - apply(&mut l, &info, &mut notes); + apply(&mut l, &info, &png, &mut notes); (l, notes) } @@ -674,13 +745,60 @@ mod tests { } #[test] - fn a_picture_marker_is_a_grey_circle_of_its_size_and_said() { - let info = - json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": "iVBORw0KGgo=", "width": 15, "height": 18}}}); + fn a_picture_marker_draws_its_image_at_its_width() { + let image = "iVBORw0KGgoSIMPLE="; + let info = json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": image, "contentType": "image/png", + "width": 15, "height": 18}}}); + let (l, notes) = styled(GeometryType::Point, info); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + assert_eq!((s.marker, s.fill, s.size), (Marker::Picture(Picture::new(image)), PICTURE_FILL, 15.0)); + assert!(notes.is_empty(), "{}", notes.text()); + // Without a width above 0, its height sizes it. + for width in [json!(null), json!(0)] { + let info = + json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": image, "width": width, "height": 18}}}); + let (l, notes) = styled(GeometryType::Point, info); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + assert_eq!(s.size, 18.0, "width {width}"); + assert_eq!(notes.text(), "picture markers with no width, drawn with their height as their width"); + } + // With neither, RustGIS's size. + let info = json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": image}}}); let (l, notes) = styled(GeometryType::Point, info); let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; - assert_eq!((s.marker, s.fill, s.size), (Marker::Circle, PICTURE_FILL, 18.0)); - assert_eq!(notes.text(), "picture markers, drawn as grey circles"); + let Renderer::Single(base) = layer(GeometryType::Point).renderer else { panic!("a single symbol") }; + assert_eq!(s.size, base.size); + assert_eq!(notes.text(), "picture markers with no size, drawn at RustGIS's"); + // Turned or shifted, it is drawn upright on its point, said. + let info = json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": image, "width": 15, "angle": 30}}}); + let (l, notes) = styled(GeometryType::Point, info); + assert!(matches!(l.renderer, Renderer::Single(Symbol { marker: Marker::Picture(_), .. }))); + assert_eq!(notes.text(), "markers' angles and offsets, left out"); + } + + #[test] + fn a_picture_marker_rustgis_cant_draw_is_a_grey_circle_of_its_size_and_said_why() { + let circle = |symbol: Value| { + let (l, notes) = styled(GeometryType::Point, json!({"renderer": {"type": "simple", "symbol": symbol}})); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + assert_eq!((s.marker, s.fill, s.size), (Marker::Circle, PICTURE_FILL, 18.0)); + notes.text() + }; + let gif = "R0lGODlhAQABAIAAAP///wAAACw="; + let long = format!("iVBORw0KGgo{}", "A".repeat(MAX_PICTURE_BYTES)); + for (image, why) in [ + (json!(null), "picture markers with no image, drawn as circles"), + (json!(""), "picture markers with no image, drawn as circles"), + (json!(12), "picture markers with no image, drawn as circles"), + (json!(gif), "picture markers whose image isn't a PNG or JPEG RustGIS reads, drawn as circles"), + (json!(long), "picture markers past 2 MiB of base64, drawn as circles"), + ] { + assert_eq!(circle(json!({"type": "esriPMS", "imageData": image, "width": 15, "height": 18})), why); + } + // Its image only at its `url`, which RustGIS doesn't fetch. + let url = json!({"type": "esriPMS", "url": "4d1f6b3c", "width": 15, "height": 18}); + assert_eq!(circle(url), "picture markers whose image is only at a URL, drawn as circles"); + assert_eq!(Picture::kept(gif), None, "an image that isn't drawn isn't kept"); } #[test] @@ -823,12 +941,15 @@ mod tests { let info = json!({"renderer": {"type": "simple", "symbol": {"type": "esriSMS", "color": [255, 0, 0, 0], "size": 0, "outline": {"color": [0, 0, 0], "width": 0}}}, "transparency": 100}); assert!(styled(GeometryType::Point, info).1.is_empty()); - // A picture marker larger than the largest both ways is said once. - let info = json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "width": 100, "height": 120}}}); - let (l, notes) = styled(GeometryType::Point, info); - let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; - assert_eq!(s.size, MAX_MARKER_SIZE); - assert_eq!(notes.text(), "picture markers, drawn as grey circles; marker sizes past RustGIS's largest (80 points), drawn at it"); + // A picture marker larger than the largest both ways is said once, drawn as its image or not. + for (image, why) in [(json!("iVBORw0KGgoLARGE="), ""), (json!(null), "picture markers with no image, drawn as circles; ")] { + let info = + json!({"renderer": {"type": "simple", "symbol": {"type": "esriPMS", "imageData": image, "width": 100, "height": 120}}}); + let (l, notes) = styled(GeometryType::Point, info); + let Renderer::Single(s) = l.renderer else { panic!("a single symbol") }; + assert_eq!(s.size, MAX_MARKER_SIZE); + assert_eq!(notes.text(), format!("{why}marker sizes past RustGIS's largest (80 points), drawn at it")); + } // Not an object: nothing changes. for info in [json!(null), json!([]), json!("simple"), json!({"renderer": null})] { let (l, notes) = styled(GeometryType::Point, info); @@ -855,7 +976,7 @@ mod tests { fn styled_rows(mut l: Layer, info: Value) -> (Layer, Notes) { let mut notes = Notes::default(); - apply(&mut l, &info, &mut notes); + apply(&mut l, &info, &png, &mut notes); (l, notes) } @@ -905,18 +1026,42 @@ mod tests { assert_eq!(class_value(&json!("1.0"), FieldType::Text).as_deref(), Some("1.0")); } + /// Each class draws its own image, each image checked once however many classes show it. #[test] - fn picture_marker_classes_are_circles_in_a_ramps_colours() { - let picture = json!({"type": "esriPMS", "imageData": "iVBORw0KGgo=", "width": 11, "height": 12}); + fn picture_marker_classes_draw_their_own_images() { + let (school, hospital) = ("iVBORw0KGgoSCHOOL=", "iVBORw0KGgoHOSPITAL="); + let picture = |image: &str| json!({"type": "esriPMS", "imageData": image, "width": 11, "height": 12}); let info = json!({"renderer": {"type": "uniqueValue", "field1": "NAME", "uniqueValueInfos": [ - {"value": "1", "label": "Distrital", "symbol": picture.clone()}, {"value": "2", "label": "Privado", "symbol": picture}]}}); - let (l, notes) = styled(GeometryType::Point, info); + {"value": "1", "label": "Escuela", "symbol": picture(school)}, {"value": "2", "label": "Hospital", "symbol": picture(hospital)}, + {"value": "3", "label": "Otra escuela", "symbol": picture(school)}]}}); + let checked = std::cell::Cell::new(0); + let counted = |b64: &str| { + checked.set(checked.get() + 1); + png(b64) + }; + let (mut l, mut notes) = (layer(GeometryType::Point), Notes::default()); + apply(&mut l, &info, &counted, &mut notes); + assert_eq!(checked.get(), 2, "the school's image once"); let Renderer::Unique { classes, .. } = &l.renderer else { panic!("{:?}", l.renderer) }; - let fills: Vec = classes.iter().map(|c| c.1.fill).collect(); + let markers: Vec = classes.iter().map(|c| c.1.marker).collect(); + let (school, hospital) = (Marker::Picture(Picture::new(school)), Marker::Picture(Picture::new(hospital))); + assert_eq!(markers, [school, hospital, school]); + assert!(classes.iter().all(|c| c.1.size == 11.0)); + assert!(notes.is_empty(), "{}", notes.text()); + } + + #[test] + fn picture_marker_classes_rustgis_cant_draw_are_circles_in_a_ramps_colours() { + let picture = json!({"type": "esriPMS", "url": "4d1f6b3c", "width": 11, "height": 12}); + let info = json!({"renderer": {"type": "classBreaks", "field": "CODE", "classBreakInfos": [ + {"classMaxValue": 1, "symbol": picture.clone()}, {"classMaxValue": 2, "symbol": picture}]}}); + let (l, notes) = styled_rows(with_rows(GeometryType::Point, FieldType::Integer, &[]), info); + let Renderer::Graduated { symbols, .. } = &l.renderer else { panic!("{:?}", l.renderer) }; + let fills: Vec = symbols.iter().map(|s| s.fill).collect(); assert_eq!(fills, [Ramp::default().pick(0, 2), Ramp::default().pick(1, 2)]); assert_ne!(fills.first(), fills.get(1), "the classes tell apart"); - assert!(classes.iter().all(|c| c.1.marker == Marker::Circle && c.1.size == 12.0)); - assert_eq!(notes.text(), "picture markers, drawn as circles in a ramp's colours (2 symbols)"); + assert!(symbols.iter().all(|s| s.marker == Marker::Circle && s.size == 12.0)); + assert_eq!(notes.text(), "picture markers whose image is only at a URL, drawn as circles (2 symbols)"); } #[test] @@ -1109,8 +1254,16 @@ mod tests { "uniqueValueInfos": [{"value": "a", "symbol": {"type": "esriSMS", "color": [1, 2, 3, 255]}}], "defaultSymbol": picture}}); let (l, notes) = styled(GeometryType::Point, info); let Renderer::Unique { other, .. } = &l.renderer else { panic!("{:?}", l.renderer) }; - assert_eq!(other.fill, PICTURE_FILL); - assert_eq!(notes.text(), "picture markers, drawn as grey circles"); + assert_eq!((other.marker, other.fill), (Marker::Circle, PICTURE_FILL)); + assert_eq!(notes.text(), "picture markers with no image, drawn as circles"); + // With its image, it draws it. + let picture = json!({"type": "esriPMS", "imageData": "iVBORw0KGgoOTHER=", "width": 10}); + let info = json!({"renderer": {"type": "uniqueValue", "field1": "NAME", + "uniqueValueInfos": [{"value": "a", "symbol": {"type": "esriSMS", "color": [1, 2, 3, 255]}}], "defaultSymbol": picture}}); + let (l, notes) = styled(GeometryType::Point, info); + let Renderer::Unique { other, .. } = &l.renderer else { panic!("{:?}", l.renderer) }; + assert_eq!(other.marker, Marker::Picture(Picture::new("iVBORw0KGgoOTHER="))); + assert!(notes.is_empty(), "{}", notes.text()); } #[test] diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 7c24596b67..3d85068e98 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -326,7 +326,7 @@ Scripts use `service.import {"url":"…","name":"My OGC Layer"}` and | OGC API Features | Collections | Their items downloaded as GeoJSON, following the service's `next` pages up to 100,000 features, or more on a computer with more memory (see Environments in the analysis guide; a command such as `service.add` waits a minute at most) | | OGC API Tiles | Collections' map and vector tiles; a landing page's dataset map tiles | A tile layer in WebMercatorQuad, from the tile template the tileset advertises (its `item` link; `…/tiles/WebMercatorQuad/{z}/{y}/{x}`, the usual path, when that can't be read), keeping the tiles link's query (a token), map tiles in the server's image format, vector tiles drawn layer by layer in colours of their own | | WCS 1.0 / 2.0 (a 1.1 service is asked in 1.0) | Coverages, with their bounds | A raster: GetCoverage of the map's view within the coverage's bounds (or its whole extent: `"bbox": "full"`), as a GeoTIFF in EPSG:4326, at most 2048 pixels on a side | -| ArcGIS REST | FeatureServer and MapServer layers; a MapServer's map; an ImageServer; a VectorTileServer | Layers queried as GeoJSON, or in protocol buffers (`f=pbf`, smaller and quicker to read) when the layer's description lists them and names its record limit (the description read first, or given up on after 15 seconds; a first protocol buffer page that can't be fetched or read has the layer read in GeoJSON, with a notice saying why), page by page past the server's record limit (`resultOffset`) up to 100,000 features or more on a computer with more memory (in protocol buffers, also no more than a quarter of the computer's memory once read, from 512 MB, the web app's limit, up to 3 GB; a later page past it ends the read with a notice, and the first page is always kept, within the reader's own limits for a page) (a command such as `service.add` waits a minute at most for the features), where the layer supports paging (ArcGIS Server 10.3 and later; otherwise up to the record limit); a layer comes in drawn as its description's `drawingInfo` says when its renderer is a simple one (fill, line and marker symbols, dashes), by the unique values of one field (as Unique Values, with the service's labels and its default symbol for the rest) or by class breaks (as Graduated Colors with the service's bounds and labels), others keeping RustGIS's symbol with a notice (a picture marker is a circle, grey or, among classes, in a ramp's colours), and with its transparency whatever its renderer (a description that can't be read, or a query asking for its own `resultOffset` or `resultRecordCount`, which skips it, keeps RustGIS's symbol with a notice saying why); a map as its tile cache when it is Web Mercator's pyramid, else exported images; an ImageServer as exported images; a VectorTileServer as its MapLibre style | +| ArcGIS REST | FeatureServer and MapServer layers; a MapServer's map; an ImageServer; a VectorTileServer | Layers queried as GeoJSON, or in protocol buffers (`f=pbf`, smaller and quicker to read) when the layer's description lists them and names its record limit (the description read first, or given up on after 15 seconds; a first protocol buffer page that can't be fetched or read has the layer read in GeoJSON, with a notice saying why), page by page past the server's record limit (`resultOffset`) up to 100,000 features or more on a computer with more memory (in protocol buffers, also no more than a quarter of the computer's memory once read, from 512 MB, the web app's limit, up to 3 GB; a later page past it ends the read with a notice, and the first page is always kept, within the reader's own limits for a page) (a command such as `service.add` waits a minute at most for the features), where the layer supports paging (ArcGIS Server 10.3 and later; otherwise up to the record limit); a layer comes in drawn as its description's `drawingInfo` says when its renderer is a simple one (fill, line and marker symbols, dashes), by the unique values of one field (as Unique Values, with the service's labels and its default symbol for the rest) or by class breaks (as Graduated Colors with the service's bounds and labels), others keeping RustGIS's symbol with a notice; a picture marker draws its image at its width, its height following the image's own proportion (without a width, its height is taken as its width, with a notice), and one whose image isn't in its description, or isn't a PNG or JPEG of at most 2 MiB of base64 that RustGIS can decode, is a circle, grey or, among classes, in a ramp's colours, with a notice; and with its transparency whatever its renderer (a description that can't be read, or a query asking for its own `resultOffset` or `resultRecordCount`, which skips it, keeps RustGIS's symbol with a notice saying why); a map as its tile cache when it is Web Mercator's pyramid, else exported images; an ImageServer as exported images; a VectorTileServer as its MapLibre style | A long list stops at its first 2,000 layers, and a note says how many the service has. An OGC API service stops at its first 2,000 collections instead, and each can make up to three layers