Skip to content

Commit

Permalink
Merge pull request #441 from bjohnso005/dev_biomodelos
Browse files Browse the repository at this point in the history
unit tests & documentation
  • Loading branch information
danflop authored Oct 23, 2024
2 parents ba74553 + 5527f0d commit 9bf6eaf
Show file tree
Hide file tree
Showing 79 changed files with 2,316 additions and 214 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: wallace
Version: 2024.08.02
Date: 2024-08-02
Version: 2024.09.18
Date: 2024-09-18
Title: A Modular Platform for Reproducible Modeling of Species Niches
and Distributions
Description: The 'shiny' application Wallace is a modular platform for
Expand Down
76 changes: 69 additions & 7 deletions R/indic_aoo.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
##' @title indic_aoo
# Wallace EcoMod: a flexible platform for reproducible modeling of
# species niches and distributions.
#
# indic_aoo.R
# File author: Wallace EcoMod Dev Team. 2023.
# --------------------------------------------------------------------------
# This file is part of the Wallace EcoMod application
# (hereafter “Wallace”).
#
# Wallace is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# Wallace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Wallace. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------
#
#' @title indic_aoo
#' @description Calculate AOO.
#'
#' @details
#' The function calculates the area of occupancy (AOO) from a thresholded
#' prediction raster or a data.frame of occurrence records.
#'
#' @param r raster. Thresholded prediction raster. It could be NULL if occs provided.
#' @param occs data.frame. Table with occurrences. It could be NULL if raster
#' provided. If occs provided raster is ignored.
Expand All @@ -13,24 +40,57 @@
#' @param logger stores all notification messages to be displayed in the Log
#' Window of Wallace GUI. Insert the logger reactive list here for running in
#' shiny, otherwise leave the default NULL
#'
#' @examples
#' \dontrun{
#' ### Set parameters
#' # binary raster
#' r <- terra::rast(system.file("extdata/Bassaricyon_neblina.tif",
#' package = "wallace"))
#' # occurrences
#'
#' ### Run function
#' # aoo from raster
#' aoo_r <- indic_aoo(r,
#' occs = NULL,
#' lon = NULL,
#' lat = NULL,
#' wktFrom = getWKT("wgs84"),
#' wktTo = getWKT("wcea"),
#' logger = NULL)
#' # aoo from occs
#' aoo_occs <- indic_aoo(r <- NULL,
#' occs,
#' lon = "longitude",
#' lat = "latitude",
#' wktFrom = getWKT("wgs84"),
#' wktTo = getWKT("wcea"),
#' logger = NULL)
#' }
#'
#' @return A list of two elements: area and AOOraster. The first is the numeric
#' value of the area (in km^2). The second is a RasterLayer of the extent
#' (in WCEA & 2x2 km resolution).
#'
#' @author Gonzalo E. Pinilla-Buitrago <gpinillabuitrago@@gradcenter.cuny.edu>
#' @author Bethany A. Johnson <bjohnso005@@citymail.cuny.edu>
#' @export
#'

indic_aoo <- function(r = NULL, occs = NULL, lon = NULL, lat = NULL,
wktFrom, wktTo, logger) {
wktFrom, wktTo, logger = NULL) {
if (!is.null(occs)) {
if (is.null(lon) | is.null(lat)) {
logger %>% writeLog("No longitude and/or latitude name provided (**).")
logger %>% writeLog(type = 'error', "No longitude and/or latitude name provided.")
return()
} else {
p.pts <- occs
if (sum(c(lon, lat) %in% names(p.pts)) != 2) {
logger %>% writeLog("Longitude and/or latitude names not found (**).")
logger %>% writeLog(type = 'error', "Longitude and/or latitude names not found.")
return()
} else {
p.pts <- p.pts %>%
dplyr::select(c(lon, lat)) %>%
dplyr::select(all_of(c(lon, lat))) %>% #BAJ
terra::vect(geom = c(lon, lat))
terra::crs(p.pts) <- wktFrom
rast_temp <- terra::rast(terra::ext(terra::project(p.pts, wktTo)) + 10000,
Expand All @@ -44,7 +104,9 @@ indic_aoo <- function(r = NULL, occs = NULL, lon = NULL, lat = NULL,
}
}
} else if (!is.null(r)) {
if ("RasterLayer" %in% class(r)) {
r <- terra::rast(r)
}
## Unsuitable for NAs
r[r == 0] <- NA
p.poly <- terra::as.polygons(r)
Expand All @@ -56,8 +118,8 @@ indic_aoo <- function(r = NULL, occs = NULL, lon = NULL, lat = NULL,
terra::trim()
AOOarea <- terra::freq(AOOraster, value = 1)$count * 4
} else {
logger %>% writeLog("Provide occurrences or raster (**).")
logger %>% writeLog(type = 'error', "Provide occurrences or raster.")
return()
}
return(list(area = AOOarea, AOOraster = methods::as(AOOraster, "Raster")))
return(list(area = AOOarea, AOOraster = raster::raster(AOOraster)))
}
35 changes: 35 additions & 0 deletions R/indic_area.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
# Wallace EcoMod: a flexible platform for reproducible modeling of
# species niches and distributions.
#
# indic_area.R
# File author: Wallace EcoMod Dev Team. 2023.
# --------------------------------------------------------------------------
# This file is part of the Wallace EcoMod application
# (hereafter “Wallace”).
#
# Wallace is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# Wallace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Wallace. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------
#
#' @title indic_area
#' @description Calculate Range size in square kilometers.
#' @details Calculate the geographic range size of a thresholded raster. The units are in square kilometers.
#'
#' @param r raster. Thresholded prediction raster. It could be NULL if occs provided.
#' @param wkt character. Well-known text representation of coordinate reference systems
#' to calculate area.
#' @param logger stores all notification messages to be displayed in the Log
#' Window of Wallace GUI. insert the logger reactive list here for running in
#' shiny, otherwise leave the default NULL
#'
#' @examples
#' \dontrun{
#' r <- terra::rast(system.file("extdata/Bassaricyon_neblina.tif",package = "wallace"))
#' wkt <- getWKT("wcea")
#' areaRange <- indic_area(r, wkt, logger = NULL)
#' }
#' @return Numeric value
#' @author Gonzalo E. Pinilla-Buitrago <gpinillabuitrago@@gradcenter.cuny.edu>
#' @author Bethany A. Johnson <bjohnso005@@citymail.cuny.edu>
#' @export
#'

indic_area <- function(r, wkt, logger = NULL) {
if ("RasterLayer" %in% class(r)) {
r <- terra::rast(r)
}
## Unsuitable for NAs
r[r == 0] <- NA
## Raster to polygon
Expand Down
55 changes: 50 additions & 5 deletions R/indic_eoo.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# Wallace EcoMod: a flexible platform for reproducible modeling of
# species niches and distributions.
#
# indic_eoo.R
# File author: Wallace EcoMod Dev Team. 2023.
# --------------------------------------------------------------------------
# This file is part of the Wallace EcoMod application
# (hereafter “Wallace”).
#
# Wallace is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# Wallace is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Wallace. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------
#
#' @title indic_eoo
#' @description Calculate EOO.
#' @details Calculate extent of occurrence (EOO) based on a binary raster or occurrence points.
#'
#' @param r raster. Thresholded prediction raster. It could be NULL if occs provided.
#' @param occs data.frame. Table with occurrences. It could be NULL if raster
Expand All @@ -11,34 +35,55 @@
#' @param logger stores all notification messages to be displayed in the Log
#' Window of Wallace GUI. Insert the logger reactive list here for running in
#' shiny, otherwise leave the default NULL
#'
#' @examples
#' \dontrun{
#' # binary raster
#' r <- terra::rast(system.file("extdata/Bassaricyon_neblina.tif",package = "wallace"))
#' # occurrences
#' occs <- read.csv(system.file("extdata/Bassaricyon_neblina.csv",package = "wallace"))
#' # wcea
#' wkt <- getWKT("wcea")
#'
#' eoo_r <- indic_eoo(r, occs = NULL, lon = NULL, lat = NULL, wkt, logger = NULL)
#' eoo_o <- indic_eoo(r = NULL, occs, lon = "longitude", lat = "latitude", wkt, logger = NULL)
#' }
#'
#' @return A list of two elements: area and eooPoly. area is a numeric value and eooPoly is a SpatialPolygon.
#' @author Andrea Paz <paz.andreita@@gmail.com>
#' @author Gonzalo E. Pinilla-Buitrago <gpinillabuitrago@@gradcenter.cuny.edu>
#' @author Bethany A. Johnson <bjohnso005@@citymail.cuny.edu>
#' @export
#'

indic_eoo <- function(r = NULL, occs = NULL, lon = NULL, lat = NULL,
wkt, logger) {
wkt, logger=NULL) {
if (!is.null(occs)) {
if (is.null(lon) | is.null(lat)) {
logger %>% writeLog("No longitude and/or latitude name provided (**).")
logger %>% writeLog(type = 'error',
"No longitude and/or latitude name provided.")
return()
} else {
p.pts <- occs
if (sum(c(lon, lat) %in% names(p.pts)) != 2) {
logger %>% writeLog("Longitude and/or latitude names not found (**).")
logger %>% writeLog(type = 'error',
"Longitude and/or latitude names not found.")
return()
} else {
p.pts <- p.pts %>% dplyr::select(c(lon, lat))
p.pts <- p.pts %>% dplyr::select(all_of(c(lon, lat)))
}
}
} else if (!is.null(r)) {
if ("RasterLayer" %in% class(r)) {
r <- terra::rast(r)
}
r[r == 0] <- NA
p.pts <- terra::as.points(r) %>%
terra::geom() %>% data.frame() %>%
dplyr::select(tidyselect::all_of(c("x", "y")))
} else {
logger %>% writeLog("Provide occurrences or raster (**).")
logger %>% writeLog(type = 'error',
"Provide occurrences or raster.")
return()
}
wgs84 <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
Expand Down
Loading

0 comments on commit 9bf6eaf

Please sign in to comment.