Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Nov 24, 2023
1 parent a445143 commit 8f571bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/methods/extract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,22 @@ function extract(x::RasterStackOrArray, data;
end

function _extract(A::RasterStackOrArray, geom::Missing; names, kw...)
vals = map(_ -> missing, names)
[_maybe_add_fields(A, vals, missing, missing; kw...)]
[_maybe_add_fields(A, map(_ -> missing, names), missing, missing; kw...)]
end
_extract(A::RasterStackOrArray, geom; kw...) = _extract(A, GI.geomtrait(geom), geom; kw...)
function _extract(A::RasterStackOrArray, ::Nothing, geoms; skipmissing=false, kw...)
function _extract(A::RasterStackOrArray, ::Nothing, geoms; names, skipmissing=false, kw...)
# Handle empty / all missing cases
(length(geoms) > 0 && any(!ismissing, geoms)) || return typeof(_maybe_add_fields(A, map(_ -> missing, names), missing, missing; kw...))[]
geom1 = first(Base.skipmissing(geoms)) # TODO: will fail if `geoms` is empty or all missing
trait1 = GI.trait(geom1)
# We need to split out points from other geoms
# TODO this will fail with mixed point/geom vectors
if trait1 isa GI.PointTrait
rows = (_extract_point(A, g; kw...) for g in geoms)
rows = (_extract_point(A, g; names, kw...) for g in geoms)
return skipmissing ? collect(_skip_missing_rows(rows)) : collect(rows)
else
# This will be a list of vectors, we need to flatten it into one
rows = Iterators.flatten(_extract(A, g; kw...) for g in geoms)
rows = Iterators.flatten(_extract(A, g; names, skipmissing, kw...) for g in geoms)
return skipmissing ? collect(_skip_missing_rows(rows)) : collect(rows)
end
end
Expand Down Expand Up @@ -140,9 +141,8 @@ function _extract_point(x::RasterStackOrArray, point;
return _maybe_add_fields(x, layer_vals, geom, I; geometry, index)
end
function _extract_point(A::RasterStackOrArray, point::Missing; names, kw...)
vals = map(_ -> missing, names)
# Missing points return a single row
return _maybe_add_fields(A, vals, missing, missing; kw...)
return _maybe_add_fields(A, map(_ -> missing, names), missing, missing; kw...)
end

# Maybe add optional fields
Expand Down
3 changes: 3 additions & 0 deletions test/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ createpoint(args...) = ArchGDAL.createpoint(args...)
(geometry = (9.0, 0.1), index = CartesianIndex(1, 1), test = 1)
(geometry = (10.0, 0.1), index = CartesianIndex(2, 1), test = 3)
]
# Empty geoms
@test extract(rast, []) == NamedTuple{(:geometry, :test),Tuple{Missing,Missing}}[]
@test extract(rast, []; geometry=false) == NamedTuple{(:test,),Tuple{Missing}}[]
end

@testset "from stack" begin
Expand Down

0 comments on commit 8f571bf

Please sign in to comment.