Skip to content

Commit c2119c4

Browse files
committed
Add method for collection of Features as StructArray
1 parent bd792e8 commit c2119c4

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/metadata.jl

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,36 @@ Feature(x; kwargs...) = Feature(x, values(kwargs))
88
Base.getproperty(f::Feature, s::Symbol) = s == :data ? getfield(f, 1) : s == :rest ? getfield(f, 2) : getproperty(getfield(f, 2), s)
99
Base.propertynames(f::Feature) = (:data, propertynames(f.rest)...)
1010

11-
getnamestypes(::Type{Feature{T, Names, Types}}) where {T, Names, Types} = (T, Names, Types)
12-
13-
function StructArrays.staticschema(::Type{f}) where {f<:Feature}
14-
T, names, types = getnamestypes(f)
15-
NamedTuple{(:data, names...), Base.tuple_type_cons(T, types)}
11+
#todo better function names
12+
"""
13+
Put a collection(Array) of Features in a StructArray
14+
"""
15+
function s_coll(iter::Array)
16+
unnested_iter = Base.Generator(iter) do geom_meta
17+
geom = getfield(geom_meta, :data) # well, the public accessor for this
18+
metadata = getfield(geom_meta, :rest)
19+
(; geometry=geom, metadata...) # I think the GeometryBasics name for this field is `:position`
20+
end
21+
soa = fieldarrays(StructArray(unnested_iter))
22+
return Feature(soa.geometry; Base.tail(soa)...)
1623
end
1724

18-
function StructArrays.createinstance(::Type{f}, x, args...) where {f<:Feature}
19-
T, names, types = getnamestypes(f)
20-
Feature(x, NamedTuple{names, types}(args))
25+
function GeometryBasics.Feature(elements::AbstractVector{XX}; rest...) where {XX}
26+
isempty(rest) && return elements # no meta to add!
27+
n = length(elements)
28+
for (k, v) in rest
29+
if v isa AbstractVector
30+
mn = length(v)
31+
mn != n && error("Metadata array needs to have same length as data.
32+
Found $(n) data items, and $mn metadata items")
33+
else
34+
error("Metadata needs to be an array with the same length as data items. Found: $(typeof(v))")
35+
end
36+
end
37+
nt = values(rest)
38+
# get the first element to get the per element named tuple type
39+
ElementNT = typeof(map(first, nt))
40+
return StructArray((geometry = elements, nt...))
2141
end
2242

2343
#=---------------------------------------------------------------------------------------------
@@ -216,9 +236,4 @@ Base.size(x::MultiPolygonMeta) = size(metafree(x))
216236

217237
@meta_type(Mesh, mesh, AbstractMesh, Element <: Polytope)
218238
Base.getindex(x::MeshMeta, idx::Int) = getindex(metafree(x), idx)
219-
Base.size(x::MeshMeta) = size(metafree(x))
220-
221-
222-
# function Base.getproperty(b::F, s::Symbol)
223-
# s == :data ? getfield(b, 1) : getproperty(getfield(b, 2), s)
224-
# end
239+
Base.size(x::MeshMeta) = size(metafree(x))

0 commit comments

Comments
 (0)