Skip to content

Commit

Permalink
test and fix edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Dec 10, 2024
1 parent 5aa8914 commit 41ec62d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,17 @@ function Raster(filename::AbstractString, dims::Tuple{<:Dimension,<:Dimension,Va
)::Raster
Raster(filename; dims, kw...)
end
Raster(ext::Extents.Extent; kw...) = Raster{Float64}(ext::Extents.Extent; kw...)
function Raster{T}(ext::Extents.Extent;
size=nothing, res=nothing, crs=nothing, mappedcrs=nothing, sampling=Points(),
kw...
) where T
@show sampling
dims = _extent2dims(ext; size, res, crs, mappedcrs, sampling)
Raster{T}(undef, dims; kw...)
end
function Raster(filename::AbstractString;
source=nokw,
source=nokw,
kw...
)
source = _sourcetrait(filename, source)
Expand Down
26 changes: 14 additions & 12 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,29 +140,31 @@ function _without_mapped_crs(f, st::AbstractRasterStack, mappedcrs::GeoFormat)
end
end

function _extent2dims(to; size=nothing, res=nothing, crs=nothing, kw...)
_extent2dims(to, size, res, crs)
function _extent2dims(to;
size=nothing, res=nothing, crs=nothing, sampling=Intervals(Start()), kw...
)
_extent2dims(to, size, res, crs, sampling)
end
function _extent2dims(to::Extents.Extent, size::Nothing, res::Nothing, crs)
function _extent2dims(to::Extents.Extent, size::Nothing, res::Nothing, crs, sampling)
isnothing(res) && throw(ArgumentError("Pass either `size` or `res` keywords or a `Tuple` of `Dimension`s for `to`."))
end
function _extent2dims(to::Extents.Extent, size, res, crs)
function _extent2dims(to::Extents.Extent, size, res, crs, sampling)
isnothing(res) || _size_and_res_error()
end
function _extent2dims(to::Extents.Extent{K}, size::Nothing, res::Real, crs) where K
function _extent2dims(to::Extents.Extent{K}, size::Nothing, res::Real, crs, sampling) where K
tuple_res = ntuple(_ -> res, length(K))
_extent2dims(to, size, tuple_res, crs)
_extent2dims(to, size, tuple_res, crs, sampling)
end
function _extent2dims(to::Extents.Extent{K}, size::Nothing, res, crs) where K
function _extent2dims(to::Extents.Extent{K}, size::Nothing, res, crs, sampling) where K
ranges = map(values(to), res) do bounds, r
start, stop_closed = bounds
stop_open = stop_closed + maybe_eps(stop_closed; grow=false)
length = ceil(Int, (stop_open - start) / r)
range(; start, step=r, length)
end
return _extent2dims(to, ranges, crs)
return _extent2dims(to, ranges, crs, sampling)
end
function _extent2dims(to::Extents.Extent{K}, size, res::Nothing, crs) where K
function _extent2dims(to::Extents.Extent{K}, size, res::Nothing, crs, sampling) where K
if size isa Int
size = ntuple(_ -> size, length(K))
end
Expand All @@ -172,14 +174,14 @@ function _extent2dims(to::Extents.Extent{K}, size, res::Nothing, crs) where K
step = (stop_open - start) / length
range(; start, step, length)
end
return _extent2dims(to, ranges, crs)
return _extent2dims(to, ranges, crs, sampling)
end
function _extent2dims(to::Extents.Extent{K}, ranges, crs) where K
function _extent2dims(to::Extents.Extent{K}, ranges, crs, sampling) where K
emptydims = map(name2dim, K)
lookups = map(ranges) do range
Projected(range;
order=ForwardOrdered(),
sampling=Intervals(Start()),
sampling,
span=Regular(step(range)),
crs,
)
Expand Down
15 changes: 15 additions & 0 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ end
A6 = falses(x, y)
@test A6 isa Raster{Bool,2}
@test A6 == [false false; false false]

A7 = Raster(extent(A1); res=1.0)
@test A7 isa Raster{Float64,2}
@test size(A7) == (2, 2)
@test dims(A8) == dims(A1)

A8 = Raster{Int}(extent(A1); res=1.0)
@test A8 isa Raster{Int,2}
@test size(A8) == (2, 2)
@test dims(A8) == dims(A1)

A9 = Raster{Bool}(extent(A1); res=1.0, sampling=Intervals(Center()))
@test A9 isa Raster{Bool,2}
@test size(A9) == (2, 2)
@test sampling(A9, X) == Intervals(Center())
end


Expand Down

0 comments on commit 41ec62d

Please sign in to comment.