Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to specify crop dimensions #539

Merged
merged 4 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/methods/crop_extend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ 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...)
function _crop_to(A, to::RasterStackOrArray; kw...)
maxfreu marked this conversation as resolved.
Show resolved Hide resolved
target_dims = get(kw, :dims, name.(dims(to)))
maxfreu marked this conversation as resolved.
Show resolved Hide resolved
maxfreu marked this conversation as resolved.
Show resolved Hide resolved
return _crop_to(A, dims(to, target_dims); kw...)
maxfreu marked this conversation as resolved.
Show resolved Hide resolved
end
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...)
end
function _crop_to(x, to::Extents.Extent; touches=false)
function _crop_to(x, to::Extents.Extent; touches=false, kw...)
ds = dims(x, map(key2dim, keys(to)))
# Take a view over the bounds
_without_mapped_crs(x) do x1
Expand Down
2 changes: 2 additions & 0 deletions test/sources/gdal.jl
Original file line number Diff line number Diff line change
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
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
Loading