Skip to content
Merged
Changes from 3 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
36 changes: 32 additions & 4 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@ struct Parquet2Driver <: Driver end
struct QuackIODriver <: Driver end

"""
write(ofn, t, columns=(:geom), crs::Union{GFT.ProjJSON,Nothing}=nothing, bbox::Union{Nothing,Vector{Float64}}=nothing; kwargs...)
write(ofn, t; geometrycolumn::Symbol, crs::Union{GFT.ProjJSON,Nothing}=nothing, bbox::Union{Nothing,Vector{Float64}}=nothing; kwargs...)

Write a dataframe with a geometry column to a Parquet file. Returns `ofn` on succes. Keyword arguments are passed to Parquet2 writefile method.
The geometry column should be a `Vector{GeoFormat.WellKnownBinary}` or its elements should support GeoInterface.
You can construct one with WellKnownGeometry for geometries that support GeoInterface.
"""
function write(ofn::Union{AbstractString,Parquet2.FilePathsBase.AbstractPath}, df, geocolumns=GI.geometrycolumns(df), crs::Union{GFT.ProjJSON,Nothing}=nothing, bbox::Union{Nothing,Vector{Float64}}=nothing; kwargs...)
function write(ofn::Union{AbstractString,Parquet2.FilePathsBase.AbstractPath}, df, geocolumns=nothing, crs::Union{GFT.ProjJSON,Nothing}=nothing, bbox::Union{Nothing,Vector{Float64}}=nothing; geometrycolumn = nothing, kwargs...)

if isnothing(geometrycolumn)
if isnothing(geocolumns)
# the happy path, everything is fine
geometrycolumn = GI.geometrycolumns(df)
else # the user has provided something to geocolumns
Base.depwarn("The `geocolumns` positional argument to `GeoParquet.write` is deprecated, please use the `geometrycolumn` keyword argument instead.", :var"GeoParquet.write")
geometrycolumn = geocolumns
end
else
if isnothing(geocolumns)
error("""
It looks like you invoked `GeoParquet.write` with three arguments, but also
provided a `geometrycolumns` keyword argument.

The third positional argument in this method, `geocolumns`, is deprecated.
Please pass geometry column information as a Symbol or Tuple of Symbols to
the `geometrycolumn` keyword argument instead, such that you only have two
positional arguments as input.
""")
end
end

# Tables.istable(df) || throw(ArgumentError("`df` must be a table"))

columns = Dict{String,Any}()
Expand All @@ -19,7 +41,13 @@ function write(ofn::Union{AbstractString,Parquet2.FilePathsBase.AbstractPath}, d
# For on the fly conversion to WKB
ndf = DataFrame(df; copycols=false)

for column in geocolumns
geometrycolumns = if geometrycolumn isa Tuple || geometrycolumn isa Vector
geometrycolumn
else
(geometrycolumn,)
end

for column in geometrycolumns
column in Tables.columnnames(tcols) || error("Geometry column $column not found in table")
data = Tables.getcolumn(tcols, column)
GI.isgeometry(first(data)) || error("Geometry in $column must support the GeoInterface")
Expand All @@ -28,7 +56,7 @@ function write(ofn::Union{AbstractString,Parquet2.FilePathsBase.AbstractPath}, d
ndf[!, column] = _getwkb.(data)
end
types = typeof.(unique(GI.geomtrait.(data)))
gtypes = getindex.(Ref(geowkb), types)
gtypes = getindex.((geowkb,), types)
mc = MetaColumn(geometry_types=gtypes, bbox=bbox, crs=crs)
columns[String(column)] = mc
end
Expand Down
Loading