Skip to content
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

Create options not supported #190

Closed
geohardtke opened this issue Jun 10, 2021 · 3 comments
Closed

Create options not supported #190

geohardtke opened this issue Jun 10, 2021 · 3 comments

Comments

@geohardtke
Copy link
Contributor

Hi all,

I didn't find a way to set creation options for new raster datasets. In a local copy of the repo I implemented it like this:

pub struct RasterCreationOption<'a>{
    key : &'a str,
    value : &'a str,
}

// in the impl block for Dataset:
//
    pub fn create_with_band_type_with_options<T: GdalType>(
        &self,
        filename: &str,
        size_x: isize,
        size_y: isize,
        bands: isize,
        options: Vec<RasterCreationOption>,
    ) -> Result<Dataset> {

        // process options:
        let mut options_c = null_mut();
        for option in options{
            let psz_name  = CString::new(option.key)?;
            let psz_value = CString::new(option.value)?;
            unsafe{
                options_c = CSLSetNameValue(options_c, psz_name.as_ptr(), psz_value.as_ptr());
            }
        }

        let c_filename = CString::new(filename)?;
        let c_dataset = unsafe {
            gdal_sys::GDALCreate(
                self.c_driver,
                c_filename.as_ptr(),
                size_x as c_int,
                size_y as c_int,
                bands as c_int,
                T::gdal_type(),
                options_c as *mut *mut i8,
            )
        };
        if c_dataset.is_null() {
            return Err(_last_null_pointer_err("GDALCreate"));
        };
        Ok(unsafe { Dataset::from_c_dataset(c_dataset) })
    }

and I use it like:

    let driver = Driver::get("GTiff").unwrap();
    let options = vec![RasterCreationOption{key:"COMPRESS", value:"LZW"} ,
                       RasterCreationOption{key:"TILED", value: "YES"}] ;
    let mut dataset = driver
          .create_with_band_type_with_options::<u8>("testing.tif",
             2048,
             2048,
             1,
             options)
          .unwrap();
}

And it seems to do what I want. I'm not sure if the implantation is safe/correct since I'm still new to rust. If you are interested I can create a PR for this (or improved with some guidance)?

@lnicola
Copy link
Member

lnicola commented Jun 10, 2021

Yeah, that looks reasonable for a start, so a PR would be great.

@geohardtke
Copy link
Contributor Author

geohardtke commented Jun 13, 2021

Tests in #193 are passing now. Didn't know that the dataset had to be dropped before the metadata could be read.

Or is there something wrong with my changes ?

@geohardtke
Copy link
Contributor Author

closed. See #193.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants