Skip to content

Commit

Permalink
Merge pull request #56 from Mikumikunisiteageru/add-wc-elev
Browse files Browse the repository at this point in the history
feat: add `WorldClim{Elevation}` (#55)
  • Loading branch information
rafaqz authored Feb 28, 2023
2 parents d5fba93 + d093fed commit cfb728f
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 12 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ RasterDataSources downloads raster data for local use or for integration into ot
[Rasters.jl](https://github.com/rafaqz/Rasters.jl). The collection is largely focussed on datasets relevant
to ecology, but will have a lot of crossover with other sciences.

Currently sources include :

| Source | URL | Status |
| --------- | ---------------------------------------- |--------------------------------------|
| CHELSA | https://chelsa-climate.org | BioClim, Future BioClim and Climate |
| WorldClim | https://www.worldclim.org | Climate, Weather and BioClim |
| EarthEnv | http://www.earthenv.org | LandCover and HabitatHeterogeneity |
| AWAP | http://www.bom.gov.au/jsp/awap/index.jsp | Complete |
| ALWB | http://www.bom.gov.au/water/landscape/ | Complete |
| SRTM | https://www2.jpl.nasa.gov/srtm/ | Complete |
| MODIS | https://modis.ornl.gov | Complete (beta) |
Currently sources include:

| Source | URL | Status |
| --------- | ---------------------------------------- | ---------------------------------------- |
| CHELSA | https://chelsa-climate.org | BioClim, Future BioClim and Climate |
| WorldClim | https://www.worldclim.org | Climate, Weather, BioClim, and Elevation |
| EarthEnv | http://www.earthenv.org | LandCover and HabitatHeterogeneity |
| AWAP | http://www.bom.gov.au/jsp/awap/index.jsp | Complete |
| ALWB | http://www.bom.gov.au/water/landscape/ | Complete |
| SRTM | https://www2.jpl.nasa.gov/srtm/ | Complete |
| MODIS | https://modis.ornl.gov | Complete (beta) |

Please open an issue if you need more datasets added, or (even better) open a pull request
following the form of the other datasets where possible.
Expand Down
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ WorldClim
getraster(T::Type{WorldClim{BioClim}}, layers::Union{Tuple,Int,Symbol}; res)
getraster(T::Type{WorldClim{Weather}}, layers::Union{Tuple,Symbol}; date)
getraster(T::Type{WorldClim{Climate}}, layers::Union{Tuple,Symbol}; month, res)
getraster(T::Type{WorldClim{Elevation}}, layers::Union{Tuple,Symbol}; month, res)
```

## MODIS
Expand All @@ -76,6 +77,7 @@ RasterDataSources.RasterDataSet
BioClim
Climate
Weather
Elevation
LandCover
HabitatHeterogeneity
Future
Expand Down
3 changes: 2 additions & 1 deletion src/RasterDataSources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import JSON.Parser as JP

export WorldClim, CHELSA, EarthEnv, AWAP, ALWB, SRTM, MODIS

export BioClim, Climate, Weather, LandCover, HabitatHeterogeneity
export BioClim, Climate, Weather, Elevation, LandCover, HabitatHeterogeneity

export Future, CMIP5, CMIP6

Expand Down Expand Up @@ -60,6 +60,7 @@ include("worldclim/shared.jl")
include("worldclim/bioclim.jl")
include("worldclim/climate.jl")
include("worldclim/weather.jl")
include("worldclim/elevation.jl")

include("chelsa/shared.jl")
include("chelsa/bioclim.jl")
Expand Down
11 changes: 11 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ See the [`getraster`](@ref) docs for implementation details.
"""
struct Weather <: RasterDataSet end

"""
Elevation <: RasterDataSet
Elevation datasets.
Currently implemented for WorldClim as `WorldClim{Elevation}`.
See the [`getraster`](@ref) docs for implementation details.
"""
struct Elevation <: RasterDataSet end

"""
LandCover <: RasterDataSet
Expand Down
49 changes: 49 additions & 0 deletions src/worldclim/elevation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
layers(::Type{WorldClim{Elevation}}) = (:elev,)

"""
getraster(T::Type{WorldClim{Elevation}}, [layer::Union{Tuple,Symbol}]; res::String="10m") => Union{Tuple,AbstractVector,String}
Download [`WorldClim`](@ref) [`Elevation`](@ref) data.
# Arguments
- `layer`: `Symbol` or `Tuple` of `Symbol` from `$(layers(WorldClim{Elevation}))`.
Without a `layer` argument, all layers will be downloaded, and a `NamedTuple` of paths returned.
# Keywords
- `res`: `String` chosen from $(resolutions(WorldClim{Elevation})), "10m" by default.
Returns the filepath/s of the downloaded or pre-existing files.
"""
function getraster(T::Type{WorldClim{Elevation}}, layers::Union{Tuple,Symbol};
res::String=defres(T)
)
_getraster(T, layers, res)
end

_getraster(T::Type{WorldClim{Elevation}}, layers::Tuple, res) = _map_layers(T, layers, res)
function _getraster(T::Type{WorldClim{Elevation}}, layer::Symbol, res)
_check_layer(T, layer)
_check_res(T, res)
raster_path = rasterpath(T, layer; res)
if !isfile(raster_path)
zip_path = zippath(T, layer; res)
_maybe_download(zipurl(T, layer; res), zip_path)
mkpath(dirname(raster_path))
raster_name = rastername(T, layer; res)
zf = ZipFile.Reader(zip_path)
write(raster_path, read(_zipfile_to_read(raster_name, zf)))
close(zf)
end
return raster_path
end

rasterpath(T::Type{<:WorldClim{Elevation}}, layer; kw...) =
joinpath(rasterpath(T), rastername(T, layer; kw...))
rastername(T::Type{<:WorldClim{Elevation}}, key; res) = "wc2.1_$(res)_elev.tif"
zipname(T::Type{<:WorldClim{Elevation}}, key; res) = "wc2.1_$(res)_elev.zip"
zipurl(T::Type{<:WorldClim{Elevation}}, key; res) =
joinpath(WORLDCLIM_URI, "base", zipname(T, key; res))
zippath(T::Type{<:WorldClim{Elevation}}, key; res) =
joinpath(rasterpath(T), "zips", zipname(T, key; res))
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ end
@time @safetestset "worldclim bioclim" begin include("worldclim-bioclim.jl") end
@time @safetestset "worldclim climate" begin include("worldclim-climate.jl") end
@time @safetestset "worldclim weather" begin include("worldclim-weather.jl") end
@time @safetestset "worldclim elevation" begin include("worldclim-elevation.jl") end
# SRTM SSL certs have expired
# @time @safetestset "srtm" begin include("srtm.jl") end
@time @safetestset "modis utilities" begin include("modis-utilities.jl") end
Expand Down
16 changes: 16 additions & 0 deletions test/worldclim-elevation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using RasterDataSources, URIs, Test, Dates
using RasterDataSources: rastername, rasterpath, zipurl, zipname, zippath

@testset "WorldClim Elevation" begin
zip_url = URI(scheme="https", host="biogeo.ucdavis.edu", path="/data/worldclim/v2.1/base/wc2.1_10m_elev.zip")
@test zipurl(WorldClim{Elevation}, :elev; res="10m") == zip_url
@test zipname(WorldClim{Elevation}, :elev; res="10m") == "wc2.1_10m_elev.zip"

raster_file = joinpath(ENV["RASTERDATASOURCES_PATH"], "WorldClim", "Elevation", "wc2.1_10m_elev.tif")
@test rasterpath(WorldClim{Elevation}, :elev; res="10m") == raster_file
@test getraster(WorldClim{Elevation}; res="10m") == (elev=raster_file,)
@test getraster(WorldClim{Elevation}, (:elev,); res="10m") == (elev=raster_file,)
@test getraster(WorldClim{Elevation}, :elev; res="10m") == raster_file
@test getraster(WorldClim{Elevation}, [:elev]; res="10m") == (elev=raster_file,)
@test isfile(raster_file)
end

0 comments on commit cfb728f

Please sign in to comment.