Skip to content

Commit 7e2d063

Browse files
authored
allow crop to work with unformatted dimensions, and test (#714)
1 parent 2adfc09 commit 7e2d063

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Diff for: src/methods/crop_extend.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ function crop(xs; to=nothing, kw...)
8080
map(l -> crop(l; to, kw...), xs)
8181
end
8282
end
83-
crop(x::RasterStackOrArray; to, geometrycolumn=nothing, kw...) = _crop_to(x, to; geometrycolumn, kw...)
83+
crop(x::RasterStackOrArray; to, kw...) = _crop_to(x, to; kw...)
8484

8585
# crop `A` to values of dims of `to`
86-
function _crop_to(x, to; geometrycolumn, kw...)
86+
function _crop_to(x, to; geometrycolumn=nothing, kw...)
8787
ext = _extent(to; geometrycolumn)
8888
if isnothing(ext)
8989
if isnothing(dims(to))
@@ -99,7 +99,7 @@ _crop_to(A, to::RasterStackOrArray; dims=DD.dims(to), kw...) = _crop_to(A, DD.di
9999
_crop_to(x, to::Dimension; kw...) = _crop_to(x, (to,); kw...)
100100
function _crop_to(x, to::DimTuple; touches=false, kw...)
101101
# We can only crop to sampled dims (e.g. not categorical dims like Band)
102-
sampled = reduce(to; init=()) do acc, d
102+
sampled = reduce(format(to); init=()) do acc, d
103103
lookup(d) isa AbstractSampled ? (acc..., d) : acc
104104
end
105105
return _crop_to(x, Extents.extent(sampled); touches)

Diff for: test/methods.jl

+14
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,20 @@ end
564564
@test all(extended_r .=== ga_r)
565565
@test all(map(==, lookup(extended_d), lookup(extended)))
566566

567+
@testset "unformatted dimension works in crop" begin
568+
xdim, ydim = X(1.0:0.2:2.0), Y(1.0:1:2.0)
569+
A = Raster(rand((X(1.0:0.2:4.0), ydim)))
570+
@test lookup(crop(A; to=xdim), X) == 1.0:0.2:2.0
571+
572+
A = Raster(rand((X(1.0:0.2:4.0), ydim)))
573+
@test lookup(crop(A; to=xdim), X) == 1.0:0.2:2.0
574+
575+
A = Raster(ones((X(1.0:0.2:1.4), ydim)); missingval=0.0)
576+
extend(A; to=xdim)
577+
@test lookup(extend(A; to=xdim), X) == 1.0:0.2:2.0
578+
@test extend(A; to=xdim) == [1.0 1.0; 1.0 1.0; 1.0 1.0; 0.0 0.0; 0.0 0.0; 0.0 0.0]
579+
end
580+
567581
@testset "to polygons" begin
568582
A1 = Raster(zeros(X(-20:-5; sampling=Points()), Y(0:30; sampling=Points())))
569583
A2 = Raster(ones(X(-20:-5; sampling=Points()), Y(0:30; sampling=Points())))

0 commit comments

Comments
 (0)