Skip to content

WIP: support attribute tables #987

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,17 @@ function _raster(ds;
data_out, dims_out, metadata_out, missingval_out = _open(source, ds; name=name1, group, mod=nothing) do var
metadata_out = isnokw(metadata) ? _metadata(var) : metadata
missingval_out = _read_missingval_pair(var, metadata_out, missingval)

# Generate mod for scaling
mod = isnokw(mod) ? _mod(eltype(var), metadata_out, missingval_out; scaled, coerce) : mod
mod = if isnokw(mod)
if !raw && isfile(filename .* ".vat.dbf") # todo: how to detect a RAT/VAT?
Copy link
Collaborator

@asinghvi17 asinghvi17 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this be able to pick up ArchGDAL rats. Maybe that goes in the extension?

Copy link
Collaborator

@asinghvi17 asinghvi17 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because those have .tif.rat or something which is a different format...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I have to think about how to check for all of that. I'm not sure if there are more formats than just those two? And if different file extensions than .tif could have a RAT or something similar?

_tablemod(filename .* ".vat.dbf", missingval_out, name) # name or name1?
else
_mod(eltype(var), metadata_out, missingval_out; scaled, coerce)
end
else
mod
end
# Define or load the data array
data_out = if lazy
# Define a lay FileArray
Expand Down
42 changes: 36 additions & 6 deletions src/modifieddiskarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,28 @@ function _resolve_mod_eltype(::Type{T}, missingval, scale, offset) where T
return T3
end

missingval(m::Mod) = m.missingval
missingval(m::NoMod) = m.missingval
# Modifies by dictionary lookup - similar to categorical array
struct AttributeMod{T, Mi, Di} <: AbstractModifications{T}
table::NamedTuple
column::Symbol
dict::Di
missingval::Mi

_inner_missingval(m::Mod) = _inner_missingval(m.missingval)
function AttributeMod(table::NamedTuple, column::Symbol, missingval::Mi) where {Mi}
keycol = identity.(first(table))
valcol = identity.(table[column])
dict = DD.OrderedCollections.LittleDict(keycol, valcol) |> DD.OrderedCollections.freeze
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

new{eltype(table[column]), Mi, typeof(dict)}(table, column, dict, missingval)
end
end

## Todo in a DBFTables extension
_tablemod(args...) = error()

## missingval handling
missingval(m::AbstractModifications) = m.missingval

_inner_missingval(m::AbstractModifications) = _inner_missingval(m.missingval)
_inner_missingval(mv) = mv
_inner_missingval(mv::Pair) = mv[1]

Expand Down Expand Up @@ -78,7 +96,7 @@ DiskArrays.haschunks(A::ModifiedDiskArray) = DiskArrays.haschunks(parent(A))
DiskArrays.eachchunk(A::ModifiedDiskArray) = DiskArrays.eachchunk(parent(A))

function DiskArrays.readblock!(
A::ModifiedDiskArray{<:Any,<:Any,<:Any,<:Mod}, outer_block, I::AbstractVector...
A::ModifiedDiskArray{<:Any,<:Any,<:Any,<:AbstractModifications}, outer_block, I::AbstractVector...
)
if isdisk(parent(A))
inner_block = similar(outer_block, eltype(parent(A)))
Expand Down Expand Up @@ -121,6 +139,11 @@ function DiskArrays.writeblock!(
parent(A)[I...] = block
end
end
function DiskArrays.writeblock!(
A::ModifiedDiskArray{<:Any,<:Any,<:Any,<:AttributeMod}, block, I::AbstractVector...
)
error("Writing using an Attribute Table is not supported")
end

Base.@assume_effects :foldable function _applymod(x, m::Mod)
if _ismissing(x, _inner_missingval(m))
Expand All @@ -130,6 +153,13 @@ Base.@assume_effects :foldable function _applymod(x, m::Mod)
end
end
Base.@assume_effects :foldable _applymod(x, ::NoMod) = x
Base.@assume_effects :foldable function _applymod(x, m::AttributeMod)
if _ismissing(x, _inner_missingval(m))
_outer_missingval(m)
else
m.dict[x]
end
end

_ismissing(x, mv) = ismissing(x) || x === mv
_ismissing(x, ::Nothing) = ismissing(x)
Expand Down Expand Up @@ -198,7 +228,7 @@ function get_scale(metadata, scaled::Bool)
end

_mod_eltype(::AbstractArray{T}, ::NoMod) where T = T
_mod_eltype(::AbstractArray, m::Mod{T}) where T = T
_mod_eltype(::AbstractArray, m::AbstractModifications{T}) where T = T

_mod_inverse_eltype(::AbstractArray{T}, ::NoMod) where T = T
_mod_inverse_eltype(::AbstractArray{T}, m::Mod) where T =
Expand Down Expand Up @@ -248,4 +278,4 @@ function _read_missingval_pair(var, metadata, missingval)
# Otherwise: detect missing value and convert it to `missingval`
Rasters.missingval(var, metadata) => missingval
end
end
end
Loading