1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
use crate::errors::*;
use crate::gdal_major_object::MajorObject;
use crate::utils::{_last_cpl_err, _last_null_pointer_err, _string, _string_array};
use gdal_sys::{self, CPLErr};
use std::ffi::CString;
/// General-Purpose Metadata API
///
/// The [`Metadata`] trait exposes a simple, general-purpose metadata model for various types
/// in the GDAL datamodel, including [`Driver`](crate::Driver),
/// [`Dataset`](crate::Dataset), [`RasterBand`](crate::raster::RasterBand),
/// and [`Layer`](crate::vector::Layer).
/// These data are comprised of key-value strings, organized under parent keys called "domains".
/// This includes the empty-string (`""`) root domain. There's even an `xml:` domain with it's own
/// world of content.
///
/// GDAL's metadata structures could be seen as peculiar, and there are driver-specific nuances to navigate,
/// but in scientifically rich datasets, or projects aiming for archival-quality content, its capabilities
/// can fulfill sophisticated requirements.
///
/// Reading the _Metadata_ section in the [GDAL Raster Data Model](https://gdal.org/user/raster_data_model.html#metadata)
/// document can help if you need to deep, fine-grained metadata access, or examples on how it is used.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{Dataset, Metadata};
/// # fn main() -> gdal::errors::Result<()> {
/// let dataset = Dataset::open("fixtures/tinymarble.tif")?;
/// // `description` on a `Dataset` is usually the file name.
/// let desc = dataset.description()?;
/// dbg!(desc);
///
/// // `IMAGE_STRUCTURE` is one of the standard domains
/// let md_domain = "IMAGE_STRUCTURE";
/// // The `INTERLEAVE` key provides a hint as to how pixel data is organized
/// let md_key = "INTERLEAVE";
/// // NB: `domain` comes **after** the `key`
/// let interleave = dataset.metadata_item(&md_key, &md_domain);
/// dbg!(interleave);
/// # Ok(())
/// # }
/// ```
pub trait Metadata: MajorObject {
/// For most [`crate::Dataset`]s, method returns this is the originating filename.
/// For [`crate::raster::RasterBand`]s it is a description (if supported) or `""`.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{Dataset, Metadata};
/// # fn main() -> gdal::errors::Result<()> {
/// let dataset = Dataset::open("fixtures/tinymarble.tif")?;
/// // `description` on a `Dataset` is usually the file name.
/// let desc = dataset.description()?;
/// assert_eq!(desc, "fixtures/tinymarble.tif");
/// # Ok(())
/// # }
/// ```
fn description(&self) -> Result<String> {
let c_res = unsafe { gdal_sys::GDALGetDescription(self.gdal_object_ptr()) };
if c_res.is_null() {
return Err(_last_null_pointer_err("GDALGetDescription"));
}
Ok(_string(c_res))
}
/// Metadata in GDAL is partitioned into namespaces, knows as "domains" in the
/// [GDAL Data Model](https://gdal.org/user/raster_data_model.html#metadata).
/// GDAL types with metadata (a.k.a. "Major Objects") have a default or "root" domain
/// identified by the empty string (`""`).
/// Specific "Major Object" types may have other conventionally recognized domains.
/// For example, in raster [`Dataset`][crate::Dataset]s you may come across the domains
/// `SUBDATASETS`, `IMAGE_STRUCTURE`, `RPC`, `IMAGERY`, `xml:`, etc.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{Dataset, Metadata};
/// # fn main() -> gdal::errors::Result<()> {
/// let dataset = Dataset::open("fixtures/labels.tif")?;
/// assert!(dataset.metadata_domains().contains(&"IMAGE_STRUCTURE".to_string()));
/// # Ok(())
/// # }
/// ```
fn metadata_domains(&self) -> Vec<String> {
let mut domains = Vec::new();
let c_res = unsafe { gdal_sys::GDALGetMetadataDomainList(self.gdal_object_ptr()) };
if !c_res.is_null() {
domains.append(&mut _string_array(c_res));
}
unsafe { gdal_sys::CSLDestroy(c_res) };
domains
}
/// Get all the metadata values within the given `domain`. Returns `None` if
/// domain is not defined.
/// Entries in the returned `Vec<String>` are formatted as "Name=value" pairs
///
/// # Arguments
/// * `domain` – the domain of interest. Use `""` for the default domain.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{Dataset, Metadata};
/// # fn main() -> gdal::errors::Result<()> {
/// let dataset = Dataset::open("fixtures/labels.tif")?;
/// assert_eq!(dataset.metadata_domain("IMAGE_STRUCTURE").unwrap(),vec!["INTERLEAVE=BAND"] );
/// # Ok(())
/// # }
/// ```
fn metadata_domain(&self, domain: &str) -> Option<Vec<String>> {
let mut metadata = Vec::new();
if let Ok(c_domain) = CString::new(domain.to_owned()) {
let c_res =
unsafe { gdal_sys::GDALGetMetadata(self.gdal_object_ptr(), c_domain.as_ptr()) };
if c_res.is_null() {
return None;
} else {
metadata.append(&mut _string_array(c_res));
}
}
Some(metadata)
}
/// Get a single metadata entry, as indicated by `key` and `domain`.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{Dataset, Metadata};
/// # fn main() -> gdal::errors::Result<()> {
/// let dataset = Dataset::open("fixtures/labels.tif")?;
/// assert_eq!(dataset.metadata_item("INTERLEAVE", "IMAGE_STRUCTURE").unwrap(),"BAND");
/// # Ok(())
/// # }
/// ```
fn metadata_item(&self, key: &str, domain: &str) -> Option<String> {
if let Ok(c_key) = CString::new(key.to_owned()) {
if let Ok(c_domain) = CString::new(domain.to_owned()) {
let c_res = unsafe {
gdal_sys::GDALGetMetadataItem(
self.gdal_object_ptr(),
c_key.as_ptr(),
c_domain.as_ptr(),
)
};
if !c_res.is_null() {
return Some(_string(c_res));
}
}
}
None
}
/// Set a metadata item in given `domain` at given `key`.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{DriverManager, Metadata};
/// # fn main() -> gdal::errors::Result<()> {
/// let mut driver = DriverManager::get_driver_by_name("MEM")?;
/// let (domain, key, value) = ("FOOBAR", "fake", "data");
/// assert!(driver.metadata_domain(domain).is_none());
/// driver.set_metadata_item(key, value, domain)?;
/// assert!(driver.metadata_domain(domain).is_some());
/// assert_eq!(driver.metadata_item(key, domain), Some(value.to_string()));
/// # Ok(())
/// # }
/// ```
fn set_metadata_item(&mut self, key: &str, value: &str, domain: &str) -> Result<()> {
let c_key = CString::new(key)?;
let c_domain = CString::new(domain)?;
let c_value = CString::new(value)?;
let c_res = unsafe {
gdal_sys::GDALSetMetadataItem(
self.gdal_object_ptr(),
c_key.as_ptr(),
c_value.as_ptr(),
c_domain.as_ptr(),
)
};
if c_res != CPLErr::CE_None {
return Err(_last_cpl_err(c_res));
}
Ok(())
}
/// For Datasets this sets the dataset name; normally
/// application code should not set the "description" for
/// GDALDatasets. For RasterBands it is actually a description
/// (if supported) or `""`.
fn set_description(&mut self, description: &str) -> Result<()> {
let c_description = CString::new(description.to_owned())?;
unsafe { gdal_sys::GDALSetDescription(self.gdal_object_ptr(), c_description.as_ptr()) };
Ok(())
}
/// Get an iterator over metadata entries, across all domains.
///
/// # Example
///
/// ```rust, no_run
/// use gdal::{Dataset, Metadata, MetadataEntry};
/// # fn main() -> gdal::errors::Result<()> {
/// let dataset = Dataset::open("fixtures/tinymarble.tif")?;
/// for MetadataEntry { domain, key, value } in dataset.metadata() {
/// let domain = if domain == "" { "DEFAULT".to_string() } else { domain };
/// println!("{domain}: {key}={value}");
/// }
/// # Ok(())
/// # }
/// ```
/// Output:
/// ```text
/// IMAGE_STRUCTURE: INTERLEAVE=PIXEL
/// DEFAULT: AREA_OR_POINT=Area
/// ...
/// DERIVED_SUBDATASETS: DERIVED_SUBDATASET_1_NAME=DERIVED_SUBDATASET:LOGAMPLITUDE:fixtures/tinymarble.tif
/// DERIVED_SUBDATASETS: DERIVED_SUBDATASET_1_DESC=log10 of amplitude of input bands from fixtures/tinymarble.tif
/// ```
fn metadata(&self) -> MetadataIter
where
Self: Sized,
{
MetadataIter::new(self)
}
}
/// Standalone metadata entry, as returned by iterator from [`Metadata::metadata`].
///
/// Defined by it's parent `domain`, and `key`/`value` pair.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct MetadataEntry {
pub domain: String,
pub key: String,
pub value: String,
}
impl MetadataEntry {
pub fn new<D, K, V>(domain: D, key: K, value: V) -> Self
where
D: Into<String>,
K: Into<String>,
V: Into<String>,
{
Self {
domain: domain.into(),
key: key.into(),
value: value.into(),
}
}
/// Determine if this entry is from the default domain, which is named `""`.
pub fn is_default_domain(&self) -> bool {
self.domain.is_empty()
}
}
/// Metadata iterator state
pub struct MetadataIter<'a> {
stream: Box<dyn Iterator<Item = MetadataEntry> + 'a>,
}
/// Iterator over metadata entries
impl<'a> MetadataIter<'a> {
fn new<P: Metadata>(parent: &'a P) -> Self {
let stream = parent
.metadata_domains()
.into_iter()
.flat_map(move |domain| {
let keyvals = parent.metadata_domain(&domain).unwrap_or_default();
keyvals.into_iter().filter_map(move |keyval| {
keyval
.split_once('=')
.map(|(key, value)| MetadataEntry::new(domain.clone(), key, value))
})
});
Self {
stream: Box::new(stream),
}
}
}
impl<'a> Iterator for MetadataIter<'a> {
type Item = MetadataEntry;
fn next(&mut self) -> Option<Self::Item> {
self.stream.next()
}
}
#[cfg(test)]
mod tests {
use crate::metadata::MetadataEntry;
use crate::test_utils::fixture;
use crate::*;
#[test]
fn test_get_dataset_driver() {
let dataset = Dataset::open(fixture("tinymarble.tif")).unwrap();
let driver = dataset.driver();
assert_eq!(driver.short_name(), "GTiff");
assert_eq!(driver.long_name(), "GeoTIFF");
}
#[test]
fn test_get_description() {
let driver = DriverManager::get_driver_by_name("mem").unwrap();
assert_eq!(driver.description().unwrap(), "MEM".to_string());
}
#[test]
fn test_get_metadata_domains() {
let dataset = Dataset::open(fixture("tinymarble.tif")).unwrap();
let mut domains = dataset.metadata_domains();
domains.retain(|d| !d.is_empty());
domains.sort();
domains.dedup();
assert_eq!(
domains,
vec![
"COLOR_PROFILE",
"DERIVED_SUBDATASETS",
"IMAGE_STRUCTURE",
"xml:XMP",
]
);
}
#[test]
fn test_get_metadata_domain() {
let dataset = Dataset::open(fixture("tinymarble.tif")).unwrap();
let domain = "None";
let meta = dataset.metadata_domain(domain);
assert_eq!(meta, None);
let domain = "IMAGE_STRUCTURE";
let meta = dataset.metadata_domain(domain);
assert_eq!(meta, Some(vec!(String::from("INTERLEAVE=PIXEL"))));
}
#[test]
fn test_get_metadata_item() {
let dataset = Dataset::open(fixture("tinymarble.tif")).unwrap();
let key = "None";
let domain = "None";
let meta = dataset.metadata_item(key, domain);
assert_eq!(meta, None);
let key = "INTERLEAVE";
let domain = "IMAGE_STRUCTURE";
let meta = dataset.metadata_item(key, domain);
assert_eq!(meta, Some(String::from("PIXEL")));
}
#[test]
fn test_set_metadata_item() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let mut dataset = driver.create("", 1, 1, 1).unwrap();
let key = "Test_Key";
let domain = "Test_Domain";
let value = "Test_Value";
let result = dataset.set_metadata_item(key, value, domain);
assert!(result.is_ok());
let result = dataset.metadata_item(key, domain);
assert_eq!(Some(value.to_owned()), result);
}
#[test]
fn test_set_description() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let mut band = dataset.rasterband(1).unwrap();
let description = "A merry and cheerful band description";
assert_eq!(band.description().unwrap(), "");
band.set_description(description).unwrap();
assert_eq!(band.description().unwrap(), description);
}
#[test]
fn test_md_iter() {
// Driver metadata...
let driver = DriverManager::get_driver_by_name("GTiff").unwrap();
driver.metadata().any(|e| e.key == "LIBGEOTIFF");
// Dataset metadata...
let ds = Dataset::open(fixture("m_3607824_se_17_1_20160620_sub.tif")).unwrap();
assert_eq!(ds.metadata_item("AREA_OR_POINT", ""), Some("Area".into()));
assert!(ds
.metadata()
.any(|e| e == MetadataEntry::new("", "AREA_OR_POINT", "Area")));
assert!(ds.metadata().any(|e| e.domain == "DERIVED_SUBDATASETS"));
// RasterBand metadata...
let ds = Dataset::open(fixture("labels.tif")).unwrap();
let band = ds.rasterband(1).unwrap();
assert!(band.metadata().any(|e| e.key == "CLASSES"));
}
}