Skip to content

Commit

Permalink
Update README & Refactor source code (#1)
Browse files Browse the repository at this point in the history
* Update README & Add the 'availableimages' function

* Refactor source code

* Add header
  • Loading branch information
eliascarv committed Oct 10, 2023
1 parent c34118b commit 4deb2d6
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 57 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 │ 040-2000-0-86765missing0-76-0-2906907000000408
│ Automatica │ A360 │ CE │ Operante │ 030-2000-0-81755missing0-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.
57 changes: 7 additions & 50 deletions src/GeoArtifacts.jl
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions src/gadm.jl
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions src/inmet.jl
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4deb2d6

Please sign in to comment.