From 4deb2d61a05001366e8da97b788fbbca6c1451ba Mon Sep 17 00:00:00 2001 From: Elias Carvalho <73039601+eliascarv@users.noreply.github.com> Date: Tue, 10 Oct 2023 08:59:49 -0300 Subject: [PATCH] Update README & Refactor source code (#1) * Update README & Add the 'availableimages' function * Refactor source code * Add header --- README.md | 37 +++++++++++++++++++++++++++++ src/GeoArtifacts.jl | 57 ++++++--------------------------------------- src/gadm.jl | 36 ++++++++++++++++++++++++++++ src/inmet.jl | 32 +++++++++++++++++++++++++ test/runtests.jl | 14 +++++------ 5 files changed, 119 insertions(+), 57 deletions(-) create mode 100644 src/gadm.jl create mode 100644 src/inmet.jl diff --git a/README.md b/README.md index 493bcdf..bef4b8c 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,40 @@ [![Coverage](https://codecov.io/gh/JuliaEarth/GeoArtifacts.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaEarth/GeoArtifacts.jl) Julia package for loading geospatial artifacts, e.g. datasets, from different databases. + +# Usage + +### (Down)loading data from GADM + +The `GADM.get` function (down)loads data from the GADM dataset: + +```julia +julia> GADM.get("BRA", depth = 1) +``` + +### (Down)loading data from INMET + +```julia +julia> INMET.stations() +565×13 GeoTable over 565 PointSet{3,Float64} +┌────────────┬────────────┬───────────┬─────────────┬─────────────┬────────────────┬─────────────────┬─────────────────────────── +│ TP_ESTACAO │ CD_ESTACAO │ SG_ESTADO │ CD_SITUACAO │ CD_DISTRITO │ CD_OSCAR │ DT_FIM_OPERACAO │ CD_WSI ⋯ +│ Textual │ Textual │ Textual │ Textual │ Textual │ Textual │ Missing │ Textual ⋯ +│ [NoUnits] │ [NoUnits] │ [NoUnits] │ [NoUnits] │ [NoUnits] │ [NoUnits] │ [NoUnits] │ [NoUnits] ⋯ +├────────────┼────────────┼───────────┼─────────────┼─────────────┼────────────────┼─────────────────┼─────────────────────────── +│ Automatica │ A422 │ BA │ Pane │ 04 │ 0-2000-0-86765 │ missing │ 0-76-0-2906907000000408 ⋯ +│ Automatica │ A360 │ CE │ Operante │ 03 │ 0-2000-0-81755 │ missing │ 0-76-0-2300200000000446 ⋯ +│ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ ⋱ +└────────────┴────────────┴───────────┴─────────────┴─────────────┴────────────────┴─────────────────┴─────────────────────────── + 5 columns and 563 rows omitted +``` + +### Loading data from GeoStatsImages.jl + +```julia +julia> GeoStatsImages.geostatsimage(identifier) +``` + +where `identifier` can be any of the strings listed with the command `GeoStatsImages.available()`. + +Please read the docstrings for more details. diff --git a/src/GeoArtifacts.jl b/src/GeoArtifacts.jl index 7b43d94..18fbce6 100644 --- a/src/GeoArtifacts.jl +++ b/src/GeoArtifacts.jl @@ -1,57 +1,14 @@ -module GeoArtifacts +# ----------------------------------------------------------------- +# Licensed under the MIT License. See LICENSE in the project root. +# ----------------------------------------------------------------- -using GeoIO -using Meshes -using Unitful -using GeoTables +module GeoArtifacts -using GADM -using INMET using GeoStatsImages -""" - gadm(country, subregions...; depth=0, ϵ=nothing, - min=3, max=typemax(Int), maxiter=10, fix=true) - -(Down)load GADM table using `GADM.get` and convert -the `geometry` column to Meshes.jl geometries. - -The `depth` option can be used to return tables for subregions -at a given depth starting from the given region specification. - -The options `ϵ`, `min`, `max` and `maxiter` are forwarded to the -`decimate` function from Meshes.jl to reduce the number of vertices. - -The option `fix` can be used to fix orientation and degeneracy -issues with polygons. -""" -function gadm(country, subregions...; depth=0, ϵ=nothing, min=3, max=typemax(Int), maxiter=10, fix=true, kwargs...) - table = GADM.get(country, subregions...; depth, kwargs...) - geotable = GeoIO.asgeotable(table, fix) - dom = domain(geotable) - newdom = decimate(dom, ϵ; min, max, maxiter) - georef(values(geotable), newdom) -end - -""" - inmetstations(kind=:automatic) - -Return INMET stations of given kind. There are two kinds of stations: `:automatic` and `:manual`. -""" -function inmetstations(kind=:automatic) - df = INMET.stations(kind) - names = propertynames(df) - cnames = [:VL_LONGITUDE, :VL_LATITUDE, :VL_ALTITUDE] - fnames = setdiff(names, cnames) - feats = df[:, fnames] - coords = ustrip.(df[:, cnames]) - points = map(eachrow(coords)) do row - x, y, z = row - Point(x, y, z) - end - georef(feats, PointSet(points)) -end +include("gadm.jl") +include("inmet.jl") -export gadm, geostatsimage, inmetstations +export GADM, INMET, GeoStatsImages end diff --git a/src/gadm.jl b/src/gadm.jl new file mode 100644 index 0000000..d01ee8f --- /dev/null +++ b/src/gadm.jl @@ -0,0 +1,36 @@ +# ----------------------------------------------------------------- +# Licensed under the MIT License. See LICENSE in the project root. +# ----------------------------------------------------------------- + +module GADM + +using GeoIO +using Meshes +using GeoTables + +import GADM as GADMData + +""" + GADM.get(country, subregions...; depth=0, ϵ=nothing, + min=3, max=typemax(Int), maxiter=10, fix=true) + +(Down)load GADM table and convert the `geometry` column to Meshes.jl geometries. + +The `depth` option can be used to return tables for subregions +at a given depth starting from the given region specification. + +The options `ϵ`, `min`, `max` and `maxiter` are forwarded to the +`decimate` function from Meshes.jl to reduce the number of vertices. + +The option `fix` can be used to fix orientation and degeneracy +issues with polygons. +""" +function get(country, subregions...; depth=0, ϵ=nothing, min=3, max=typemax(Int), maxiter=10, fix=true, kwargs...) + table = GADMData.get(country, subregions...; depth, kwargs...) + geotable = GeoIO.asgeotable(table, fix) + dom = domain(geotable) + newdom = decimate(dom, ϵ; min, max, maxiter) + georef(values(geotable), newdom) +end + +end diff --git a/src/inmet.jl b/src/inmet.jl new file mode 100644 index 0000000..d1f9d58 --- /dev/null +++ b/src/inmet.jl @@ -0,0 +1,32 @@ +# ----------------------------------------------------------------- +# Licensed under the MIT License. See LICENSE in the project root. +# ----------------------------------------------------------------- + +module INMET + +using Meshes +using Unitful +using GeoTables + +import INMET as INMETData + +""" + INMET.stations(kind=:automatic) + +Return INMET stations of given kind. There are two kinds of stations: `:automatic` and `:manual`. +""" +function stations(kind=:automatic) + df = INMETData.stations(kind) + names = propertynames(df) + cnames = [:VL_LONGITUDE, :VL_LATITUDE, :VL_ALTITUDE] + fnames = setdiff(names, cnames) + feats = df[:, fnames] + coords = ustrip.(df[:, cnames]) + points = map(eachrow(coords)) do row + x, y, z = row + Point(x, y, z) + end + georef(feats, PointSet(points)) +end + +end diff --git a/test/runtests.jl b/test/runtests.jl index c990fa8..e832c2b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,26 +3,26 @@ using Meshes using Test @testset "GeoArtifacts.jl" begin - # @testset "gadm" begin - # gtb = gadm("SVN", depth=1, ϵ=0.04) + # @testset "GADM" begin + # gtb = GADM.get("SVN", depth=1, ϵ=0.04) # @test length(gtb.geometry) == 12 - # gtb = gadm("QAT", depth=1, ϵ=0.04) + # gtb = GADM.get("QAT", depth=1, ϵ=0.04) # @test length(gtb.geometry) == 7 - # gtb = gadm("ISR", depth=1) + # gtb = GADM.get("ISR", depth=1) # @test length(gtb.geometry) == 7 # end - @testset "inmetstations" begin + @testset "INMET" begin # automatic stations - gtb = inmetstations() + gtb = INMET.stations() @test all(isequal("Automatica"), gtb.TP_ESTACAO) @test gtb.geometry isa PointSet @test embeddim(gtb.geometry) == 3 # manual stations - gtb = inmetstations(:manual) + gtb = INMET.stations(:manual) @test all(isequal("Convencional"), gtb.TP_ESTACAO) @test gtb.geometry isa PointSet @test embeddim(gtb.geometry) == 3