Skip to content

Commit b89b0b6

Browse files
Improve performance for extract on point tables (#838)
* _get_geometries should not touch geometries * move _get_geometries to top
1 parent 1dd9a42 commit b89b0b6

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/methods/extract.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ function extract end
5858
names=_names(x), name=names, skipmissing=false, geometry=true, index=false, geometrycolumn=nothing, kw...
5959
)
6060
n = DD._astuple(name)
61-
_extract(x, data;
61+
geoms = _get_geometries(data, geometrycolumn)
62+
_extract(x, geoms;
6263
dims=DD.dims(x, DEFAULT_POINT_ORDER),
6364
names=NamedTuple{n}(n),
6465
# These keywords are converted to _True/_False for type stability later on
6566
# The @inline above helps constant propagation of the Bools
6667
geometry=_booltype(geometry),
6768
index=_booltype(index),
6869
skipmissing=_booltype(skipmissing),
69-
geometrycolumn,
7070
kw...
7171
)
7272
end
@@ -78,10 +78,9 @@ end
7878
function _extract(A::RasterStackOrArray, geom; names, kw...)
7979
_extract(A, GI.geomtrait(geom), geom; names, kw...)
8080
end
81-
function _extract(A::RasterStackOrArray, ::Nothing, data;
82-
names, skipmissing, geometrycolumn, kw...
81+
function _extract(A::RasterStackOrArray, ::Nothing, geoms;
82+
names, skipmissing, kw...
8383
)
84-
geoms = _get_geometries(data, geometrycolumn)
8584
T = _rowtype(A, eltype(geoms); names, skipmissing, kw...)
8685
# Handle empty / all missing cases
8786
(length(geoms) > 0 && any(!ismissing, geoms)) || return T[]

src/utils.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,12 @@ function _get_geometries(data, ::Nothing)
211211
data
212212
else
213213
trait = GI.trait(data)
214-
if GI.trait(data) isa GI.FeatureCollectionTrait
214+
if trait isa GI.FeatureCollectionTrait
215215
[GI.geometry(f) for f in GI.getfeature(data)]
216-
else
216+
elseif isnothing(trait)
217217
collect(data)
218+
else
219+
data
218220
end
219221
end
220222
# check if data iterates valid geometries before returning
@@ -240,9 +242,10 @@ function _get_geometries(data, geometrycolumn::NTuple{<:Any, <:Symbol})
240242
return points
241243
end
242244
function _check_geometries(geoms)
245+
!isnothing(GI.trait(geoms)) && return
243246
for g in geoms
244-
ismissing(g) || GI.geomtrait(g) !== nothing ||
245-
throw(ArgumentError("$g is not a valid GeoInterface.jl geometry"))
247+
ismissing(g) || !isnothing(GI.geomtrait(g)) ||
248+
throw(ArgumentError("$g is not a valid GeoInterface.jl geometry"))
246249
end
247250
return
248251
end

0 commit comments

Comments
 (0)