Skip to content

Commit

Permalink
Upgrade compat to Julia 1.8. Allows for generic GeoInterface write.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Aug 13, 2023
1 parent 69664aa commit 2d35e97
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
version:
- '1.6'
- '1.8'
- '1'
- 'nightly'
os:
Expand Down
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GeoParquet"
uuid = "e99870d8-ce00-4fdd-aeee-e09192881159"
authors = ["Maarten Pronk <[email protected]>", "Julia Computing and contributors"]
version = "0.1.3"
authors = ["Maarten Pronk <[email protected]> and contributors."]
version = "0.1.4"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand All @@ -20,11 +20,11 @@ Extents = "0.1"
GeoFormatTypes = "0.4"
GeoInterface = "1"
JSON3 = "1.9"
Parquet2 = "0.1, 0.2"
Parquet2 = "0.2"
StructTypes = "1.8"
Tables = "1"
WellKnownGeometry = "0.2"
julia = "1.6"
julia = "1.8"

[extras]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,3 @@ test.parquet
- [ ] Better access to metadata in `read`
- [ ] Lazy read option
- [ ] Easier support for ProjJSON
We thank Julia Computing for supporting contributions to this package.
12 changes: 10 additions & 2 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@
write(ofn, t, columns=(:geom), crs::Union{GFT.ProjJSON,Nothing}=nothing, bbox::Union{Nothing,Vector{Float64}}=nothing; kwargs...)
Write a dataframe with a geometry column to a Parquet file. Keyword arguments are passed to Parquet2 writefile method.
The geometry column should be a `Vector{GeoFormat.WellKnownBinary}`.
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=(:geom,), crs::Union{GFT.ProjJSON,Nothing}=nothing, bbox::Union{Nothing,Vector{Float64}}=nothing; kwargs...)

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

columns = Dict{String,Any}()
tcols = Tables.columns(df)

# For on the fly conversion to WKB
ndf = DataFrame(df; copycols=false)

for column in geocolumns
column in Tables.columnnames(df) || error("Geometry column $column not found in table")
data = Tables.getcolumn(tcols, column)
GI.isgeometry(data[1]) || error("Geometry in $column must support the GeoInterface")
if !(data isa Vector{GFT.WellKnownBinary}) || !(data isa Vector{Vector{UInt8}})
ndf[!, column] = _getwkb.(data)
end
types = unique(typeof.(GI.geomtrait.(data)))
gtypes = getindex.(Ref(geowkb), types)
mc = MetaColumn(geometry_type=gtypes, bbox=bbox, crs=crs)
columns[String(column)] = mc
end

md = Dict("geo" => JSON3.write(GeoParquet.MetaRoot(columns=columns, primary_column=String(geocolumns[1]))))
Parquet2.writefile(ofn, df, metadata=md, compression_codec=:zstd, kwargs...)
Parquet2.writefile(ofn, ndf, metadata=md, compression_codec=:zstd, kwargs...)
ofn
end

Expand Down
4 changes: 4 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ function todict(obj::Dict{Symbol,Any})
end
return dict
end

_getwkb(x) = WellKnownGeometry.getwkb(x)
_getwkb(x::GFT.WellKnownBinary) = x
_getwkb(x::Vector{UInt8}) = GFT.WellKnownBinary(GFT.Geom(), x)
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ end
df = GeoParquet.read(fn)
df.test[1] == "test"

# Transparently convert columns to WKB
fn = "data/writec.parquet"
df = DataFrame(test="test", value=rand(2), geom=geom)
GeoParquet.write(fn, df)
ndf = GeoParquet.read(fn)
df.geom != ndf.geom # original is not mutated

fn = "data/example.parquet"
df = GeoParquet.read(fn)
GeoParquet.write("data/example_copy.parquet", df, (:geometry,))
Expand Down

0 comments on commit 2d35e97

Please sign in to comment.