pub struct RasterBuffer { /* private fields */ }Expand description
A typed buffer for raster data
Implementations§
Source§impl RasterBuffer
impl RasterBuffer
Sourcepub fn to_float64_array(&self) -> Result<Float64Array>
pub fn to_float64_array(&self) -> Result<Float64Array>
Converts to an Arrow Float64Array
Source§impl RasterBuffer
impl RasterBuffer
Sourcepub fn new(
data: Vec<u8>,
width: u64,
height: u64,
data_type: RasterDataType,
nodata: NoDataValue,
) -> Result<Self>
pub fn new( data: Vec<u8>, width: u64, height: u64, data_type: RasterDataType, nodata: NoDataValue, ) -> Result<Self>
Creates a new raster buffer
§Errors
Returns an error if the data size doesn’t match the dimensions and type
Sourcepub fn zeros(width: u64, height: u64, data_type: RasterDataType) -> Self
pub fn zeros(width: u64, height: u64, data_type: RasterDataType) -> Self
Creates a zero-filled buffer
Sourcepub fn nodata_filled(
width: u64,
height: u64,
data_type: RasterDataType,
nodata: NoDataValue,
) -> Self
pub fn nodata_filled( width: u64, height: u64, data_type: RasterDataType, nodata: NoDataValue, ) -> Self
Creates a buffer filled with the nodata value
Sourcepub fn fill_value(&mut self, value: f64)
pub fn fill_value(&mut self, value: f64)
Fills the buffer with a constant value
Sourcepub const fn data_type(&self) -> RasterDataType
pub const fn data_type(&self) -> RasterDataType
Returns the data type
Sourcepub const fn nodata(&self) -> NoDataValue
pub const fn nodata(&self) -> NoDataValue
Returns the nodata value
Sourcepub const fn pixel_count(&self) -> u64
pub const fn pixel_count(&self) -> u64
Returns the total number of pixels
Sourcepub fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
pub fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
Returns mutable raw bytes
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the buffer and returns the raw bytes
Sourcepub fn from_typed_vec<T: Copy + 'static>(
width: usize,
height: usize,
data: Vec<T>,
data_type: RasterDataType,
) -> Result<Self>
pub fn from_typed_vec<T: Copy + 'static>( width: usize, height: usize, data: Vec<T>, data_type: RasterDataType, ) -> Result<Self>
Creates a buffer from typed vector data
The samples are copied in one bulk memcpy and keep their native
endianness.
§Arguments
width- Width in pixelsheight- Height in pixelsdata- Typed data (e.g.,Vec<f32>,Vec<u8>)data_type- The raster data type
§Type Parameters
T- A plain-old-data numeric type whose size matchesdata_type. Types with padding bytes (e.g.#[repr(C)]structs) must not be used; preferRasterBuffer::from_element_slice, which is restricted to the ten sealedRasterElementtypes and cannot be misused.
§Errors
Returns an error if the data size doesn’t match dimensions and type
Sourcepub fn from_element_slice<T: RasterElement>(
width: u64,
height: u64,
data: &[T],
) -> Result<Self>
pub fn from_element_slice<T: RasterElement>( width: u64, height: u64, data: &[T], ) -> Result<Self>
Creates a buffer from a slice of typed samples.
The type-safe counterpart of RasterBuffer::from_typed_vec: the data
type is derived from T, the samples are copied in one bulk memcpy,
and only the ten RasterElement types are accepted.
§Errors
Returns an error if data.len() differs from width * height.
§Examples
use oxigeo_core::buffer::RasterBuffer;
use oxigeo_core::types::RasterDataType;
let buffer = RasterBuffer::from_element_slice(2, 2, &[1.0f32, 2.0, 3.0, 4.0])?;
assert_eq!(buffer.data_type(), RasterDataType::Float32);
assert_eq!(buffer.get_pixel(1, 1)?, 4.0);Sourcepub fn as_slice<T: Copy + 'static>(&self) -> Result<&[T]>
pub fn as_slice<T: Copy + 'static>(&self) -> Result<&[T]>
Returns the buffer data as a typed slice (zero-copy)
§Type Parameters
T- The target type (must match the buffer’s data type size)
§Errors
Returns an error if the type size doesn’t match the data type, or if the
buffer’s storage is not aligned for T. Use
RasterBuffer::to_typed_vec or RasterBuffer::copy_to_slice when an
alignment-independent typed view is needed.
Sourcepub fn as_slice_mut<T: Copy + 'static>(&mut self) -> Result<&mut [T]>
pub fn as_slice_mut<T: Copy + 'static>(&mut self) -> Result<&mut [T]>
Sourcepub fn copy_to_slice<T: RasterElement>(&self, dst: &mut [T]) -> Result<()>
pub fn copy_to_slice<T: RasterElement>(&self, dst: &mut [T]) -> Result<()>
Converts every pixel into a caller-supplied slice, in one pass and without allocating.
This is the OxiGeo equivalent of GDAL’s
RasterBand::read_into_slice: the destination belongs to the caller
(a Vec<f64>, an ndarray::Array2<f64>’s backing slice, a
memory-mapped region…), so no temporary buffer and no second pass are
needed. Unlike RasterBuffer::as_slice it works for any destination
type and needs no alignment or data-type match.
When T already matches the buffer’s data type the copy is a plain
memcpy. See element for the exact conversion semantics; floats are
rounded with FloatToIntRounding::Nearest.
§Errors
Returns an error if dst.len() differs from RasterBuffer::pixel_count.
§Examples
use oxigeo_core::buffer::RasterBuffer;
use oxigeo_core::types::RasterDataType;
let mut buffer = RasterBuffer::zeros(3, 2, RasterDataType::UInt16);
buffer.set_pixel(2, 1, 40_000.0)?;
// Allocate once, reuse across tiles/bands: nothing else is allocated.
let mut pixels = vec![0.0f64; buffer.pixel_count() as usize];
buffer.copy_to_slice(&mut pixels)?;
assert_eq!(pixels.last().copied(), Some(40_000.0));Sourcepub fn copy_to_slice_with<T: RasterElement>(
&self,
dst: &mut [T],
rounding: FloatToIntRounding,
) -> Result<()>
pub fn copy_to_slice_with<T: RasterElement>( &self, dst: &mut [T], rounding: FloatToIntRounding, ) -> Result<()>
RasterBuffer::copy_to_slice with an explicit float→integer rounding
mode.
§Errors
Returns an error if dst.len() differs from RasterBuffer::pixel_count.
Sourcepub fn to_typed_vec<T: RasterElement>(&self) -> Result<Vec<T>>
pub fn to_typed_vec<T: RasterElement>(&self) -> Result<Vec<T>>
Converts every pixel into a freshly allocated Vec<T>.
Exactly one allocation, then a single conversion pass. Prefer
RasterBuffer::copy_to_slice when a destination already exists.
§Errors
Returns an error only if the buffer’s declared size and its storage disagree, which the constructors make impossible.
§Examples
use oxigeo_core::buffer::RasterBuffer;
use oxigeo_core::types::RasterDataType;
let buffer = RasterBuffer::from_element_slice(2, 1, &[-3i16, 300])?;
assert_eq!(buffer.to_typed_vec::<f64>()?, vec![-3.0, 300.0]);
// Integer destinations saturate instead of wrapping.
assert_eq!(buffer.to_typed_vec::<u8>()?, vec![0, 255]);Sourcepub fn to_typed_vec_with<T: RasterElement>(
&self,
rounding: FloatToIntRounding,
) -> Result<Vec<T>>
pub fn to_typed_vec_with<T: RasterElement>( &self, rounding: FloatToIntRounding, ) -> Result<Vec<T>>
RasterBuffer::to_typed_vec with an explicit float→integer rounding
mode.
§Errors
Returns an error only if the buffer’s declared size and its storage disagree, which the constructors make impossible.
Sourcepub fn get_u8(&self, x: u64, y: u64) -> Result<u8>
pub fn get_u8(&self, x: u64, y: u64) -> Result<u8>
Gets a pixel value as u8.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not UInt8.
Sourcepub fn get_i8(&self, x: u64, y: u64) -> Result<i8>
pub fn get_i8(&self, x: u64, y: u64) -> Result<i8>
Gets a pixel value as i8.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Int8.
Sourcepub fn get_u16(&self, x: u64, y: u64) -> Result<u16>
pub fn get_u16(&self, x: u64, y: u64) -> Result<u16>
Gets a pixel value as u16.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not UInt16.
Sourcepub fn get_i16(&self, x: u64, y: u64) -> Result<i16>
pub fn get_i16(&self, x: u64, y: u64) -> Result<i16>
Gets a pixel value as i16.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Int16.
Sourcepub fn get_u32(&self, x: u64, y: u64) -> Result<u32>
pub fn get_u32(&self, x: u64, y: u64) -> Result<u32>
Gets a pixel value as u32.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not UInt32.
Sourcepub fn get_i32(&self, x: u64, y: u64) -> Result<i32>
pub fn get_i32(&self, x: u64, y: u64) -> Result<i32>
Gets a pixel value as i32.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Int32.
Sourcepub fn get_u64(&self, x: u64, y: u64) -> Result<u64>
pub fn get_u64(&self, x: u64, y: u64) -> Result<u64>
Gets a pixel value as u64.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not UInt64.
Sourcepub fn get_i64(&self, x: u64, y: u64) -> Result<i64>
pub fn get_i64(&self, x: u64, y: u64) -> Result<i64>
Gets a pixel value as i64.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Int64.
Sourcepub fn get_f32(&self, x: u64, y: u64) -> Result<f32>
pub fn get_f32(&self, x: u64, y: u64) -> Result<f32>
Gets a pixel value as f32.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Float32.
Sourcepub fn get_f64(&self, x: u64, y: u64) -> Result<f64>
pub fn get_f64(&self, x: u64, y: u64) -> Result<f64>
Gets a pixel value as f64.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Float64.
Sourcepub fn set_u8(&mut self, x: u64, y: u64, value: u8) -> Result<()>
pub fn set_u8(&mut self, x: u64, y: u64, value: u8) -> Result<()>
Sets a pixel value from u8.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not UInt8.
Sourcepub fn set_f32(&mut self, x: u64, y: u64, value: f32) -> Result<()>
pub fn set_f32(&mut self, x: u64, y: u64, value: f32) -> Result<()>
Sets a pixel value from f32.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Float32.
Sourcepub fn set_f64(&mut self, x: u64, y: u64, value: f64) -> Result<()>
pub fn set_f64(&mut self, x: u64, y: u64, value: f64) -> Result<()>
Sets a pixel value from f64.
§Errors
Returns an error if coordinates are out of bounds or the buffer type is not Float64.
Sourcepub fn row_slice<T: Copy + 'static>(&self, y: u64) -> Result<&[T]>
pub fn row_slice<T: Copy + 'static>(&self, y: u64) -> Result<&[T]>
Returns a row of pixel data as a typed slice (zero-copy).
§Errors
Returns an error if y is out of bounds, the type size mismatches, or
the buffer’s storage is not aligned for T.
Sourcepub fn window(&self, x: u64, y: u64, width: u64, height: u64) -> Result<Self>
pub fn window(&self, x: u64, y: u64, width: u64, height: u64) -> Result<Self>
Returns a rectangular window of pixel data as a new RasterBuffer.
§Errors
Returns an error if the window extends outside buffer bounds.
Sourcepub fn is_nodata(&self, value: f64) -> bool
pub fn is_nodata(&self, value: f64) -> bool
Returns true if the given value equals the nodata value
Sourcepub fn convert_to(&self, target_type: RasterDataType) -> Result<Self>
pub fn convert_to(&self, target_type: RasterDataType) -> Result<Self>
Converts the buffer to a different data type
The nodata value is carried over unchanged (it is not re-encoded into
the target type), exactly as before.
Values that do not fit the target type saturate at its bounds and
floating-point samples are truncated towards zero
(FloatToIntRounding::Truncate), matching the historical per-pixel
implementation bit-for-bit. Use RasterBuffer::copy_to_slice_with or
convert_raw_bytes to pick FloatToIntRounding::Nearest instead.
Integer→integer conversions are exact: they no longer bridge through
f64, so u64/i64 values beyond 253 keep every bit.
§Errors
Returns an error if conversion fails
Sourcepub fn compute_statistics(&self) -> Result<BufferStatistics>
pub fn compute_statistics(&self) -> Result<BufferStatistics>
Computes basic statistics
Sourcepub fn compute_statistics_with_histogram(
&self,
bin_count: usize,
) -> Result<BufferStatistics>
pub fn compute_statistics_with_histogram( &self, bin_count: usize, ) -> Result<BufferStatistics>
Computes statistics and an optional histogram in one pass.
The returned BufferStatistics contains a histogram of bin_count bins
with uniform spacing covering the range [min, max]. Each bin holds the count
of valid pixels whose value falls within that bin’s interval.
§Arguments
bin_count— Number of histogram bins. Must be ≥ 1.
§Errors
Returns crate::error::OxiGeoError::InvalidParameter if bin_count is 0.
Returns errors from pixel access on corrupt buffers.
§Notes
- NaN and infinite values are excluded (same as
RasterBuffer::compute_statistics). - When all valid values are identical (
min == max), all counts go into bin 0. - When no valid pixels exist,
histogramisSome(vec![0; bin_count]).
Trait Implementations§
Source§impl Clone for RasterBuffer
impl Clone for RasterBuffer
Source§fn clone(&self) -> RasterBuffer
fn clone(&self) -> RasterBuffer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RasterBuffer
impl Debug for RasterBuffer
Source§impl TryFrom<&RasterBuffer> for RecordBatch
impl TryFrom<&RasterBuffer> for RecordBatch
Source§fn try_from(buf: &RasterBuffer) -> Result<Self>
fn try_from(buf: &RasterBuffer) -> Result<Self>
Converts a RasterBuffer into an Arrow RecordBatch.
§Errors
OxiGeoError::NotSupported– if the buffer’s data type isCFloat32orCFloat64, which have no standard Arrow equivalent.OxiGeoError::Internal– if the underlying Arrow builder rejects the constructed schema or column (should not happen in practice).
Source§type Error = OxiGeoError
type Error = OxiGeoError
Source§impl TryFrom<RecordBatch> for RasterBuffer
impl TryFrom<RecordBatch> for RasterBuffer
Source§fn try_from(batch: RecordBatch) -> Result<Self>
fn try_from(batch: RecordBatch) -> Result<Self>
Converts an Arrow RecordBatch back into a RasterBuffer.
The RecordBatch must have been produced by the TryFrom<&RasterBuffer>
impl (or be schema-compatible): exactly one column named "pixel_values"
and schema metadata containing "width", "height", and "data_type".
Arrow nulls in the column are replaced by the zero representation of the
target type. The returned buffer carries NoDataValue::None.
§Errors
OxiGeoError::InvalidParameter– wrong number of columns, wrong column name, or missing / malformed schema metadata.OxiGeoError::Internal– unexpected Arrow type mismatch (should not occur for batches produced by this module).