-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for saving and loading geotables in CSV format (#38)
* Add support for saving and loading geotables in CSV format * Apply suggestions
- Loading branch information
Showing
7 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
csvread(fname; coords, kwargs...) = georef(CSV.File(fname; kwargs...), coords) | ||
|
||
function csvwrite(fname, geotable; coords=nothing, kwargs...) | ||
dom = domain(geotable) | ||
tab = values(geotable) | ||
cols = Tables.columns(tab) | ||
names = Tables.columnnames(tab) | ||
Dim = embeddim(dom) | ||
|
||
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)")) | ||
end | ||
Symbol.(coords) | ||
end | ||
|
||
points = [centroid(dom, i) for i in 1:nelements(dom)] | ||
cpairs = map(cnames, 1:Dim) do name, d | ||
name => [coordinates(p)[d] for p in points] | ||
end | ||
|
||
pairs = (nm => Tables.getcolumn(cols, nm) for nm in names) | ||
newtab = (; cpairs..., pairs...) | ||
|
||
CSV.write(fname, newtab; kwargs...) | ||
end | ||
|
||
function _cnames(Dim, names) | ||
map(1:Dim) do d | ||
name = Symbol(:X, d) | ||
# make unique | ||
while name ∈ names | ||
name = Symbol(name, :_) | ||
end | ||
name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
X1,X2,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 | ||
3.0,3.3,0.6282647403425017,4,word4 | ||
4.0,4.4,0.9149290036628314,5,word5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters