@@ -16,9 +16,23 @@ use std::convert::TryInto;
16
16
17
17
use bitflags:: bitflags;
18
18
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
19
23
pub type GeoTransform = [ c_double ; 6 ] ;
20
24
static START : Once = Once :: new ( ) ;
21
25
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
22
36
#[ derive( Debug ) ]
23
37
pub struct Dataset {
24
38
c_dataset : GDALDatasetH ,
@@ -32,44 +46,54 @@ pub fn _register_drivers() {
32
46
}
33
47
}
34
48
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.
41
50
#[ cfg( major_ge_2) ]
42
51
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
43
63
pub struct GdalOpenFlags : c_uint {
64
+ /// Open in read-only mode (default).
44
65
const GDAL_OF_READONLY = 0x00 ;
45
- // Open in update mode.
66
+ /// Open in update mode.
46
67
const GDAL_OF_UPDATE = 0x01 ;
47
- // Allow raster and vector drivers to be used.
68
+ /// Allow raster and vector drivers to be used.
48
69
const GDAL_OF_ALL = 0x00 ;
49
- // Allow raster drivers to be used.
70
+ /// Allow raster drivers to be used.
50
71
const GDAL_OF_RASTER = 0x02 ;
51
- // Allow vector drivers to be used.
72
+ /// Allow vector drivers to be used.
52
73
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 ) ) ]
55
76
const GDAL_OF_GNM = 0x08 ;
56
- // Allow multidimensional raster drivers to be used.
77
+ /// Allow multidimensional raster drivers to be used.
57
78
#[ cfg( all( major_ge_3, minor_ge_1) ) ]
58
79
const GDAL_OF_MULTIDIM_RASTER = 0x10 ;
59
- // Emit error message in case of failed open.
80
+ /// Emit error message in case of failed open.
60
81
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.
63
85
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 ) ) ]
69
89
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 ) ) ]
71
93
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 ) ) ]
73
97
const GDAL_OF_HASHSET_BLOCK_ACCESS = 0x200 ;
74
98
}
75
99
}
@@ -115,10 +139,14 @@ impl Dataset {
115
139
self . c_dataset
116
140
}
117
141
142
+ /// Open a dataset at the given `path` with default
143
+ /// options.
118
144
pub fn open ( path : & Path ) -> Result < Dataset > {
119
145
Self :: open_ex ( path, DatasetOptions :: default ( ) )
120
146
}
121
147
148
+ /// Open a dataset with extended options. See
149
+ /// [GDALOpenEx].
122
150
pub fn open_ex ( path : & Path , options : DatasetOptions ) -> Result < Dataset > {
123
151
_register_drivers ( ) ;
124
152
let filename = path. to_string_lossy ( ) ;
@@ -217,23 +245,27 @@ impl Dataset {
217
245
Dataset { c_dataset }
218
246
}
219
247
248
+ /// Fetch the projection definition string for this dataset.
220
249
pub fn projection ( & self ) -> String {
221
250
let rv = unsafe { gdal_sys:: GDALGetProjectionRef ( self . c_dataset ) } ;
222
251
_string ( rv)
223
252
}
224
253
254
+ /// Set the projection reference string for this dataset.
225
255
pub fn set_projection ( & self , projection : & str ) -> Result < ( ) > {
226
256
let c_projection = CString :: new ( projection) ?;
227
257
unsafe { gdal_sys:: GDALSetProjection ( self . c_dataset , c_projection. as_ptr ( ) ) } ;
228
258
Ok ( ( ) )
229
259
}
230
260
231
261
#[ cfg( major_ge_3) ]
262
+ /// Get the spatial reference system for this dataset.
232
263
pub fn spatial_ref ( & self ) -> Result < SpatialRef > {
233
264
unsafe { SpatialRef :: from_c_obj ( gdal_sys:: GDALGetSpatialRef ( self . c_dataset ) ) }
234
265
}
235
266
236
267
#[ cfg( major_ge_3) ]
268
+ /// Set the spatial reference system for this dataset.
237
269
pub fn set_spatial_ref ( & self , spatial_ref : & SpatialRef ) -> Result < ( ) > {
238
270
let rv = unsafe { gdal_sys:: GDALSetSpatialRef ( self . c_dataset , spatial_ref. to_c_hsrs ( ) ) } ;
239
271
if rv != CPLErr :: CE_None {
@@ -261,13 +293,18 @@ impl Dataset {
261
293
Ok ( unsafe { Dataset :: from_c_dataset ( c_dataset) } )
262
294
}
263
295
296
+ /// Fetch the driver to which this dataset relates.
264
297
pub fn driver ( & self ) -> Driver {
265
298
unsafe {
266
299
let c_driver = gdal_sys:: GDALGetDatasetDriver ( self . c_dataset ) ;
267
300
Driver :: from_c_driver ( c_driver)
268
301
}
269
302
}
270
303
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.
271
308
pub fn rasterband ( & self , band_index : isize ) -> Result < RasterBand > {
272
309
unsafe {
273
310
let c_band = gdal_sys:: GDALGetRasterBand ( self . c_dataset , band_index as c_int ) ;
@@ -282,10 +319,15 @@ impl Dataset {
282
319
unsafe { Layer :: from_c_layer ( self , c_layer) }
283
320
}
284
321
322
+ /// Get the number of layers in this dataset.
285
323
pub fn layer_count ( & self ) -> isize {
286
324
( unsafe { gdal_sys:: OGR_DS_GetLayerCount ( self . c_dataset ) } ) as isize
287
325
}
288
326
327
+ /// Fetch a layer by index.
328
+ ///
329
+ /// Applies to vector datasets, and fetches by the given
330
+ /// _0-based_ index.
289
331
pub fn layer ( & mut self , idx : isize ) -> Result < Layer > {
290
332
let c_layer = unsafe { gdal_sys:: OGR_DS_GetLayer ( self . c_dataset , idx as c_int ) } ;
291
333
if c_layer. is_null ( ) {
@@ -294,6 +336,7 @@ impl Dataset {
294
336
Ok ( self . child_layer ( c_layer) )
295
337
}
296
338
339
+ /// Fetch a layer by name.
297
340
pub fn layer_by_name ( & mut self , name : & str ) -> Result < Layer > {
298
341
let c_name = CString :: new ( name) ?;
299
342
let c_layer = unsafe { gdal_sys:: OGR_DS_GetLayerByName ( self . c_dataset ( ) , c_name. as_ptr ( ) ) } ;
@@ -303,14 +346,17 @@ impl Dataset {
303
346
Ok ( self . child_layer ( c_layer) )
304
347
}
305
348
349
+ /// Returns an iterator over the layers of the dataset.
306
350
pub fn layers ( & self ) -> LayerIterator {
307
351
LayerIterator :: with_dataset ( self )
308
352
}
309
353
354
+ /// Fetch the number of raster bands on this dataset.
310
355
pub fn raster_count ( & self ) -> isize {
311
356
( unsafe { gdal_sys:: GDALGetRasterCount ( self . c_dataset ) } ) as isize
312
357
}
313
358
359
+ /// Returns the raster dimensions: (width, height).
314
360
pub fn raster_size ( & self ) -> ( usize , usize ) {
315
361
let size_x = unsafe { gdal_sys:: GDALGetRasterXSize ( self . c_dataset ) } as usize ;
316
362
let size_y = unsafe { gdal_sys:: GDALGetRasterYSize ( self . c_dataset ) } as usize ;
0 commit comments