-
Notifications
You must be signed in to change notification settings - Fork 43
feat(rust/sedona-functions): Add raster display support to SD_Format #591
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
+278
−5
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f810dea
feat(sedona-functions): add raster display support to SD_Format
Kontinuation 88f92b2
test(sedona-functions): add null raster and width_hint truncation tes…
Kontinuation 5cfe013
refactor: move raster display logic to sedona-raster and show bounds+CRS
Kontinuation 6860718
Update rust/sedona-raster/src/display.rs
Kontinuation b2e7e2f
fix: remove RASTER prefix from display/test assertions to match forma…
Kontinuation 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,163 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| use crate::affine_transformation::to_world_coordinate; | ||
| use crate::traits::RasterRef; | ||
| use sedona_schema::raster::StorageType; | ||
|
|
||
| /// Wrapper for formatting a raster reference as a human-readable string. | ||
| /// | ||
| /// # Format | ||
| /// | ||
| /// Non-skewed rasters: | ||
| /// ```text | ||
| /// [WxH/nbands] @ [xmin ymin xmax ymax] / CRS | ||
| /// ``` | ||
| /// | ||
| /// Skewed rasters (includes skew parameters): | ||
| /// ```text | ||
| /// [WxH/nbands] @ [xmin ymin xmax ymax] skew=(skew_x, skew_y) / CRS | ||
| /// ``` | ||
| /// | ||
| /// With outdb bands: | ||
| /// ```text | ||
| /// [WxH/nbands] @ [xmin ymin xmax ymax] / CRS <outdb> | ||
| /// ``` | ||
| /// | ||
| /// Without CRS: | ||
| /// ```text | ||
| /// [WxH/nbands] @ [xmin ymin xmax ymax] | ||
| /// ``` | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```text | ||
| /// [64x32/3] @ [43.08 79.07 171.08 143.07] / OGC:CRS84 | ||
| /// [3x4/1] @ [3 2.4 3.84 4.24] skew=(0.06, 0.08) / EPSG:2193 | ||
| /// [10x10/1] @ [0 0 10 10] / OGC:CRS84 <outdb> | ||
| /// ``` | ||
| pub struct RasterDisplay<'a>(pub &'a dyn RasterRef); | ||
|
|
||
| impl fmt::Display for RasterDisplay<'_> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let raster = self.0; | ||
| let metadata = raster.metadata(); | ||
| let bands = raster.bands(); | ||
|
|
||
| let width = metadata.width(); | ||
| let height = metadata.height(); | ||
| let nbands = bands.len(); | ||
|
|
||
| // Compute axis-aligned bounding box from 4 corners in world coordinates. | ||
| // This handles both skewed and non-skewed rasters correctly. | ||
| let w = width as i64; | ||
| let h = height as i64; | ||
| let (ulx, uly) = to_world_coordinate(raster, 0, 0); | ||
| let (urx, ury) = to_world_coordinate(raster, w, 0); | ||
| let (lrx, lry) = to_world_coordinate(raster, w, h); | ||
| let (llx, lly) = to_world_coordinate(raster, 0, h); | ||
|
|
||
| let xmin = ulx.min(urx).min(lrx).min(llx); | ||
| let xmax = ulx.max(urx).max(lrx).max(llx); | ||
| let ymin = uly.min(ury).min(lry).min(lly); | ||
| let ymax = uly.max(ury).max(lry).max(lly); | ||
|
|
||
| let skew_x = metadata.skew_x(); | ||
| let skew_y = metadata.skew_y(); | ||
| let has_skew = skew_x != 0.0 || skew_y != 0.0; | ||
|
|
||
| let has_outdb = bands | ||
| .iter() | ||
| .any(|band| matches!(band.metadata().storage_type(), Ok(StorageType::OutDbRef))); | ||
|
|
||
| // Write: [WxH/nbands] @ [xmin ymin xmax ymax] | ||
| write!( | ||
| f, | ||
| "[{width}x{height}/{nbands}] @ [{xmin} {ymin} {xmax} {ymax}]" | ||
| )?; | ||
|
|
||
| // Conditionally append skew info when the raster is rotated/skewed | ||
| if has_skew { | ||
| write!(f, " skew=({skew_x}, {skew_y})")?; | ||
| } | ||
|
|
||
| // Append CRS if present. For PROJJSON (starts with '{'), show compact placeholder. | ||
| if let Some(crs) = raster.crs() { | ||
| if crs.starts_with('{') { | ||
| write!(f, " / {{...}}")?; | ||
| } else { | ||
| write!(f, " / {crs}")?; | ||
| } | ||
| } | ||
|
|
||
| if has_outdb { | ||
| write!(f, " <outdb>")?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::array::RasterStructArray; | ||
| use sedona_testing::rasters::generate_test_rasters; | ||
|
|
||
| #[test] | ||
| fn display_non_skewed_raster() { | ||
| // i=0: w=1, h=2, scale=(0.1, -0.2), skew=(0, 0), CRS=OGC:CRS84 | ||
| // Bounds: xmin=1, ymin=1.6, xmax=1.1, ymax=2 | ||
| let rasters = generate_test_rasters(1, None).unwrap(); | ||
| let raster_array = RasterStructArray::new(&rasters); | ||
| let raster = raster_array.get(0).unwrap(); | ||
|
|
||
| let display = format!("{}", RasterDisplay(&raster)); | ||
| assert_eq!(display, "[1x2/1] @ [1 1.6 1.1 2] / OGC:CRS84"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_skewed_raster() { | ||
| // i=2: w=3, h=4, scale=(0.2, -0.4), skew=(0.06, 0.08), CRS=OGC:CRS84 | ||
| // Corners: (3,4), (3.6,4.24), (3.84,2.64), (3.24,2.4) | ||
| // AABB: xmin=3, ymin=2.4, xmax=3.84, ymax=4.24 | ||
| let rasters = generate_test_rasters(3, None).unwrap(); | ||
| let raster_array = RasterStructArray::new(&rasters); | ||
| let raster = raster_array.get(2).unwrap(); | ||
|
|
||
| let display = format!("{}", RasterDisplay(&raster)); | ||
| assert_eq!( | ||
| display, | ||
| "[3x4/1] @ [3 2.4 3.84 4.24] skew=(0.06, 0.08) / OGC:CRS84" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_write_to_fmt_write() { | ||
| // Verify RasterDisplay works with any fmt::Write target (e.g., String) | ||
| let rasters = generate_test_rasters(1, None).unwrap(); | ||
| let raster_array = RasterStructArray::new(&rasters); | ||
| let raster = raster_array.get(0).unwrap(); | ||
|
|
||
| let mut buf = String::new(); | ||
| use std::fmt::Write; | ||
| write!(buf, "{}", RasterDisplay(&raster)).unwrap(); | ||
| assert_eq!(buf, "[1x2/1] @ [1 1.6 1.1 2] / OGC:CRS84"); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -18,4 +18,5 @@ | |
| pub mod affine_transformation; | ||
| pub mod array; | ||
| pub mod builder; | ||
| pub mod display; | ||
| pub mod traits; | ||
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.