From 87adabd3a246bc0d8b68dee4662dfe52e99681f7 Mon Sep 17 00:00:00 2001 From: Max Freudenberg <67329240+maxfreu@users.noreply.github.com> Date: Sun, 22 Oct 2023 14:22:17 +0200 Subject: [PATCH] add possibility to specify crop dimensions (#539) * allow specifying dims to crop * add tests * use dims keyword * fix unused variables --------- Co-authored-by: maxfreu Co-authored-by: Max Freudenberg --- src/methods/crop_extend.jl | 5 ++--- test/sources/gdal.jl | 8 +++++--- test/sources/grd.jl | 2 ++ test/sources/ncdatasets.jl | 2 ++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/methods/crop_extend.jl b/src/methods/crop_extend.jl index 2ae57366c..b153c2a64 100644 --- a/src/methods/crop_extend.jl +++ b/src/methods/crop_extend.jl @@ -92,16 +92,15 @@ function _crop_to(x, to; kw...) return _crop_to(x, ext; kw...) end end -_crop_to(A, to::RasterStackOrArray; kw...) = _crop_to(A, dims(to); kw...) +_crop_to(A, to::RasterStackOrArray; dims=DD.dims(to), kw...) = _crop_to(A, DD.dims(to, dims); kw...) function _crop_to(x, to::DimTuple; kw...) # We can only crop to sampled dims (e.g. not categorical dims like Band) sampled = reduce(to; init=()) do acc, d lookup(d) isa AbstractSampled ? (acc..., d) : acc end - return _crop_to(x, Extents.extent(to); kw...) + return _crop_to(x, Extents.extent(sampled); kw...) end function _crop_to(x, to::Extents.Extent; touches=false) - ds = dims(x, map(key2dim, keys(to))) # Take a view over the bounds _without_mapped_crs(x) do x1 if touches diff --git a/test/sources/gdal.jl b/test/sources/gdal.jl index 1eca831eb..635e4d4b6 100644 --- a/test/sources/gdal.jl +++ b/test/sources/gdal.jl @@ -109,13 +109,13 @@ gdalpath = maybedownload(url) @testset "other fields" begin # This file has an inorrect missing value - @test missingval(gdalarray) == nothing + @test missingval(gdalarray) === nothing @test metadata(gdalarray) isa Metadata{GDALsource,Dict{String,Any}} @test basename(metadata(gdalarray)["filepath"]) == "cea.tif" metadata(gdalarray)["filepath"] @test name(gdalarray) == :test @test label(gdalarray) == "test" - @test units(gdalarray) == nothing + @test units(gdalarray) === nothing @test crs(dims(gdalarray, Y)) isa WellKnownText @test crs(dims(gdalarray, X)) isa WellKnownText @test crs(gdalarray) isa WellKnownText @@ -152,6 +152,8 @@ gdalpath = maybedownload(url) @test size(trimmed) == (414, 514) cropped = Rasters.crop(a; to=trimmed) @test size(cropped) == (414, 514) + kwcropped = Rasters.crop(a; to=trimmed, dims=(X,)) + @test size(kwcropped) == (414, 515) # mind the 1px difference here, only cropped along x @test all(collect(cropped .=== trimmed)) extended = extend(cropped; to=a) @test all(collect(extended .=== a)) @@ -254,7 +256,7 @@ gdalpath = maybedownload(url) @test dims(geoA) isa Tuple{<:X,Y} @test refdims(geoA) isa Tuple{<:Band} @test metadata(geoA) == metadata(gdalarray) - @test missingval(geoA) == nothing + @test missingval(geoA) === nothing @test name(geoA) == :test end diff --git a/test/sources/grd.jl b/test/sources/grd.jl index 128bb38b4..97b78de77 100644 --- a/test/sources/grd.jl +++ b/test/sources/grd.jl @@ -100,6 +100,8 @@ grdpath = stem * ".gri" @test size(trimmed) == (81, 77, 3) cropped = crop(a; to=trimmed) @test size(cropped) == (81, 77, 3) + kwcropped = crop(a; to=trimmed, dims=(X,)) + @test size(kwcropped) == (81, size(a,Y), 3) @test all(collect(cropped .== trimmed)) extended = extend(cropped; to=a); @test all(collect(extended .== a)) diff --git a/test/sources/ncdatasets.jl b/test/sources/ncdatasets.jl index 7f25792b8..b3c8c4794 100644 --- a/test/sources/ncdatasets.jl +++ b/test/sources/ncdatasets.jl @@ -146,6 +146,8 @@ end @test size(trimmed) == (160, 169, 24) cropped = crop(a; to=trimmed) @test size(cropped) == (160, 169, 24) + kwcropped = crop(a; to=trimmed, dims=(X,)) + @test size(kwcropped) == (160, size(a,Y), 24) @test all(collect(cropped .=== trimmed)) extended = extend(cropped; to=a) @test all(collect(extended .=== a))