Skip to content

Commit

Permalink
fix stack keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Jul 3, 2023
1 parent 70941cd commit 7f40be3
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ function RasterStack(layers::NamedTuple{<:Any,<:Tuple{Vararg{<:AbstractRaster}}}
end
# Single-file stack from a string
function RasterStack(filename::AbstractString;
dims=nothing, refdims=(), metadata=nothing, crs=nothing, mappedcrs=nothing,
layerdims=nothing, layermetadata=nothing, missingval=nothing,
source=nothing, name=nothing, keys=name, layersfrom=nothing,
resize=nothing, lazy=false, ext=nothing, dropband=true,
source=nothing, name=nothing, keys=name, resize=nothing, lazy=false, dropband=true, kw...
)
source = isnothing(source) ? _sourcetype(filename) : _sourcetype(source)
st = if isdir(filename)
Expand All @@ -248,25 +245,13 @@ function RasterStack(filename::AbstractString;
else
keys
end
RasterStack(joinpath.(Ref(filename), filenames); keys)
RasterStack(joinpath.(Ref(filename), filenames); lazy, kw...)
else
st = if haslayers(source)
crs = defaultcrs(source, crs)
mappedcrs = defaultmappedcrs(source, mappedcrs)
data, field_kw = _open(filename; source) do ds
dims = dims isa Nothing ? DD.dims(ds, crs, mappedcrs) : dims
refdims = refdims == () || refdims isa Nothing ? () : refdims
layerdims = layerdims isa Nothing ? DD.layerdims(ds) : layerdims
metadata = metadata isa Nothing ? DD.metadata(ds) : metadata
layermetadata = layermetadata isa Nothing ? DD.layermetadata(ds) : layermetadata
missingval = missingval isa Nothing ? Rasters.missingval(ds) : missingval
data = FileStack{source}(ds, filename; keys)
data, (; dims, refdims, layerdims, metadata, layermetadata, missingval)
end
RasterStack(data; field_kw...)
_layer_raster(filename; kw...)
else
# Band dims acts as layers
RasterStack(Raster(filename; lazy); layersfrom)
# layeresfrom (default: Band) dims acts as layers
RasterStack(Raster(filename; source); kw...)
end
# Maybe split the stack into separate arrays to remove extra dims.
if !(keys isa Nothing)
Expand All @@ -276,7 +261,7 @@ function RasterStack(filename::AbstractString;
end
end
st1 = lazy ? st : read(st)
if hasdim(st1, Band()) && size(st1, Band()) < 2
if dropband && hasdim(st1, Band()) && size(st1, Band()) == 1
if lazy
return view(st1, Band(1)) # TODO fix dropdims in DiskArrays
else
Expand All @@ -286,6 +271,27 @@ function RasterStack(filename::AbstractString;
return st1
end
end

function _layer_raster(filename;
dims=nothing, refdims=(), metadata=nothing, crs=nothing, mappedcrs=nothing,
layerdims=nothing, layermetadata=nothing, missingval=nothing,
source=nothing, name=nothing, keys=name, kw...
)
crs = defaultcrs(source, crs)
mappedcrs = defaultmappedcrs(source, mappedcrs)
data, field_kw = _open(filename; source) do ds
dims = dims isa Nothing ? DD.dims(ds, crs, mappedcrs) : dims
refdims = refdims == () || refdims isa Nothing ? () : refdims
layerdims = layerdims isa Nothing ? DD.layerdims(ds) : layerdims
metadata = metadata isa Nothing ? DD.metadata(ds) : metadata
layermetadata = layermetadata isa Nothing ? DD.layermetadata(ds) : layermetadata
missingval = missingval isa Nothing ? Rasters.missingval(ds) : missingval
data = FileStack{source}(ds, filename; keys)
data, (; dims, refdims, layerdims, metadata, layermetadata, missingval)
end
return RasterStack(data; field_kw..., kw...)
end

function RasterStack(A::Raster;
layersfrom=nothing, name=nothing, keys=name, metadata=metadata(A), refdims=refdims(A), kw...
)
Expand Down

0 comments on commit 7f40be3

Please sign in to comment.