Backends for GeoRegions.jl
This page gives a brief summary of the backend process that GeoRegions.j uses to save manually defined custom GeoRegion
s for projects.
JSON files
All relevant information for a GeoRegion can be divided into two components:
String descriptors that identify a GeoRegion and make each GeoRegion unique
Geometry, or a vector of Geometries, that describe the geographic region itself
Ever since ≥v8, I have decided that JSON is the appropriate format to store this information. Therefore, each GeoRegion
will have its own unique .json
file, stored in the location $path/.georegions/$ID.json
, where path
is the directory of your project.
We use JSON3.jl to write and read these .json
files, as JSON3.jl is able to take a struct
and parse it into a readable JSON string.
Let's show an example
using GeoRegions
geo = GeoRegion(
[3,8,5,3],[40,-20,14,40],
ID = "TSTjson", pID = "GLB", name = "Test Save GeoRegion",
save = true, path = joinpath(pwd(),"jsoneg")
)
The GeoRegion TSTjson has the following properties:
Region ID (ID) : TSTjson
Parent ID (pID) : GLB
Name (name) : Test Save GeoRegion
Bounds (N,S,E,W) : 40.0, -20.0, 8.0, 3.0
Rotation (θ) : 0.0
File Path (path) : /home/runner/work/GeoRegions.jl/GeoRegions.jl/docs/build/tutorials/projects/jsoneg/.georegions/TSTjson.json
Centroid (geometry.centroid) : [5.333333333333333, 11.333333333333334]
Shape (geometry.shape) : Vector{Point{2, Float64}}(4)
And let's read the string inside this file
open(geo.path) do file
read(file, String)
end
"{\"ID\":\"TSTjson\",\"pID\":\"GLB\",\"name\":\"Test Save GeoRegion\",\"rotation\":0.0,\"geometry\":{\"longitude\":[3.0,8.0,5.0,3.0],\"latitude\":[40.0,-20.0,14.0,40.0]}}"
Let's clean up the directory.
rm(dirname(geo.path),recursive=true)