weiji14 / cog3pio

Cloud-optimized GeoTIFF ... Parallel I/O 🦀
Apache License 2.0
29 stars 0 forks source link

:sparkles: A read_geotiff function for reading GeoTIFF into ndarray #3

Closed weiji14 closed 7 months ago

weiji14 commented 7 months ago

Rust-based function for reading GeoTIFF files!

Uses the tiff crate for the I/O, ndarray crate to store the 2D array in Rust, and numpy crate to convert to numpy.ndarray in Python.

Note: This only works on single-band GeoTIFF files with float32 dtype for now

Usage:

In Rust:

use ndarray::Array2;
use std::fs::File;
use cog3pio::io::geotiff::read_geotiff;

let path: &str = "path/to/file.tif";
let file: File = File::open(path).expect("Cannot find GeoTIFF file");
let arr: Array2<f32> = read_geotiff(file).unwrap();
assert_eq!(arr.dim(), (20, 20));
assert_eq!(arr.mean(), Some(19.0));

In Python:

import numpy as np
from cog3pio import read_geotiff

array: np.ndarray = read_geotiff(path="georaster/data/tiff/float32.tif")
assert array.shape == (20, 20)
assert array.dtype == "float32"

TODO:

TODO in future:

References: