Created
November 3, 2023 20:13
-
-
Save metasim/d2a288d5b6126438aaa84b9e95707aff to your computer and use it in GitHub Desktop.
Testing Reprojection Nodata
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
use std::ffi::CString; | |
use std::process::exit; | |
use std::ptr; | |
use gdal::errors::Result; | |
use gdal::spatial_ref::SpatialRef; | |
use gdal::Dataset; | |
use gdal_sys::{ | |
CPLErr, CSLSetNameValue, GDALCreateWarpOptions, GDALDestroyWarpOptions, GDALResampleAlg, | |
GDALWarpInitDefaultBandMapping, GDALWarpInitDstNoDataReal, | |
}; | |
fn main() -> Result<()> { | |
// Implement the following: | |
// gdalwarp -overwrite -t_srs EPSG:4269 -dstnodata 255 -r near fixtures/labels.tif target/labels-reprojected.tif | |
let ds = Dataset::open("fixtures/labels.tif")?; | |
let dest_file = CString::new("target/labels-reprojected.tif")?; | |
let dest_srs = CString::new(SpatialRef::from_epsg(4269)?.to_wkt()?)?; | |
let rv: CPLErr::Type; | |
unsafe { | |
let opts = GDALCreateWarpOptions(); | |
GDALWarpInitDefaultBandMapping(opts, 1); | |
GDALWarpInitDstNoDataReal(opts, 255.0); | |
(*opts).papszWarpOptions = CSLSetNameValue( | |
ptr::null_mut(), | |
CString::new("INIT_DEST")?.as_ptr(), | |
CString::new("NO_DATA")?.as_ptr(), | |
); | |
rv = gdal_sys::GDALCreateAndReprojectImage( | |
ds.c_dataset(), | |
ptr::null(), | |
dest_file.as_ptr(), | |
dest_srs.as_ptr(), | |
ds.driver().c_driver(), | |
ptr::null_mut(), | |
GDALResampleAlg::GRA_NearestNeighbour, | |
0.0, | |
0.0, | |
None, | |
ptr::null_mut(), | |
opts, | |
); | |
GDALDestroyWarpOptions(opts); | |
}; | |
if rv != CPLErr::CE_None { | |
exit(1); | |
} else { | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment