Skip to content
Closed
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
8 changes: 5 additions & 3 deletions crates/map/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ pub fn utm_epsg(lon: f64, lat: f64) -> u32 {
}

/// The WGS 84 UTM zone a coordinate system names by its EPSG code (`EPSG:32632`, `32632`, any
/// case): its zone and whether it is the northern hemisphere's. `None` for any other system, and
/// for a UTM zone given as WKT or a PROJ string, which MGRS labels don't read.
/// case, also `epsg 32632`): its zone and whether it is the northern hemisphere's. `None` for any
/// other system, and for a UTM zone given as WKT or a PROJ string, which MGRS labels don't read.
pub fn utm_zone_of(crs: &str) -> Option<(u8, bool)> {
let t = crs.trim();
let digits = match t.get(..5) {
Some(p) if p.eq_ignore_ascii_case("EPSG:") => t.get(5..)?.trim(),
Some(p) if p.eq_ignore_ascii_case("EPSG:") || p.eq_ignore_ascii_case("EPSG ") => t.get(5..)?.trim(),
_ => t,
};
if !(digits.len() == 5 && digits.bytes().all(|b| b.is_ascii_digit())) {
Expand Down Expand Up @@ -2050,6 +2050,8 @@ mod tests {
(" epsg:32760 ", Some((60, false))),
("32601", Some((1, true))),
("EPSG: 32733", Some((33, false))),
("epsg 32632", Some((32, true))),
(" EPSG 32733 ", Some((33, false))),
("EPSG:32600", None),
("EPSG:32661", None),
("EPSG:32761", None),
Expand Down
3 changes: 2 additions & 1 deletion crates/map/src/qgis/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn bookmarks(root: Node, report: &mut Report) -> Vec<ProjectBookmark> {
// Those whose rotation doesn't read, facing north.
let mut unturned = Vec::new();
let mut groups: Vec<&str> = Vec::new();
let mut seen_groups = std::collections::HashSet::new();
// Those with no name, named "Bookmark".
let mut unnamed = 0usize;
// Those whose own coordinate system is there but doesn't read (no code, no PROJ string):
Expand All @@ -66,7 +67,7 @@ pub fn bookmarks(root: Node, report: &mut Report) -> Vec<ProjectBookmark> {
continue;
}
if let Some(g) = b.attribute("group").map(str::trim).filter(|g| !g.is_empty())
&& !groups.contains(&g)
&& seen_groups.insert(g)
{
groups.push(g);
}
Expand Down
58 changes: 53 additions & 5 deletions crates/map/src/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,12 +2169,30 @@ impl RasterLayer {
self.masked_stats().is_some_and(|m| m.first().is_some_and(|s| s.count == 0))
}

/// Unique Values without an enumerated statistics list shows every configured value, just
/// as an explicit request for all classes does. None of those classes can be missed.
fn lists_every_class(&self, all_classes: bool) -> bool {
match &self.style.renderer {
RasterRenderer::UniqueValues { band, .. } => {
if all_classes {
return true;
}
let masked = self.masked_stats();
let stats = masked.as_ref().and_then(|m| m.first()).or_else(|| self.data.as_ref()?.bands.get(*band).map(|b| &b.stats));

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 (maintainability), not a bug: the "masked statistics first, else the band's own" selection is now written three times (classes_unknown around line 2162, the stats closure in legend_classes around line 2250, and here). lists_every_class has to pick the same statistics as legend_classes, or the rare-class warning can disagree with the legend it describes. A small shared helper would keep them in step, e.g. fn legend_stats(&self, band: usize) -> Option<Arc<Vec<BandStats>>> or a method returning Option<&BandStats> for the masked-or-band choice, used by all three sites.

stats.is_none_or(|s| s.categories.is_none())
}
RasterRenderer::Classify { .. } => all_classes,
_ => false,
}
}

/// Whether the classes a legend lists for it ([`RasterLayer::legend_classes`]) come from a
/// sample of its pixels: under a mask, a band of more than [`MASK_STATS_SAMPLE`] cells has its
/// statistics read from every few cells ([`RasterMask::stats`]), so a class too rare for the
/// sample can be missing (#1679). Not with `all_classes` for Unique Values or Classify, nor when
/// no class can be listed (a mask hiding every pixel included); a colour table's come from the sample only when its own statistics,
/// read whole, can't list its values.
/// no class can be listed (a mask hiding every pixel included). Unique Values without an
/// enumerated statistics list shows every class. A colour table's come from the sample only
/// when its own statistics, read whole, can't list its values.
pub fn classes_from_sample(&self, all_classes: bool) -> bool {
let Some(r) = &self.data else { return false };
if !self.style.renderer.draws_classes() || self.style.mask.is_empty() || self.classes_unknown() || self.mask_hides_all() {
Expand All @@ -2184,7 +2202,7 @@ impl RasterLayer {
let sampled = r.bands.get(band).is_some_and(|b| b.data.len() > MASK_STATS_SAMPLE);
let from_sample = match &self.style.renderer {
RasterRenderer::Colormap => r.bands.first().is_some_and(|b| b.stats.categories.is_none()),
_ => !all_classes,
_ => !self.lists_every_class(all_classes),
};
sampled && from_sample
}
Expand All @@ -2194,10 +2212,11 @@ impl RasterLayer {
/// while the map draws the full resolution: a class too rare for the overview can be missing
/// (#1679). Not with `all_classes` for Unique Values or Classify, whose every class is listed
/// whatever the statistics, nor when no class can be listed ([`RasterLayer::classes_unknown`],
/// or a mask hiding every pixel).
/// or a mask hiding every pixel), or when Unique Values lists every configured class because
/// its statistics have no enumerated values.
pub fn classes_from_overview(&self, all_classes: bool) -> bool {
let Some(r) = &self.data else { return false };
let every = all_classes && !matches!(self.style.renderer, RasterRenderer::Colormap);
let every = self.lists_every_class(all_classes);
self.style.renderer.draws_classes()
&& (self.in_place || r.overview.is_some())
&& !every
Expand Down Expand Up @@ -2472,6 +2491,35 @@ mod tests {
assert_eq!(rl.legend_classes(true), None);
}

/// Without an enumerated statistics list, Unique Values lists all configured classes; neither
/// an overview nor masked sampling can then hide a class from the legend.
#[test]
fn unique_values_listing_every_class_has_no_missing_class_warning() {
let side = 2049u32;
let gt = GeoTransform { origin_x: 0.0, origin_y: 1.0, pixel_width: 0.01, pixel_height: -0.01 };
let cells = (0..side * side).map(|i| (i % 1000) as f32).collect();
let raster = Raster::new("many", side, side, vec![cells], gt, Crs::LonLat, None, ColorHint::Gray, "UInt16").unwrap();
assert!(raster.bands[0].stats.categories.is_none());
let mut layer = RasterLayer::from_path(raster, "many.tif");
layer.style.renderer = RasterRenderer::UniqueValues {
band: 0,
ramp: Ramp::Harbour,
values: (0..1000).map(|v| (f64::from(v), Rgba::rgb(30, 100, 200))).collect(),
};
layer.in_place = true;
assert_eq!(layer.legend_classes(false).unwrap().len(), 1000);
assert!(!layer.classes_from_overview(false));
layer.style.mask = RasterMask { values: vec![999.0], ..RasterMask::default() };
assert_eq!(layer.legend_classes(false).unwrap().len(), 999);
assert!(!layer.classes_from_sample(false) && !layer.classes_from_overview(false));
// Once a sampled subset supplies the legend, the rare-class warning applies again.
layer.style.mask = RasterMask { min: Some(0.0), max: Some(3.0), ..RasterMask::default() };
// Sampling every second cell sees 0 and 2, missing the configured odd values.
assert_eq!(layer.legend_classes(false).unwrap().len(), 2);
assert!(layer.classes_from_sample(false) && layer.classes_from_overview(false));
assert!(!layer.classes_from_sample(true) && !layer.classes_from_overview(true));
}

/// A colour table has no entry for a negative value, and the map draws none: neither the
/// Layers pane nor a legend lists it (#1679), where `as usize` would have taken entry 0.
#[test]
Expand Down
Loading