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

do not unnecessarily return a union in extract #790

Merged
merged 6 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 27 additions & 33 deletions src/methods/extract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ istrue(::_True) = true
istrue(::_False) = false

"""
extract(x, data; atol)
extract(x, data; [geometry, index, name, skipmissing, atol])
tiemvanderdeure marked this conversation as resolved.
Show resolved Hide resolved

Extracts the value of `Raster` or `RasterStack` at given points, returning
an iterable of `NamedTuple` with properties for `:geometry` and raster or
Expand Down Expand Up @@ -311,50 +311,44 @@ end

# _rowtype returns the complete NamedTuple type for a point row
# This code is entirely for types stability and performance.
_rowtype(x, g; kw...) = _rowtype(x, typeof(g); kw...)
_rowtype(x, g::Type; geometry, index, skipmissing, names, kw...) =
_rowtype(x, g, geometry, index, skipmissing, names)
function _rowtype(x, ::Type{G}, geometry::_True, index::_True, skipmissing::_True, names::NamedTuple{Names}) where {G,Names}
keys = (:geometry, :index, Names...,)
types = Tuple{G,Tuple{Int,Int},_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}
function _rowtype(x, g; geometry, index, skipmissing, names, kw...)
G = if skipmissing isa _True
nonmissingtype(typeof(g))
else
typeof(g)
end
_rowtype(x, G; geometry, index, skipmissing, names)
end
function _rowtype(x, ::Type{G}, geometry::_True, index::_False, skipmissing::_True, names::NamedTuple{Names}) where {G,Names}
keys = (:geometry, Names...,)
types = Tuple{G,_nametypes(x, names, skipmissing)...}
function _rowtype(x, g::Type; geometry, index, skipmissing, names, kw...)
keys = _rowkeys(geometry, index, names)
types = _rowtypes(x, g, geometry, index, skipmissing, names)
tiemvanderdeure marked this conversation as resolved.
Show resolved Hide resolved
NamedTuple{keys,types}
end
function _rowtype(x, ::Type{G}, geometry::_False, index::_True, skipmissing::_True, names::NamedTuple{Names}) where {G,Names}
keys = (:index, Names...,)
types = Tuple{Tuple{Int,Int},_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}

function _rowtypes(x, ::Type{G}, geometry::_True, index::_True, skipmissing::_True, names::NamedTuple{Names}) where {G,Names}
types = Tuple{G,Tuple{Int,Int},_nametypes(x, names, skipmissing)...}
end
function _rowtype(x, ::Type{G}, geometry::_False, index::_False, skipmissing::_True, names::NamedTuple{Names}) where {G,Names}
keys = Names
types = Tuple{_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}
function _rowtypes(x, ::Type{G}, geometry::_True, index::_False, skipmissing, names::NamedTuple{Names}) where {G,Names}
types = Tuple{G,_nametypes(x, names, skipmissing)...}
end
function _rowtype(x, ::Type{G}, geometry::_True, index::_True, skipmissing::_False, names::NamedTuple{Names}) where {G,Names}
keys = (:geometry, :index, names...,)
types = Tuple{Union{Missing,G},Union{Missing,Tuple{Int,Int}},_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}
function _rowtypes(x, ::Type{G}, geometry::_False, index::_True, skipmissing::_True, names::NamedTuple{Names}) where {G,Names}
types = Tuple{Tuple{Int,Int},_nametypes(x, names, skipmissing)...}
end
function _rowtype(x, ::Type{G}, geometry::_True, index::_False, skipmissing::_False, names::NamedTuple{Names}) where {G,Names}
keys = (:geometry, Names...,)
types = Tuple{Union{Missing,G},_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}
function _rowtypes(x, ::Type{G}, geometry::_True, index::_True, skipmissing::_False, names::NamedTuple{Names}) where {G,Names}
types = Tuple{G,Union{Missing,Tuple{Int,Int}},_nametypes(x, names, skipmissing)...}
end
function _rowtype(x, ::Type{G}, geometry::_False, index::_True, skipmissing::_False, names::NamedTuple{Names}) where {G,Names}
keys = (:index, Names...,)
function _rowtypes(x, ::Type{G}, geometry::_False, index::_True, skipmissing::_False, names::NamedTuple{Names}) where {G,Names}
types = Tuple{Union{Missing,Tuple{Int,Int}},_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}
end
function _rowtype(x, ::Type{G}, geometry::_False, index::_False, skipmissing::_False, names::NamedTuple{Names}) where {G,Names}
keys = Names
function _rowtypes(x, ::Type{G}, geometry::_False, index::_False, skipmissing, names::NamedTuple{Names}) where {G,Names}
types = Tuple{_nametypes(x, names, skipmissing)...}
NamedTuple{keys,types}
end

_rowkeys(geometry::_False, index::_False, names::NamedTuple{Names}) where Names = Names
_rowkeys(geometry::_True, index::_False, names::NamedTuple{Names}) where Names = (:geometry, Names...)
_rowkeys(geometry::_True, index::_True, names::NamedTuple{Names}) where Names = (:geometry, :index, Names...)
_rowkeys(geometry::_False, index::_True, names::NamedTuple{Names}) where Names = (:index, Names...)

@inline _skip_missing_rows(rows, ::Missing, names) =
Iterators.filter(row -> !any(ismissing, row), rows)
@inline _skip_missing_rows(rows, missingval, names) =
Expand Down
6 changes: 3 additions & 3 deletions test/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ createpoint(args...) = ArchGDAL.createpoint(args...)
(index = (1, 2), test = 2,)
])
# NamedTuple (reversed) points - tests a Table that iterates over points
T = @NamedTuple{geometry::Union{Missing,@NamedTuple{Y::Float64,X::Float64}},test::Union{Missing,Int64}}
T = @NamedTuple{geometry::Union{@NamedTuple{Y::Float64,X::Float64}},test::Union{Missing,Int64}}
@test all(extract(rast, [(Y=0.1, X=9.0), (Y=0.2, X=10.0), (Y=0.3, X=10.0)]) .=== T[
(geometry = (Y = 0.1, X = 9.0), test = 1)
(geometry = (Y = 0.2, X = 10.0), test = 4)
Expand All @@ -412,7 +412,7 @@ createpoint(args...) = ArchGDAL.createpoint(args...)
])
# Extract a polygon
p = ArchGDAL.createpolygon([[[8.0, 0.0], [11.0, 0.0], [11.0, 0.4], [8.0, 0.0]]])
T = @NamedTuple{geometry::Union{Missing,Tuple{Float64,Float64}},test::Union{Missing,Int64}}
T = @NamedTuple{geometry::Union{Tuple{Float64,Float64}},test::Union{Missing,Int64}}
@test all(extract(rast_m, p) .=== T[
(geometry = (9.0, 0.1), test = 1)
(geometry = (10.0, 0.1), test = 3)
Expand Down Expand Up @@ -447,7 +447,7 @@ createpoint(args...) = ArchGDAL.createpoint(args...)
(index = (2, 1), test = 3)
(index = (2, 2), test = missing)
])
T = @NamedTuple{geometry::Union{Missing,Tuple{Float64,Float64}},index::Union{Missing,Tuple{Int,Int}},test::Union{Missing,Int64}}
T = @NamedTuple{geometry::Union{Tuple{Float64,Float64}},index::Union{Missing,Tuple{Int,Int}},test::Union{Missing,Int64}}
@test all(extract(rast_m, p; index=true) .=== T[
(geometry = (9.0, 0.1), index = (1, 1), test = 1)
(geometry = (10.0, 0.1), index = (2, 1), test = 3)
Expand Down
Loading