Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyhanson committed Jun 7, 2022
0 parents commit 52e47de
Show file tree
Hide file tree
Showing 16 changed files with 3,400 additions and 0 deletions.
1 change: 1 addition & 0 deletions .Rprofile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
source("renv/activate.R")
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# R profile files
*.Rproj.user
*.Rhistory
.Rproj.user

# log files
*.log

# data files
*.tif
*.zip
*.tar
*.tar.bil
*.bil
*.hdr
*.prj
*.aux.xml
*.stx
*.qml
*.tar.gz

# system files
.directory

# do not ignore the following files
!.gitkeep
!*/
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
all: readme data

data:
R CMD BATCH --no-restore --no-save code/lumbierres-habitat-data.R

clean:
rm -f results/*.tif

readme:
R --slave -e "rmarkdown::render('README.Rmd')"

.PHONY: all data readme
122 changes: 122 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
output:
rmarkdown::github_document:
html_preview: no
---

<!--- README.md is generated from README.Rmd. Please edit that file -->

# Habitat classification data from Lumbierres *et al.* 2021

[![lifecycle](https://img.shields.io/badge/Lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![DOI](https://img.shields.io/badge/DOI-TODO-blue.svg)](https://doi.org/TODO)

```{r, include = FALSE}
knitr::opts_chunk$set(fig.path = "figures/README-", fig.align = "center")
```

This repository contains code to generate global habitat classification data (based on Lumbierres *et al.* 2021). Specifically, source data are downloaded from the [Zenodo Digital Archive](https://doi.org/10.5281/zenodo.5146072), and then projected to the World Behrmann coordinate reference system using (100 m resolution; [ESRI:4326 coordinate reference system](https://epsg.io/54017)). A processed version of the data can be downloaded from the [Zenodo Digital Repository](https://zenodo.org/badge/latestdoi/TODO). **All credit should go to Lumbierres *et al.* (2021), please cite their work if you use this dataset.**

```{r "preliminary_processing", echo = FALSE, message = FALSE, warning = FALSE}
# load packages
library(dplyr)
library(terra)
library(ggplot2)
# find file paths
data_path <- dir("results", "^.*tif$", full.names = TRUE)
# load raster
data_raster <- terra::rast(data_path)
# extract data to plot (note that we spatially sample data to reduce run time)
plot_data <-
data_raster %>%
terra::spatSample(size = 400000, method = "regular", as.raster = TRUE) %>%
terra::as.data.frame(xy = TRUE) %>%
dplyr::rename(value = 3) %>%
dplyr::mutate(title = "Habitat classification") %>%
dplyr::mutate(subtitle = basename(data_path)) %>%
dplyr::mutate(value = as.character(value))
# create an underlying map
p <-
ggplot() +
coord_fixed(expand = FALSE) +
geom_tile(
data = plot_data,
mapping = aes(x = x, y = y, fill = value)
) +
facet_wrap(~ title + subtitle) +
theme(
legend.pos = "hide",
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
axis.ticks.length = unit(0, "null"),
axis.ticks.margin = unit(0, "null"),
panel.border = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank(),
legend.text = element_text(size = 8),
legend.margin = unit(0, "null"),
plot.margin = unit(c(0.001, 0.001, 0.001, 0.01), "pt"),
strip.background = ggplot2::element_rect(color = "black", fill = "black"),
strip.text = ggplot2::element_text(color = "white")
)
```

```{r "map", echo = FALSE, message = FALSE, warning = FALSE, fig.height = 2.4, fig.width = 5.0, dpi = 250}
# render plot
print(p)
```

## Metadata

The habitat classification data are stored in a raster (GeoTIFF) file. The file name contains the version number of the original datasets (i.e., per the digital object identifier). The latest version of the file is ``r basename(data_path)``.

The metadata for the ``r basename(data_path)`` raster file is shown below.

```{r, echo = FALSE, comment = NA}
withr::with_dir("results", {
cat(
system(paste("gdalinfo", basename(data_path)), intern = TRUE),
sep = "\n"
)
})
```

## Usage

You can run the code to produce the dataset using the system command `make clean all`. After processing has successfully completed, the code will produce two raster (GeoTIFF) files in the `results` directory.

## Software requirements

Operating system

* Ubuntu (21.10) (300 Gb disk space available)

Software

* GNU make (version 4.1)
* GDAL (version 3.2.2)
* R (version 4.1.1)

System packages

* `gdal-bin`
* `libudunits2-dev`
* `libgdal-dev`
* `libgeos-dev`
* `libproj-dev`
* `libxml2-dev`
* `libssl-dev`

## Citation

This dataset was derived from Lumbierres *et al.* (2021). If you use this dataset, you should cite their work - they deserve all the credit!

> Lumbierres, M (2021). *Map of habitat classes (Level 1) from the IUCN Habitat.* Zenodo Digital Repository. Available at https://doi.org/10.5281/zenodo.5146072.
> Lumbierres M, Dahal PR, Di Marco M, Butchart SHM, Donald PF, and Rondinini C (2021) Translating habitat class to land cover to map area of habitat of terrestrial vertebrates. *Conservation Biology*, **In press**, DOI:10.1111/cobi.13851. Available at https://doi.org/10.1111/cobi.13851.
131 changes: 131 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@

<!--- README.md is generated from README.Rmd. Please edit that file -->

# Habitat classification data from Lumbierres *et al.* 2021

[![lifecycle](https://img.shields.io/badge/Lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![DOI](https://img.shields.io/badge/DOI-TODO-blue.svg)](https://doi.org/TODO)

This repository contains code to generate global habitat classification
data (based on Lumbierres *et al.* 2021). Specifically, source data are
downloaded from the [Zenodo Digital
Archive](https://doi.org/10.5281/zenodo.5146072), and then projected to
the World Behrmann coordinate reference system using (100 m resolution;
[ESRI:4326 coordinate reference system](https://epsg.io/54017)). A
processed version of the data can be downloaded from the [Zenodo Digital
Repository](https://zenodo.org/badge/latestdoi/TODO). **All credit
should go to Lumbierres *et al.* (2021), please cite their work if you
use this dataset.**

<img src="figures/README-map-1.png" style="display: block; margin: auto;" />

## Metadata

The habitat classification data are stored in a raster (GeoTIFF) file.
The file name contains the version number of the original datasets
(i.e., per the digital object identifier). The latest version of the
file is `lumbierres-10-5281_zenodo-5146073.tif`.

The metadata for the `lumbierres-10-5281_zenodo-5146073.tif` raster file
is shown below.

Driver: GTiff/GeoTIFF
Files: lumbierres-10-5281_zenodo-5146073.tif
Size is 347351, 132926
Coordinate System is:
PROJCRS["World_Behrmann",
BASEGEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]],
CONVERSION["Lambert Cylindrical Equal Area",
METHOD["Lambert Cylindrical Equal Area",
ID["EPSG",9835]],
PARAMETER["Latitude of 1st standard parallel",30,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8823]],
PARAMETER["Longitude of natural origin",0,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8802]],
PARAMETER["False easting",0,
LENGTHUNIT["metre",1],
ID["EPSG",8806]],
PARAMETER["False northing",0,
LENGTHUNIT["metre",1],
ID["EPSG",8807]]],
CS[Cartesian,2],
AXIS["easting",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["northing",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]]]
Data axis to CRS axis mapping: 1,2
Origin = (-17367531.000000000000000,7287077.000000000000000)
Pixel Size = (100.000000000000000,-100.000000000000000)
Metadata:
AREA_OR_POINT=Area
TIFFTAG_SOFTWARE=GRASS GIS 7.8.5 with GDAL 3.0.4
Image Structure Metadata:
COMPRESSION=DEFLATE
INTERLEAVE=BAND
Corner Coordinates:
Upper Left (-17367531.000, 7287077.000) (179d59'59.98"E, 83d 0'14.04"N)
Lower Left (-17367531.000,-6005523.000) (179d59'59.98"E, 55d 0' 0.03"S)
Upper Right (17367569.000, 7287077.000) (179d59'58.56"W, 83d 0'14.04"N)
Lower Right (17367569.000,-6005523.000) (179d59'58.56"W, 55d 0' 0.03"S)
Center ( 19.000, 640777.000) ( 0d 0' 0.71"E, 5d 1'44.75"N)
Band 1 Block=256x256 Type=Byte, ColorInterp=Gray
Description = habitat_CGLS
NoData Value=0
Metadata:
COLOR_TABLE_RULES_COUNT=1
COLOR_TABLE_RULE_RGB_0=0.000000e+00 1.200000e+02 255 255 255 150 123 0

## Usage

You can run the code to produce the dataset using the system command
`make clean all`. After processing has successfully completed, the code
will produce two raster (GeoTIFF) files in the `results` directory.

## Software requirements

Operating system

- Ubuntu (21.10) (300 Gb disk space available)

Software

- GNU make (version 4.1)
- GDAL (version 3.2.2)
- R (version 4.1.1)

System packages

- `gdal-bin`
- `libudunits2-dev`
- `libgdal-dev`
- `libgeos-dev`
- `libproj-dev`
- `libxml2-dev`
- `libssl-dev`

## Citation

This dataset was derived from Lumbierres *et al.* (2021). If you use
this dataset, you should cite their work - they deserve all the credit!

> Lumbierres, M (2021). *Map of habitat classes (Level 1) from the IUCN
> Habitat.* Zenodo Digital Repository. Available at
> <https://doi.org/10.5281/zenodo.5146072>.
> Lumbierres M, Dahal PR, Di Marco M, Butchart SHM, Donald PF, and
> Rondinini C (2021) Translating habitat class to land cover to map area
> of habitat of terrestrial vertebrates. *Conservation Biology*, **In
> press**, <DOI:10.1111/cobi.13851>. Available at
> <https://doi.org/10.1111/cobi.13851>.
90 changes: 90 additions & 0 deletions code/lumbierres-habitat-data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Initialization
## load packages
library(aoh)
library(raster)
library(terra)
library(sf)
library(rappdirs)
library(gdalUtilities)

## set variables
### set number of threads
n_threads <- max(1, parallel::detectCores() - 2)

### change this to where you want to save the inputs
input_dir <- "inputs"

### change this to where you want to save the outputs
output_dir <- "results"

### set version to process
### note that latest_zenodo_version() fails for Zenodo archives that only have
## one version, so here we manually specify the first version if it fails
version <- try(
aoh:::latest_zenodo_version(
x = "10.5281/zenodo.5146072",
file = "habitat_CGLS.tiff"
),
silent = TRUE
)
if (inherits(version, "try-error")) {
version <- "10.5281/zenodo.5146073"
}

# Preliminary processing
## print version
cli::cli_alert_info(paste0("Version: ", version))
cli::cli_alert_info(paste0("GDAL_CACHEMAX: ", Sys.getenv("GDAL_CACHEMAX")))

## download data
raw_path <- aoh:::get_zenodo_data(
x = version,
dir = input_dir,
force = FALSE,
file = "habitat_CGLS.tiff"
)

## construct output path
output_path <- gsub(
".", "-", gsub("/", "_", version, fixed = TRUE), fixed = TRUE
)
output_path <- file.path(tempdir(), paste0("lumbierres-", output_path, ".tif"))
output_path <- gsub("\\", "/", output_path, fixed = TRUE)

# Main processing
## import habitat data
raw_data <- terra::rast(raw_path)

## import elevation data
elev_data <- get_global_elevation_data(
dir = rappdirs::user_data_dir("aoh"),
version = "latest",
force = FALSE,
verbose = TRUE
)

## project habitat data to match elevation data
habitat_data <- aoh:::terra_gdal_project(
x = raw_data,
y = elev_data,
filename = output_path,
method = "near",
n_threads = n_threads,
datatype = "INT1U",
cache_limit = 5000,
tiled = TRUE,
bigtiff = TRUE,
compress = "DEFLATE",
NAflag = 0,
verbose = TRUE
)

## verification
habitat_data <- terra::rast(output_path)
assertthat::assert_that(
terra::compareGeom(habitat_data, elev_data, res = TRUE, stopiffalse = FALSE),
msg = "GDAL processing didn't work correctly"
)

# Finished
message("Done!")
1 change: 1 addition & 0 deletions data/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading

0 comments on commit 52e47de

Please sign in to comment.