use crate::{Dataset, OxiGdalError, Result};
#[cfg(feature = "cloud")]
use crate::{DatasetFormat, DatasetInfo};
pub fn is_cloud_uri(path: &str) -> bool {
path.starts_with("s3://")
|| path.starts_with("gs://")
|| path.starts_with("az://")
|| path.starts_with("abfs://")
|| path.starts_with("http://")
|| path.starts_with("https://")
}
pub fn open_cloud_dataset(path: &str) -> Result<Dataset> {
if !is_cloud_uri(path) {
return Err(OxiGdalError::InvalidParameter {
parameter: "path",
message: format!("'{}' is not a recognised cloud URI", path),
});
}
#[cfg(feature = "cloud")]
{
open_cloud_with_feature(path)
}
#[cfg(not(feature = "cloud"))]
{
let _ = path;
Err(OxiGdalError::NotSupported {
operation: format!(
"Dataset::open(\"{path}\") — cloud URIs require the 'cloud' feature flag",
),
})
}
}
#[cfg(feature = "cloud")]
fn open_cloud_with_feature(path: &str) -> Result<Dataset> {
let guessed_format = DatasetFormat::from_extension(path);
let info = DatasetInfo {
format: guessed_format,
path: Some(path.to_string()),
width: None,
height: None,
band_count: 0,
layer_count: 0,
crs: None,
geotransform: None,
feature_count: None,
bounds: None,
};
validate_cloud_uri(path)?;
Ok(crate::Dataset::from_info(path.to_string(), info))
}
#[cfg(feature = "cloud")]
fn validate_cloud_uri(path: &str) -> Result<()> {
let _backend = oxigdal_cloud::CloudBackend::from_url(path).map_err(|e| {
OxiGdalError::InvalidParameter {
parameter: "path",
message: format!("invalid cloud URI '{}': {e}", path),
}
})?;
Ok(())
}