Skip to content

Commit 89808b8

Browse files
committed
feat(rust/sedona-testing): Add test helpers for loading raster test data
Add test_raster() function in data.rs for resolving paths to raster test files in the sedona-testing submodule, and raster_from_single_band() in rasters.rs for programmatically building minimal single-band rasters in unit tests. Update sedona-testing submodule to include raster test data files (test1.tiff, test3.tif, test4.tiff, test5.tiff).
1 parent 963cd94 commit 89808b8

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

rust/sedona-testing/src/data.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,33 @@ pub fn sedona_testing_dir() -> Result<String> {
125125
)
126126
}
127127

128+
/// Get the path to a raster test file from the sedona-testing data directory.
129+
///
130+
/// # Arguments
131+
/// * `name` - The name of the raster file (e.g., "test1.tiff", "test4.tiff")
132+
///
133+
/// # Returns
134+
/// The full path to the raster test file if it exists.
135+
///
136+
/// # Example
137+
/// ```ignore
138+
/// let path = test_raster("test4.tiff")?;
139+
/// ```
140+
pub fn test_raster(name: &str) -> Result<String> {
141+
let base = sedona_testing_dir()?;
142+
let path = format!("{}/data/raster/{}", base, name);
143+
if fs::exists(&path)? {
144+
Ok(path)
145+
} else {
146+
sedona_internal_err!(
147+
"sedona-testing raster file '{}' not found at '{}', \
148+
run submodules/download-assets.py or check the file name",
149+
name,
150+
path
151+
)
152+
}
153+
}
154+
128155
#[cfg(test)]
129156
mod test {
130157
use super::*;
@@ -172,4 +199,18 @@ mod test {
172199
env::remove_var("SEDONA_TESTING_DIR");
173200
assert!(maybe_dir.is_ok());
174201
}
202+
203+
#[test]
204+
fn test_raster_resolves() {
205+
// Test that test_raster can find existing raster files
206+
let path = test_raster("test4.tiff");
207+
assert!(path.is_ok(), "Failed to find test4.tiff: {:?}", path.err());
208+
let path_str = path.unwrap();
209+
assert!(path_str.ends_with("test4.tiff"));
210+
assert!(fs::exists(&path_str).unwrap());
211+
212+
// Test that non-existent files return an error
213+
let err = test_raster("nonexistent.tiff");
214+
assert!(err.is_err());
215+
}
175216
}

rust/sedona-testing/src/rasters.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,43 @@ pub fn build_noninvertible_raster() -> StructArray {
184184
builder.finish().expect("finish")
185185
}
186186

187+
/// Builds a single-band raster from raw bytes for tests.
188+
pub fn raster_from_single_band(
189+
width: usize,
190+
height: usize,
191+
data_type: BandDataType,
192+
band_bytes: &[u8],
193+
crs: Option<&str>,
194+
) -> StructArray {
195+
let mut builder = RasterBuilder::new(1);
196+
let metadata = RasterMetadata {
197+
width: width as u64,
198+
height: height as u64,
199+
upperleft_x: 0.0,
200+
upperleft_y: 0.0,
201+
scale_x: 1.0,
202+
scale_y: -1.0,
203+
skew_x: 0.0,
204+
skew_y: 0.0,
205+
};
206+
207+
builder.start_raster(&metadata, crs).expect("start raster");
208+
builder
209+
.start_band(BandMetadata {
210+
datatype: data_type,
211+
nodata_value: None,
212+
storage_type: StorageType::InDb,
213+
outdb_url: None,
214+
outdb_band_id: None,
215+
})
216+
.expect("start band");
217+
builder.band_data_writer().append_value(band_bytes);
218+
builder.finish_band().expect("finish band");
219+
builder.finish_raster().expect("finish raster");
220+
221+
builder.finish().expect("finish")
222+
}
223+
187224
/// Determine if this tile contains a corner of the overall grid and return its position
188225
/// Returns Some(position) if this tile contains a corner, None otherwise
189226
fn get_corner_position(

0 commit comments

Comments
 (0)