Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
metasim committed Jan 5, 2024
1 parent 0adef4b commit b3d68e3
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/raster/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::vec::IntoIter;
#[cfg(feature = "ndarray")]
use ndarray::Array2;

#[cfg_attr(not(feature = "ndarray"), allow(rustdoc::broken_intra_doc_links))]
/// `Buffer<T>` manages cell values in in raster I/O operations.
///
/// It conceptually represents a 2-D array backed by a `Vec<T>` with row-major organization
Expand Down
20 changes: 12 additions & 8 deletions src/raster/warp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn create_and_reproject<P: AsRef<Path>>(
let dst_wkt = CString::new(dst_srs.to_wkt()?)?;
// Format the source projection, if specified.
let src_wkt = options
.src_projection()
.src_spatial_ref()
.map(|s| s.to_wkt())
.transpose()?
.map(CString::new)
Expand Down Expand Up @@ -82,7 +82,8 @@ pub fn create_and_reproject<P: AsRef<Path>>(
}

// See https://lists.osgeo.org/pipermail/gdal-dev/2023-November/057887.html for
// why this is required. To get around it We should rewrite this function to use the lower
// why this is required. To get around it We should rewrite this function to use the
// lower-level `GDALWarp` API.
if options.dst_nodata().is_some() {
let ds = Dataset::open(dst_file)?;
for b in 1..=ds.raster_count() {
Expand All @@ -98,18 +99,18 @@ pub fn create_and_reproject<P: AsRef<Path>>(

/// Reproject one dataset into another dataset.
///
/// Assumes destination dataset is properly sized and setup with a [`SpatialRef`][crate::SpatialRef],
/// [`GeoTransform`][crate::GeoTransform], [`Rasterband`][crate::Rasterband], etc.
/// Assumes destination dataset is properly sized and setup with a [`SpatialRef`],
/// [`GeoTransform`][crate::GeoTransform], [`RasterBand`][crate::raster::RasterBand], etc.
///
/// See [`reproject_options`] for a more flexible alternative.
/// See [`create_and_reproject`] for a more flexible alternative.
pub fn reproject_into(
src: &Dataset,
dst: &mut Dataset,
options: &ReprojectIntoOptions,
) -> Result<()> {
// Format the source projection, if specified.
let src_wkt = options
.src_projection()
.src_spatial_ref()
.map(|s| s.to_wkt())
.transpose()?
.map(CString::new)
Expand All @@ -118,7 +119,7 @@ pub fn reproject_into(

// Format the destination projection, if specified.
let dst_wkt = options
.src_projection()
.src_spatial_ref()
.map(|s| s.to_wkt())
.transpose()?
.map(CString::new)
Expand Down Expand Up @@ -150,7 +151,8 @@ pub fn reproject_into(
}

// See https://lists.osgeo.org/pipermail/gdal-dev/2023-November/057887.html for
// why this is required. To get around it We should rewrite this function to use the lower
// why this is required. To get around it We should rewrite this function to use the
// lower-level `GDALWarp` API.
if options.dst_nodata().is_some() {
for b in 1..=dst.raster_count() {
let mut rb = dst.rasterband(b)?;
Expand Down Expand Up @@ -185,6 +187,7 @@ mod tests {
opts.with_output_format("GTiff")
.with_dst_nodata(255.0)
.warp_options_mut()
.with_initial_value(InitValue::NoData)
.with_resampling_alg(WarpResampleAlg::NearestNeighbour);

create_and_reproject(&ds, &dest, &dst_srs, &opts)?;
Expand Down Expand Up @@ -228,6 +231,7 @@ mod tests {
let mut opts = ReprojectIntoOptions::default();
opts.with_dst_nodata(255.0)
.warp_options_mut()
.with_initial_value(InitValue::NoData)
.with_resampling_alg(WarpResampleAlg::NearestNeighbour);

reproject_into(&source_ds, &mut dest_ds, &opts)?;
Expand Down
84 changes: 42 additions & 42 deletions src/raster/warp/reproject_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::raster::warp::GdalWarpOptions;
use crate::spatial_ref::SpatialRef;

/// Injects methods associated with specifying warp no-data values.
macro_rules! common_nodata_methods {
macro_rules! nodata_accessors {
() => {
/// Specify the source no-data value.
///
Expand Down Expand Up @@ -32,8 +32,14 @@ macro_rules! common_nodata_methods {
}

/// Injects methods around [`GdalWarpOptions`].
macro_rules! common_warp_methods {
macro_rules! warp_options_accessors {
() => {
/// Set the general Warp options.
pub fn with_warp_options(&mut self, warp_options: GdalWarpOptions) -> &mut Self {
self.warp_options = warp_options;
self
}

/// Fetch an immutable reference to the general Warp options.
pub fn warp_options(&self) -> &GdalWarpOptions {
&self.warp_options
Expand Down Expand Up @@ -79,7 +85,26 @@ macro_rules! common_warp_methods {
};
}

/// Settings for [`create_and_reproject`].
macro_rules! src_sr_accessors {
() => {
/// Set the source spatial reference system.
///
/// If not specified here, the source [`SpatialRef`] is read from the source dataset.
///
/// If specified here, any [`SpatialRef`] in the source dataset is overridden.
pub fn with_src_spatial_ref(&mut self, srs: SpatialRef) -> &mut Self {
self.src_srs = Some(srs);
self
}

/// Fetch the source spatial reference system, if set.
pub fn src_spatial_ref(&self) -> Option<&SpatialRef> {
self.src_srs.as_ref()
}
};
}

/// Settings for [`create_and_reproject`][super::create_and_reproject].
#[derive(Debug, Clone, Default)]
pub struct ReprojectOptions {
warp_options: GdalWarpOptions,
Expand Down Expand Up @@ -113,20 +138,6 @@ impl ReprojectOptions {
self.max_error
}

/// Specify the source projection.
///
/// If unset, the source projection is read from the source dataset.
/// If set, the source projection os overridden.
pub fn with_src_projection(&mut self, srs: &SpatialRef) -> &mut Self {
self.src_srs = Some(srs.clone());
self
}

/// Fetch the specified source projection, if any.
pub fn src_projection(&self) -> Option<&SpatialRef> {
self.src_srs.as_ref()
}

/// Explicitly specify output raster format.
///
/// This is equivalent to the `-of <format>` CLI flag accepted by many GDAL tools.
Expand Down Expand Up @@ -157,11 +168,12 @@ impl ReprojectOptions {
self.output_format.clone()
}

common_nodata_methods!();
common_warp_methods!();
src_sr_accessors!();
nodata_accessors!();
warp_options_accessors!();
}

/// Settings for [`reproject_into`].
/// Settings for [`reproject_into`][super::reproject_into].
#[derive(Debug, Clone, Default)]
pub struct ReprojectIntoOptions {
warp_options: GdalWarpOptions,
Expand Down Expand Up @@ -195,34 +207,22 @@ impl ReprojectIntoOptions {
self.max_error
}

/// Specify the source projection.
/// Set the destination spatial reference system.
///
/// If unset, the source projection is read from the source dataset.
/// If set, the source projection os overridden.
pub fn with_src_projection(&mut self, srs: &SpatialRef) -> &mut Self {
self.src_srs = Some(srs.clone());
self
}

/// Fetch the specified source projection, if any.
pub fn src_projection(&self) -> Option<&SpatialRef> {
self.src_srs.as_ref()
}

/// Specify the destination projection.
/// If not specified here, the source [`SpatialRef`] is read from the destination dataset.
///
/// If unset, the destination projection is read from the destination dataset.
/// If set, the destination projection os overridden.
pub fn with_dst_projection(&mut self, srs: &SpatialRef) -> &mut Self {
self.dst_srs = Some(srs.clone());
/// If specified here, any [`SpatialRef`] in the destination dataset is overridden.
pub fn with_dst_spatial_ref(&mut self, srs: SpatialRef) -> &mut Self {
self.dst_srs = Some(srs);
self
}

/// Fetch the specified destination projection, if any.
pub fn dst_projection(&self) -> Option<&SpatialRef> {
/// Fetch the destination spatial reference system, if set.
pub fn dst_spatial_ref(&self) -> Option<&SpatialRef> {
self.dst_srs.as_ref()
}

common_nodata_methods!();
common_warp_methods!();
src_sr_accessors!();
nodata_accessors!();
warp_options_accessors!();
}
1 change: 1 addition & 0 deletions src/raster/warp/resample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl WarpResampleAlg {
pub fn to_gdal(self) -> GDALResampleAlg::Type {
self as GDALResampleAlg::Type
}

pub fn from_gdal(alg: GDALResampleAlg::Type) -> Result<Self> {
Ok(match alg {
GDALResampleAlg::GRA_NearestNeighbour => WarpResampleAlg::NearestNeighbour,
Expand Down
Loading

0 comments on commit b3d68e3

Please sign in to comment.