-
Notifications
You must be signed in to change notification settings - Fork 96
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
add a driver manager wrapper #324
Changes from 3 commits
c88b95a
fc0d5ce
71560d8
49fdfd4
141c8d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,7 @@ use crate::errors::*; | |
static START: Once = Once::new(); | ||
|
||
pub fn _register_drivers() { | ||
unsafe { | ||
START.call_once(|| { | ||
gdal_sys::GDALAllRegister(); | ||
}); | ||
} | ||
START.call_once(DriverManager::register_all); | ||
} | ||
|
||
/// # Raster and Vector Driver API | ||
|
@@ -63,14 +59,9 @@ impl Driver { | |
/// ```text | ||
/// Cloud optimized GeoTIFF generator | ||
/// ``` | ||
#[deprecated(note = "Please use `DriverManager::get_driver_by_name()` instead")] | ||
pub fn get_by_name(name: &str) -> Result<Driver> { | ||
_register_drivers(); | ||
let c_name = CString::new(name)?; | ||
let c_driver = unsafe { gdal_sys::GDALGetDriverByName(c_name.as_ptr()) }; | ||
if c_driver.is_null() { | ||
return Err(_last_null_pointer_err("GDALGetDriverByName")); | ||
}; | ||
Ok(Driver { c_driver }) | ||
DriverManager::get_driver_by_name(name) | ||
} | ||
|
||
/// Returns the driver with the given index, which must be less than the value returned by | ||
|
@@ -92,13 +83,9 @@ impl Driver { | |
/// ```text | ||
/// 'VRT' is 'Virtual Raster' | ||
/// ``` | ||
#[deprecated(note = "Please use `DriverManager::get_driver()` instead")] | ||
pub fn get(index: usize) -> Result<Driver> { | ||
_register_drivers(); | ||
let c_driver = unsafe { gdal_sys::GDALGetDriver(index.try_into().unwrap()) }; | ||
if c_driver.is_null() { | ||
return Err(_last_null_pointer_err("GDALGetDriver")); | ||
} | ||
Ok(Driver { c_driver }) | ||
DriverManager::get_driver(index) | ||
} | ||
|
||
/// Returns the number of registered drivers. | ||
|
@@ -112,10 +99,9 @@ impl Driver { | |
/// ```text | ||
/// 203 drivers are registered | ||
/// ``` | ||
#[deprecated(note = "Please use `DriverManager::count()` instead")] | ||
pub fn count() -> usize { | ||
_register_drivers(); | ||
let count = unsafe { gdal_sys::GDALGetDriverCount() }; | ||
count.try_into().unwrap() | ||
DriverManager::count() | ||
} | ||
|
||
/// Return the short name of a driver. | ||
|
@@ -366,3 +352,140 @@ impl MajorObject for Driver { | |
} | ||
|
||
impl Metadata for Driver {} | ||
|
||
/// A wrapper around `GDALDriverManager`. | ||
/// This struct helps listing and registering [`Driver`]s. | ||
pub struct DriverManager; | ||
|
||
impl DriverManager { | ||
/// Returns the number of registered drivers. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust, no_run | ||
/// use gdal::Driver; | ||
/// println!("{} drivers are registered", Driver::count()); | ||
/// ``` | ||
/// ```text | ||
/// 203 drivers are registered | ||
/// ``` | ||
pub fn count() -> usize { | ||
_register_drivers(); | ||
let count = unsafe { gdal_sys::GDALGetDriverCount() }; | ||
count | ||
.try_into() | ||
.expect("The returned count should be zero or positive") | ||
} | ||
|
||
/// Returns the driver with the given index, which must be less than the value returned by | ||
/// `Driver::count()`. | ||
/// | ||
/// See also: [`count`](Self::count) | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust, no_run | ||
/// use gdal::Driver; | ||
/// # fn main() -> gdal::errors::Result<()> { | ||
/// assert!(Driver::count() > 0); | ||
/// let d = Driver::get(0)?; | ||
/// println!("'{}' is '{}'", d.short_name(), d.long_name()); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
/// ```text | ||
/// 'VRT' is 'Virtual Raster' | ||
/// ``` | ||
pub fn get_driver(index: usize) -> Result<Driver> { | ||
_register_drivers(); | ||
let c_driver = unsafe { gdal_sys::GDALGetDriver(index.try_into().unwrap()) }; | ||
if c_driver.is_null() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return Err(_last_null_pointer_err("GDALGetDriver")); | ||
} | ||
Ok(Driver { c_driver }) | ||
} | ||
|
||
/// Returns the driver with the given short name or [`Err`] if not found. | ||
/// | ||
/// See also: [`count`](Self::count), [`get`](Self::get) | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust, no_run | ||
/// use gdal::Driver; | ||
/// # fn main() -> gdal::errors::Result<()> { | ||
/// let cog_driver = Driver::get_by_name("COG")?; | ||
/// println!("{}", cog_driver.long_name()); | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
/// ```text | ||
/// Cloud optimized GeoTIFF generator | ||
/// ``` | ||
pub fn get_driver_by_name(name: &str) -> Result<Driver> { | ||
_register_drivers(); | ||
let c_name = CString::new(name)?; | ||
let c_driver = unsafe { gdal_sys::GDALGetDriverByName(c_name.as_ptr()) }; | ||
if c_driver.is_null() { | ||
return Err(_last_null_pointer_err("GDALGetDriverByName")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
Ok(Driver { c_driver }) | ||
} | ||
|
||
/// Register a driver for use. | ||
/// | ||
/// Wraps [`GDALRegisterDriver()`](https://gdal.org/api/raster_c_api.html#_CPPv418GDALRegisterDriver11GDALDriverH) | ||
pub fn register_driver(driver: &Driver) -> usize { | ||
let index = unsafe { gdal_sys::GDALRegisterDriver(driver.c_driver) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not impossible, but it's pretty hard to end up needing to call this (because we do |
||
index | ||
.try_into() | ||
.expect("The returned index should be zero or positive") | ||
} | ||
|
||
/// Deregister the passed driver. | ||
/// | ||
/// Wraps [`GDALDeregisterDriver()`](https://gdal.org/api/raster_c_api.html#_CPPv420GDALDeregisterDriver11GDALDriverH) | ||
pub fn deregister_driver(driver: &Driver) { | ||
unsafe { | ||
gdal_sys::GDALDeregisterDriver(driver.c_driver); | ||
} | ||
} | ||
|
||
/// Register all known GDAL drivers. | ||
/// | ||
/// Wraps [`GDALAllRegister()`](https://gdal.org/api/raster_c_api.html#gdal_8h_1a9d40bc998bd6ed07ccde96028e85ae26) | ||
pub fn register_all() { | ||
unsafe { | ||
gdal_sys::GDALAllRegister(); | ||
} | ||
} | ||
|
||
/// Prevents the automatic registration of all known GDAL drivers when first calling create, open, etc. | ||
pub fn prevent_auto_registration() { | ||
START.call_once(|| {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
} | ||
|
||
/// Destroys the driver manager, i.e., unloads all drivers. | ||
/// | ||
/// Wraps [`GDALDestroyDriverManager()`](https://gdal.org/api/raster_c_api.html#_CPPv417GDALDestroyDriver11GDALDriverH) | ||
pub fn destroy() { | ||
unsafe { | ||
gdal_sys::GDALDestroyDriverManager(); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_driver_access() { | ||
let driver = DriverManager::get_driver_by_name("GTiff").unwrap(); | ||
assert_eq!(driver.short_name(), "GTiff"); | ||
assert_eq!(driver.long_name(), "GeoTIFF"); | ||
|
||
assert!(DriverManager::count() > 0); | ||
assert!(DriverManager::get_driver(0).is_ok()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
Driver
is to be deprecated, should these examples be updated?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
49fdfd4