Repository navigation
feat(layout): a profile's samples along its section line (#2863 part 2b) #3295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elpidioassinaturas
merged 3 commits into
opengeos:main
from
elpidioassinaturas:feat/2863-margin-profiles-2b
Nov 29, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7c645e5
feat(layout): a profile's samples along its section line (#2863 part 2b)
elpidiofilho d55a5b2
fix(layout): profile samples wrap 0-360° grids, refuse band 0 and lin…
elpidiofilho c8393ad
fix(layout): a profile across the antimeridian counts the cells it cr…
elpidiofilho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| //! A raster's profile beside its map frame (#2863 part 2): its samples along a section line, read | ||
| //! at each one's point of the page line through the frame's projection. | ||
|
|
||
| use rustgis_map::layout::{MAX_PROFILE_SAMPLES, Profile}; | ||
|
|
||
| use super::*; | ||
|
|
||
| /// What a profile reads along its section line ([`profile_samples`]). | ||
| #[derive(Clone, Debug, Default, PartialEq)] | ||
| pub struct ProfileSamples { | ||
| /// Each sample's place along the frame's side, in the frame's points from its left (or top). | ||
| pub along: Vec<f64>, | ||
| /// Each sample's metres along the ground from the section line's start (its A end), on the | ||
| /// WGS 84 ellipsoid, between one sample and the next. | ||
| pub metres: Vec<f64>, | ||
| /// The band's value at each sample; `None` where it has no data. | ||
| pub values: Vec<Option<f64>>, | ||
| /// The raster's cells along the section line, when there are more than the samples read | ||
| /// ([`MAX_PROFILE_SAMPLES`]). | ||
| pub cells: Option<usize>, | ||
| } | ||
|
|
||
| impl ProfileSamples { | ||
| /// The least and most of the values read, if any. | ||
| pub fn range(&self) -> Option<(f64, f64)> { | ||
| let mut it = self.values.iter().flatten().copied(); | ||
| let first = it.next()?; | ||
| Some(it.fold((first, first), |(lo, hi), v| (lo.min(v), hi.max(v)))) | ||
| } | ||
| } | ||
|
|
||
| /// How many points of the line a profile probes to count the raster's cells it crosses. | ||
| const CELL_PROBES: usize = 64; | ||
|
|
||
| /// A profile's frame, section line and raster in `layout` over `maps`, and what it reads along | ||
| /// the line: a sample per cell of the raster it crosses (at most [`MAX_PROFILE_SAMPLES`]), each at | ||
| /// its point of the line on the page, taken back through the frame's projection to the ground and | ||
| /// read bilinearly there ([`rustgis_data::raster::Raster::bilinear`]), with no smoothing. | ||
| /// `Err` says why it draws nothing. | ||
| pub fn profile_samples(layout: &Layout, maps: &[Map], p: &Profile) -> Result<(ProfileSamples, Section), String> { | ||
| let frame = layout.element(p.frame).ok_or("its map frame is missing")?; | ||
| let ElementKind::MapFrame { map, center, scale, bearing, pitch, shape, sections, .. } = &frame.kind else { | ||
| return Err("its map frame is missing".into()); | ||
| }; | ||
| let map = map_index(maps, map).and_then(|i| maps.get(i)).ok_or("its frame's map is missing")?; | ||
| let section = Section::find(sections, &p.section) | ||
| .and_then(|i| sections.get(i)) | ||
| .ok_or_else(|| format!("its frame has no section line '{}'", p.section))?; | ||
| if !p.side.suits(section.line) { | ||
| return Err(format!( | ||
| "a {} section line's profile goes {}, not {}", | ||
| section.line.code(), | ||
| if p.side.is_horizontal() { "left or right of its frame" } else { "under or over its frame" }, | ||
| p.side.code() | ||
| )); | ||
| } | ||
| let cam = frame_camera(map, *center, *scale, *bearing, *pitch); | ||
| if map.projection.is_globe() || cam.pitch.abs() > 1e-9 { | ||
| return Err("its frame is tilted or on the globe, where a line straight on the page isn't a section of the ground".into()); | ||
| } | ||
| let layer = map | ||
| .layer_by_name(&p.layer) | ||
| .filter(|l| l.is_raster()) | ||
| .ok_or_else(|| format!("the frame's map has no raster layer '{}'", p.layer))?; | ||
| let rl = layer.raster.as_ref().ok_or_else(|| format!("the frame's map has no raster layer '{}'", p.layer))?; | ||
| if rl.in_place || rl.tiled { | ||
| return Err(format!("its layer '{}' is read in place, a part at a time, not held whole", layer.name)); | ||
| } | ||
| let r = rl.data.as_ref().ok_or_else(|| format!("its layer '{}' has no pixels read", layer.name))?; | ||
| if r.overview.is_some() { | ||
| return Err(format!("its layer '{}' holds only an overview of its file", layer.name)); | ||
| } | ||
| if !r.georeferenced { | ||
| return Err(format!("its layer '{}' isn't placed on the ground", layer.name)); | ||
| } | ||
| let band = usize::try_from(p.band).ok().and_then(|b| b.checked_sub(1)).unwrap_or(usize::MAX); | ||
| if band >= r.bands.len() { | ||
| return Err(format!("its layer '{}' has no band {} (it has {})", layer.name, p.band, r.bands.len())); | ||
| } | ||
| let (fw, fh) = (frame.w * PT_PER_MM, frame.h * PT_PER_MM); | ||
| let [start, end] = section_ends(map, &cam, (fw, fh), shape, section.line) | ||
| .map_err(|why| format!("its section line '{}' isn't drawn: {why}", section.name))?; | ||
| // Along the line and across it: a vertical line is worked out with the axes swapped. | ||
| let vertical = matches!(section.line, SectionLine::Vertical(_)); | ||
| let swap = |(x, y): (f64, f64)| if vertical { (y, x) } else { (x, y) }; | ||
| let ((a, across), (b, _)) = (swap(start), swap(end)); | ||
| let lonlat = |along: f64| { | ||
| let (x, y) = swap((along, across)); | ||
| cam.try_to_map(x, y, fw, fh).and_then(|m| map.to_lonlat(m)).filter(|c| c.x.is_finite() && c.y.is_finite()) | ||
| }; | ||
| // The cells the line crosses, counted along it in the raster's own grid. | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| let cell = |c: Coord| r.transformer().from_lonlat(c).map(|w| r.transform.to_pixel(w)); | ||
|
elpidioassinaturas marked this conversation as resolved.
Resolved
|
||
| let probes: Vec<(f64, f64)> = | ||
| (0..=CELL_PROBES).filter_map(|i| lonlat(a + (b - a) * i as f64 / CELL_PROBES as f64).and_then(cell)).collect(); | ||
| // A geographic grid's columns either side of 180° are a turn apart: a step between them is | ||
| // the short way round. | ||
| let turn = r.transformer().is_geographic().then(|| 360.0 / r.transform.pixel_width.abs()); | ||
| let step = |d: f64| turn.map_or(d, |t| (d + t / 2.0).rem_euclid(t) - t / 2.0); | ||
| let crossed: f64 = probes.windows(2).map(|w| if let [p, q] = w { step(q.0 - p.0).hypot(q.1 - p.1) } else { 0.0 }).sum(); | ||
| let cells = if crossed.is_finite() { crossed.ceil() as usize } else { 0 }; | ||
| let n = cells.saturating_add(1).clamp(2, MAX_PROFILE_SAMPLES); | ||
| let mut out = ProfileSamples { cells: (cells.saturating_add(1) > MAX_PROFILE_SAMPLES).then_some(cells), ..ProfileSamples::default() }; | ||
| let (mut metres, mut last) = (0.0, None); | ||
| for i in 0..n { | ||
| let along = a + (b - a) * i as f64 / (n - 1) as f64; | ||
| // Off the ground there is no distance from A to measure. | ||
| let at = | ||
| lonlat(along).ok_or_else(|| format!("its section line '{}' runs off the ground of the frame's projection", section.name))?; | ||
| if let Some(p) = last { | ||
| metres += rustgis_geom::wgs84_distance_m(p, at); | ||
| } | ||
| last = Some(at); | ||
| out.along.push(along); | ||
| out.metres.push(metres); | ||
| out.values.push(r.bilinear(band, at)); | ||
| } | ||
| if out.values.iter().all(Option::is_none) { | ||
| return Err(format!("its layer '{}' has no data along section line '{}'", layer.name, section.name)); | ||
| } | ||
| Ok((out, section.clone())) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "profile_tests.rs"] | ||
| mod tests; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.