Skip to content

Commit 29a9585

Browse files
authored
Merge pull request #158 from rmanoka/fix/docs-and-open-flags-cfg
Add documentation
2 parents 7e4fd8a + 9f07fcc commit 29a9585

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

src/dataset.rs

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@ use std::convert::TryInto;
1616

1717
use bitflags::bitflags;
1818

19+
/// A 2-D affine transform mapping pixel coordiates to world
20+
/// coordinates. See [GDALGetGeoTransform] for more details.
21+
///
22+
/// [GDALGetGeoTransform]: https://gdal.org/api/gdaldataset_cpp.html#classGDALDataset_1a5101119705f5fa2bc1344ab26f66fd1d
1923
pub type GeoTransform = [c_double; 6];
2024
static START: Once = Once::new();
2125

26+
/// Wrapper around a [`GDALDataset`][GDALDataset] object.
27+
///
28+
/// Represents both a [vector dataset][vector-data-model]
29+
/// containing a collection of layers; and a [raster
30+
/// dataset][raster-data-model] containing a collection of
31+
/// rasterbands.
32+
///
33+
/// [vector-data-model]: https://gdal.org/user/vector_data_model.html
34+
/// [raster-data-model]: https://gdal.org/user/raster_data_model.html
35+
/// [GDALDataset]: https://gdal.org/api/gdaldataset_cpp.html#_CPPv411GDALDataset
2236
#[derive(Debug)]
2337
pub struct Dataset {
2438
c_dataset: GDALDatasetH,
@@ -32,44 +46,54 @@ pub fn _register_drivers() {
3246
}
3347
}
3448

35-
// GDal extended open flags, skipped by bindgen
36-
//
37-
// Note that the `GDAL_OF_SHARED` option is removed from the set
38-
// of allowed option because it subverts the [`Send`] implementation
39-
// that allow passing the dataset the another thread.
40-
// See https://github.com/georust/gdal/issues/154.
49+
// These are skipped by bindgen and manually updated.
4150
#[cfg(major_ge_2)]
4251
bitflags! {
52+
/// GDal extended open flags used by [`Dataset::open_ex`].
53+
///
54+
/// Used in the `nOpenFlags` argument to [`GDALOpenEx`].
55+
///
56+
/// Note that the `GDAL_OF_SHARED` option is removed
57+
/// from the set of allowed option because it subverts
58+
/// the [`Send`] implementation that allow passing the
59+
/// dataset the another thread. See
60+
/// https://github.com/georust/gdal/issues/154.
61+
///
62+
/// [`GDALOpenEx`]: https://gdal.org/doxygen/gdal_8h.html#a9cb8585d0b3c16726b08e25bcc94274a
4363
pub struct GdalOpenFlags: c_uint {
64+
/// Open in read-only mode (default).
4465
const GDAL_OF_READONLY = 0x00;
45-
// Open in update mode.
66+
/// Open in update mode.
4667
const GDAL_OF_UPDATE = 0x01;
47-
// Allow raster and vector drivers to be used.
68+
/// Allow raster and vector drivers to be used.
4869
const GDAL_OF_ALL = 0x00;
49-
// Allow raster drivers to be used.
70+
/// Allow raster drivers to be used.
5071
const GDAL_OF_RASTER = 0x02;
51-
// Allow vector drivers to be used.
72+
/// Allow vector drivers to be used.
5273
const GDAL_OF_VECTOR = 0x04;
53-
// Allow gnm drivers to be used.
54-
#[cfg(all(major_ge_2,minor_ge_1))]
74+
/// Allow gnm drivers to be used.
75+
#[cfg(any( all(major_ge_2,minor_ge_1), major_ge_3 ))]
5576
const GDAL_OF_GNM = 0x08;
56-
// Allow multidimensional raster drivers to be used.
77+
/// Allow multidimensional raster drivers to be used.
5778
#[cfg(all(major_ge_3,minor_ge_1))]
5879
const GDAL_OF_MULTIDIM_RASTER = 0x10;
59-
// Emit error message in case of failed open.
80+
/// Emit error message in case of failed open.
6081
const GDAL_OF_VERBOSE_ERROR = 0x40;
61-
// Open as internal dataset. Such dataset isn't registered in the global list
62-
// of opened dataset. Cannot be used with GDAL_OF_SHARED.
82+
/// Open as internal dataset. Such dataset isn't
83+
/// registered in the global list of opened dataset.
84+
/// Cannot be used with GDAL_OF_SHARED.
6385
const GDAL_OF_INTERNAL = 0x80;
64-
// Let GDAL decide if a array-based or hashset-based storage strategy for
65-
// cached blocks must be used.
66-
// GDAL_OF_DEFAULT_BLOCK_ACCESS, GDAL_OF_ARRAY_BLOCK_ACCESS and
67-
// GDAL_OF_HASHSET_BLOCK_ACCESS are mutually exclusive.
68-
#[cfg(all(major_ge_2,minor_ge_1))]
86+
87+
/// Default strategy for cached blocks.
88+
#[cfg(any( all(major_ge_2,minor_ge_1), major_ge_3 ))]
6989
const GDAL_OF_DEFAULT_BLOCK_ACCESS = 0;
70-
#[cfg(all(major_ge_2,minor_ge_1))]
90+
91+
/// Array based strategy for cached blocks.
92+
#[cfg(any( all(major_ge_2,minor_ge_1), major_ge_3 ))]
7193
const GDAL_OF_ARRAY_BLOCK_ACCESS = 0x100;
72-
#[cfg(all(major_ge_2,minor_ge_1))]
94+
95+
/// Hashset based strategy for cached blocks.
96+
#[cfg(any( all(major_ge_2,minor_ge_1), major_ge_3 ))]
7397
const GDAL_OF_HASHSET_BLOCK_ACCESS = 0x200;
7498
}
7599
}
@@ -115,10 +139,14 @@ impl Dataset {
115139
self.c_dataset
116140
}
117141

142+
/// Open a dataset at the given `path` with default
143+
/// options.
118144
pub fn open(path: &Path) -> Result<Dataset> {
119145
Self::open_ex(path, DatasetOptions::default())
120146
}
121147

148+
/// Open a dataset with extended options. See
149+
/// [GDALOpenEx].
122150
pub fn open_ex(path: &Path, options: DatasetOptions) -> Result<Dataset> {
123151
_register_drivers();
124152
let filename = path.to_string_lossy();
@@ -217,23 +245,27 @@ impl Dataset {
217245
Dataset { c_dataset }
218246
}
219247

248+
/// Fetch the projection definition string for this dataset.
220249
pub fn projection(&self) -> String {
221250
let rv = unsafe { gdal_sys::GDALGetProjectionRef(self.c_dataset) };
222251
_string(rv)
223252
}
224253

254+
/// Set the projection reference string for this dataset.
225255
pub fn set_projection(&self, projection: &str) -> Result<()> {
226256
let c_projection = CString::new(projection)?;
227257
unsafe { gdal_sys::GDALSetProjection(self.c_dataset, c_projection.as_ptr()) };
228258
Ok(())
229259
}
230260

231261
#[cfg(major_ge_3)]
262+
/// Get the spatial reference system for this dataset.
232263
pub fn spatial_ref(&self) -> Result<SpatialRef> {
233264
unsafe { SpatialRef::from_c_obj(gdal_sys::GDALGetSpatialRef(self.c_dataset)) }
234265
}
235266

236267
#[cfg(major_ge_3)]
268+
/// Set the spatial reference system for this dataset.
237269
pub fn set_spatial_ref(&self, spatial_ref: &SpatialRef) -> Result<()> {
238270
let rv = unsafe { gdal_sys::GDALSetSpatialRef(self.c_dataset, spatial_ref.to_c_hsrs()) };
239271
if rv != CPLErr::CE_None {
@@ -261,13 +293,18 @@ impl Dataset {
261293
Ok(unsafe { Dataset::from_c_dataset(c_dataset) })
262294
}
263295

296+
/// Fetch the driver to which this dataset relates.
264297
pub fn driver(&self) -> Driver {
265298
unsafe {
266299
let c_driver = gdal_sys::GDALGetDatasetDriver(self.c_dataset);
267300
Driver::from_c_driver(c_driver)
268301
}
269302
}
270303

304+
/// Fetch a band object for a dataset.
305+
///
306+
/// Applies to raster datasets, and fetches the
307+
/// rasterband at the given _1-based_ index.
271308
pub fn rasterband(&self, band_index: isize) -> Result<RasterBand> {
272309
unsafe {
273310
let c_band = gdal_sys::GDALGetRasterBand(self.c_dataset, band_index as c_int);
@@ -282,10 +319,15 @@ impl Dataset {
282319
unsafe { Layer::from_c_layer(self, c_layer) }
283320
}
284321

322+
/// Get the number of layers in this dataset.
285323
pub fn layer_count(&self) -> isize {
286324
(unsafe { gdal_sys::OGR_DS_GetLayerCount(self.c_dataset) }) as isize
287325
}
288326

327+
/// Fetch a layer by index.
328+
///
329+
/// Applies to vector datasets, and fetches by the given
330+
/// _0-based_ index.
289331
pub fn layer(&mut self, idx: isize) -> Result<Layer> {
290332
let c_layer = unsafe { gdal_sys::OGR_DS_GetLayer(self.c_dataset, idx as c_int) };
291333
if c_layer.is_null() {
@@ -294,6 +336,7 @@ impl Dataset {
294336
Ok(self.child_layer(c_layer))
295337
}
296338

339+
/// Fetch a layer by name.
297340
pub fn layer_by_name(&mut self, name: &str) -> Result<Layer> {
298341
let c_name = CString::new(name)?;
299342
let c_layer = unsafe { gdal_sys::OGR_DS_GetLayerByName(self.c_dataset(), c_name.as_ptr()) };
@@ -303,14 +346,17 @@ impl Dataset {
303346
Ok(self.child_layer(c_layer))
304347
}
305348

349+
/// Returns an iterator over the layers of the dataset.
306350
pub fn layers(&self) -> LayerIterator {
307351
LayerIterator::with_dataset(self)
308352
}
309353

354+
/// Fetch the number of raster bands on this dataset.
310355
pub fn raster_count(&self) -> isize {
311356
(unsafe { gdal_sys::GDALGetRasterCount(self.c_dataset) }) as isize
312357
}
313358

359+
/// Returns the raster dimensions: (width, height).
314360
pub fn raster_size(&self) -> (usize, usize) {
315361
let size_x = unsafe { gdal_sys::GDALGetRasterXSize(self.c_dataset) } as usize;
316362
let size_y = unsafe { gdal_sys::GDALGetRasterYSize(self.c_dataset) } as usize;

src/raster/rasterband.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl<'a> RasterBand<'a> {
202202
Array2::from_shape_vec((size.1, size.0), data).map_err(Into::into)
203203
}
204204

205-
// Write a 'Buffer<T>' into a 'Dataset'.
205+
/// Write a 'Buffer<T>' into a 'Dataset'.
206206
/// # Arguments
207207
/// * band_index - the band_index
208208
/// * window - the window position from top left
@@ -236,10 +236,12 @@ impl<'a> RasterBand<'a> {
236236
Ok(())
237237
}
238238

239+
/// Returns the pixel datatype of this band.
239240
pub fn band_type(&self) -> GDALDataType::Type {
240241
unsafe { gdal_sys::GDALGetRasterDataType(self.c_rasterband) }
241242
}
242243

244+
/// Returns the no data value of this band.
243245
pub fn no_data_value(&self) -> Option<f64> {
244246
let mut pb_success = 1;
245247
let no_data =
@@ -250,6 +252,7 @@ impl<'a> RasterBand<'a> {
250252
None
251253
}
252254

255+
/// Set the no data value of this band.
253256
pub fn set_no_data_value(&self, no_data: f64) -> Result<()> {
254257
let rv = unsafe { gdal_sys::GDALSetRasterNoDataValue(self.c_rasterband, no_data) };
255258
if rv != CPLErr::CE_None {
@@ -258,6 +261,7 @@ impl<'a> RasterBand<'a> {
258261
Ok(())
259262
}
260263

264+
/// Returns the scale of this band if set.
261265
pub fn scale(&self) -> Option<f64> {
262266
let mut pb_success = 1;
263267
let scale = unsafe { gdal_sys::GDALGetRasterScale(self.c_rasterband, &mut pb_success) };
@@ -267,6 +271,7 @@ impl<'a> RasterBand<'a> {
267271
None
268272
}
269273

274+
/// Returns the offset of this band if set.
270275
pub fn offset(&self) -> Option<f64> {
271276
let mut pb_success = 1;
272277
let offset = unsafe { gdal_sys::GDALGetRasterOffset(self.c_rasterband, &mut pb_success) };

0 commit comments

Comments
 (0)