From 88fbfabac063d0dff9e2f16e98408e9039d49e53 Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Wed, 6 Dec 2023 16:04:28 -0300 Subject: [PATCH] Rename coordinate names in CSV files (#40) --- src/extra/csv.jl | 19 +++++++++++++++---- test/data/points.csv | 2 +- test/runtests.jl | 27 ++++++++++++++++----------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/extra/csv.jl b/src/extra/csv.jl index d20aeea..b9998c0 100644 --- a/src/extra/csv.jl +++ b/src/extra/csv.jl @@ -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 @@ -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 diff --git a/test/data/points.csv b/test/data/points.csv index 2a5c86d..6f7c042 100644 --- a/test/data/points.csv +++ b/test/data/points.csv @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 54cfc65..e3b78e7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -441,9 +441,9 @@ 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) @@ -451,20 +451,20 @@ end 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 @@ -475,7 +475,7 @@ 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] @@ -483,7 +483,12 @@ end @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