Skip to content

Commit

Permalink
add possibility to specify crop dimensions (#539)
Browse files Browse the repository at this point in the history
* allow specifying dims to crop

* add tests

* use dims keyword

* fix unused variables

---------

Co-authored-by: maxfreu <[email protected]>
Co-authored-by: Max Freudenberg <[email protected]>
  • Loading branch information
3 people authored Oct 22, 2023
1 parent 1a61491 commit 87adabd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/methods/crop_extend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions test/sources/gdal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions test/sources/grd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions test/sources/ncdatasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 87adabd

Please sign in to comment.