Skip to content

Commit

Permalink
Rename coordinate names in CSV files (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv authored Dec 6, 2023
1 parent 22b6ac6 commit 88fbfab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
19 changes: 15 additions & 4 deletions src/extra/csv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ function csvwrite(fname, geotable; coords=nothing, floatformat=nothing, kwargs..
names = Tables.columnnames(tab)
Dim = embeddim(dom)

if Dim > 3
throw(ArgumentError("embedding dimensions greater than 3 are not supported"))
end

cnames = if isnothing(coords)
_cnames(Dim, names)
else
if length(coords) Dim
throw(ArgumentError("the number of coordinates names must be equal to $Dim (embedding dimension)"))
throw(ArgumentError("the number of coordinate names must be equal to $Dim (embedding dimension)"))
end
Symbol.(coords)
end
Expand All @@ -36,9 +40,16 @@ _floatformat(val, format) = val
_floatformat(val::AbstractFloat, format) = isnothing(format) ? val : generate_formatter(format)(val)

function _cnames(Dim, names)
map(1:Dim) do d
name = Symbol(:X, d)
# make unique
cnames = if Dim == 1
[:x]
elseif Dim == 2
[:x, :y]
else
[:x, :y, :z]
end

# make unique
map(cnames) do name
while name names
name = Symbol(name, :_)
end
Expand Down
2 changes: 1 addition & 1 deletion test/data/points.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
X1,X2,variable,code,name
x,y,variable,code,name
0.0,0.0,0.07336635446929285,1,word1
1.0,1.1,0.34924148955718615,2,word2
2.0,2.2,0.6988266836914685,3,word3
Expand Down
27 changes: 16 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ end
end

@testset "CSV" begin
table = GeoIO.load(joinpath(datadir, "points.csv"), coords=["X1", "X2"])
table = GeoIO.load(joinpath(datadir, "points.csv"), coords=["x", "y"])
@test eltype(table.code) <: Integer
@test eltype(table.name) <: AbstractString
@test eltype(table.variable) <: Real
Expand Down Expand Up @@ -441,30 +441,30 @@ end
@testset "CSV" begin
file1 = joinpath(datadir, "points.csv")
file2 = joinpath(savedir, "points.csv")
gtb1 = GeoIO.load(file1, coords=[:X1, :X2])
GeoIO.save(file2, gtb1, coords=["X1", "X2"])
gtb2 = GeoIO.load(file2, coords=[:X1, :X2])
gtb1 = GeoIO.load(file1, coords=[:x, :y])
GeoIO.save(file2, gtb1, coords=["x", "y"])
gtb2 = GeoIO.load(file2, coords=[:x, :y])
@test gtb1 == gtb2
@test values(gtb1, 0) == values(gtb2, 0)

grid = CartesianGrid(2, 2, 2)
gtb1 = georef((; a=rand(8)), grid)
file = joinpath(savedir, "grid.csv")
GeoIO.save(file, gtb1)
gtb2 = GeoIO.load(file, coords=[:X1, :X2, :X3])
gtb2 = GeoIO.load(file, coords=[:x, :y, :z])
@test gtb1.a == gtb2.a
@test nelements(gtb1.geometry) == nelements(gtb2.geometry)
@test collect(gtb2.geometry) == centroid.(gtb1.geometry)

# make coordinate names unique
pset = PointSet(rand(Point2, 10))
gtb1 = georef((X1=rand(10), X2=rand(10)), pset)
gtb1 = georef((x=rand(10), y=rand(10)), pset)
file = joinpath(savedir, "pset.csv")
GeoIO.save(file, gtb1)
gtb2 = GeoIO.load(file, coords=[:X1_, :X2_])
gtb2 = GeoIO.load(file, coords=[:x_, :y_])
@test propertynames(gtb1) == propertynames(gtb2)
@test gtb1.X1 == gtb2.X1
@test gtb1.X2 == gtb2.X2
@test gtb1.x == gtb2.x
@test gtb1.y == gtb2.y
@test gtb1.geometry == gtb2.geometry

# float format
Expand All @@ -475,15 +475,20 @@ end
gtb1 = georef((; a), pset)
file = joinpath(savedir, "pset2.csv")
GeoIO.save(file, gtb1, floatformat="%.2f")
gtb2 = GeoIO.load(file, coords=[:X1, :X2])
gtb2 = GeoIO.load(file, coords=[:x, :y])
xf = [0.69, 0.99, 0.37, 0.18, 0.91, 0.71]
yf = [0.39, 0.44, 0.66, 0.35, 0.18, 0.84]
af = [0.14, 0.77, 0.46, 0.81, 0.56, 0.79]
@test gtb2.a == af
@test gtb2.geometry == PointSet(Point.(xf, yf))

# throw: invalid number of coordinate names
@test_throws ArgumentError GeoIO.save(file, gtb1, coords=["X", "Y", "Z"])
file = joinpath(savedir, "throw.csv")
gtb = georef((; a=rand(10)), rand(Point2, 10))
@test_throws ArgumentError GeoIO.save(file, gtb, coords=["x", "y", "z"])
# throw: geometries with more than 3 dimensions
gtb = georef((; a=rand(10)), rand(Point{4,Float64}, 10))
@test_throws ArgumentError GeoIO.save(file, gtb)
end

@testset "VTK" begin
Expand Down

0 comments on commit 88fbfab

Please sign in to comment.