Skip to content
Merged
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
21 changes: 18 additions & 3 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,32 @@ function write(path, t; geocolumns=GeoInterface.geometrycolumns(t), crs=GeoInter
dcrs = Dict("crs" => GeoFormatTypes.val(jcrs))
end
ct = Tables.columntable(t)
colmetadata = Dict{Symbol,Vector{Pair{String,String}}}()
metadata = DataAPI.metadata(t)
if !isnothing(metadata)
metadata = (k=>string(v) for (k,v) in pairs(metadata))
end
colmetadata = Dict{Symbol,Dict{String,String}}()
tcolmetadata = DataAPI.colmetadata(t)
if !isnothing(tcolmetadata)
for (k, v) in pairs(tcolmetadata)
colmetadata[k] = Dict(k=>string(v) for (k, v) in pairs(v))
end
end
for column in geocolumns
column in Tables.columnnames(t) || error("Geometry column $column not found in table")
data = Tables.getcolumn(t, column)
T = nonmissingtype(Tables.columntype(t, column))
GeoInterface.isgeometry(T) || error("Geometry in $column must support the GeoInterface")
ct = merge(ct, NamedTuple{(column,)}((Wrapper.(Ref(encoding), data),)))

colmetadata[column] = ["ARROW:extension:metadata" => JSON3.write(dcrs)]
geometa = Dict("ARROW:extension:metadata" => JSON3.write(dcrs))
if haskey(colmetadata, column)
merge!(colmetadata[column], geometa)
else
colmetadata[column] = geometa
end
end
Arrow.write(path, ct; colmetadata, kwargs...)
Arrow.write(path, ct; metadata, colmetadata, kwargs...)
end

"""
Expand Down
4 changes: 4 additions & 0 deletions src/type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ GeoInterface.isgeometry(::Type{<:Geometry}) = true
GeoInterface.ncoord(_, ::Geometry{X,D}) where {X,D} = D
GeoInterface.getcoord(::PointTrait, g::Geometry, i) = Base.getindex(g.geom, i)
GeoInterface.getcoord(::PointTrait, g::Geometry{X,D,T,<:GeoFormatTypes.MixedFormat}, i) where {X,D,T} = getcoord(PointTrait(), g.geom, i)
GeoInterface.x(t::PointTrait, g::Geometry) = getcoord(t, g, 1)
GeoInterface.y(t::PointTrait, g::Geometry) = getcoord(t, g, 2)
# GeoInterface.z(t::PointTrait, g::Geometry) = getcoord(t, g, 3) # TODO Fix!
GeoInterface.m(t::PointTrait, g::Geometry) = getcoord(t, g, 4)
GeoInterface.geomtrait(::Geometry{X}) where {X} = X()
GeoInterface.ngeom(_, g::Geometry) = length(g.geom)
GeoInterface.ngeom(t, g::Geometry{X,D,T,<:GeoFormatTypes.MixedFormat}) where {X,D,T} = ngeom(t, g.geom)
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Test
using GeoFormatTypes
using DataFrames
using Extents
using DataAPI

mkpath(joinpath(@__DIR__, "data/write"))

Expand Down Expand Up @@ -111,4 +112,15 @@ mkpath(joinpath(@__DIR__, "data/write"))
dfn = GeoArrow.read("simple.arrow")
@test GeoInterface.isgeometry(dfn.geometry[1])
end
@testset "Metadata" begin
df = DataFrame(a=1, geometry=[(1.,2.)])
DataAPI.metadata!(df, "author", "test")
DataAPI.colmetadata!(df, :a, "description", "A normal column")
DataAPI.colmetadata!(df, :geometry, "description", "A point geometry")
GeoArrow.write("metadata.arrow", df)
dfn = GeoArrow.read("metadata.arrow")
@test DataAPI.metadata(dfn)["author"] == "test"
@test DataAPI.colmetadata(dfn)[:a]["description"] == "A normal column"
@test DataAPI.colmetadata(dfn)[:geometry]["description"] == "A point geometry"
end
end
Loading