Skip to content

Commit

Permalink
Merge pull request #39 from gdauby/devel
Browse files Browse the repository at this point in the history
Devel to master (version 2.1)
  • Loading branch information
LimaRAF committed Jan 11, 2024
2 parents 193b7dd + f419e61 commit 5102b3d
Show file tree
Hide file tree
Showing 67 changed files with 2,206 additions and 1,197 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ inst/doc
*.Rproj
*.Rhistory
test_data.R
data-raw/
data-raw/
data/data-raw
7 changes: 5 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
export(AOH.estimation)
export(AOO.computing)
export(AOO.decline)
export(AOO.estimation)
export(EOO.comp)
export(EOO.computing)
export(EOO.sensitivity)
Expand All @@ -20,15 +19,16 @@ export(criterion_B)
export(criterion_C)
export(criterion_D)
export(dummy_dist)
export(frag_assess)
export(locations.comp)
export(map.res)
export(near.threatened)
export(over.valid.poly)
export(pop.decline)
export(pop.decline.fit)
export(pop.decline.test)
export(pop.fluctuation)
export(proj_crs)
export(severe_frag)
export(subpop.comp)
export(subpop.estimation)
export(subpop.radius)
Expand All @@ -50,6 +50,8 @@ importFrom(foreach,foreach)
importFrom(grDevices,chull)
importFrom(grDevices,rgb)
importFrom(graphics,axis)
importFrom(graphics,box)
importFrom(graphics,layout)
importFrom(graphics,legend)
importFrom(graphics,mtext)
importFrom(graphics,par)
Expand All @@ -67,6 +69,7 @@ importFrom(sf,st_as_sf)
importFrom(sf,st_coordinates)
importFrom(sf,st_distance)
importFrom(sf,st_geometry)
importFrom(sf,st_intersects)
importFrom(sf,st_linestring)
importFrom(sf,st_multilinestring)
importFrom(sf,st_sfc)
Expand Down
Empty file removed R/.Rhistory
Empty file.
29 changes: 14 additions & 15 deletions R/AOO.computing.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

#' @title Area of occupancy
#'
#' @description Compute areas of occupancy (AOO) for multiple taxa in square kilometers
#' @description Compute areas of occupancy (AOO) for multiple taxa in square
#' kilometers
#'
#' @author Gilles Dauby \email{gilles.dauby@@ird.fr}
#'
Expand All @@ -11,14 +12,12 @@
#' @param nbe.rep.rast.AOO numeric, by default is 0. Indicate the number of
#' raster with random starting position used for estimating the AOO. If 0 but
#' some translation of the raster are still done.
#' @param parallel logical, by default is FALSE. Whether running in parallel.
#' @param NbeCores integer, by default is 2. Register the number of cores for
#' parallel execution. Only used if parallel is TRUE.
#' @param show_progress logical, by default is TRUE. Whether a progress bar
#' during computation is shown.
#' @inheritParams activate_parallel
#' @param show_progress logical. Whether progress informations should displayed.
#' TRUE by default
#' @param export_shp logical, by default is FALSE. Whether a shapefile of
#' occupied cells should be exported.
#' @param proj_type character or numeric, by default is "cea", see Details.
#' @inheritParams proj_crs
#'
#' @details
#' # Input data
Expand Down Expand Up @@ -48,7 +47,7 @@
#' See `proj_type`
#'
#'
#' @references Gaston & Fuller 2009 The sizes of species'geographic ranges,
#' @references Gaston & Fuller 2009 The sizes of species' geographic ranges,
#' Journal of Applied Ecology, 49 1-9
#'
#' @return
Expand All @@ -66,9 +65,9 @@
#'
#'
#'# This would estimate AOO for all taxa by overlaying randomly a
#'# grid 10 times. For each taxa, the minimum value is kept
#'# grid 3 times. For each taxa, the minimum value is kept
#'
#' AOO <- AOO.computing(dataset.ex, nbe.rep.rast.AO = 10)
#' AOO <- AOO.computing(dataset.ex, nbe.rep.rast.AO = 3)
#'
#'
#' @importFrom utils txtProgressBar setTxtProgressBar
Expand Down Expand Up @@ -153,7 +152,7 @@ AOO.computing <- function(XY,

if(parallel) parallel::stopCluster(cl)
if(show_progress) close(pb)

res <- unlist(output[names(output) != "spatial"])

res_df[res_df$tax %in% names(res), 2] <-
Expand All @@ -164,11 +163,11 @@ AOO.computing <- function(XY,
"AOO could not computed because grid cells would overlap with antimeridian"

if (export_shp) {

shapes <- output[names(output) == "spatial"]
shapes <- do.call('rbind', shapes)
row.names(shapes) <- 1:nrow(shapes)

}

} else {
Expand All @@ -189,8 +188,8 @@ AOO.computing <- function(XY,


#' @importFrom foreach %dopar% %do% foreach
#' @keywords intenal
#' @export
#' @keywords internal
#'
AOO.estimation <- function(coordEAC,
cell_size = 2,
nbe_rep = 0,
Expand Down
2 changes: 1 addition & 1 deletion R/EOO.comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' @param proj_type crs
#' @param reproject logical FALSE whether the polygon should be converted to geographic coordinates if mode is `planar`
#'
#' @author Gilles Dauby & Renato A. Ferreira de LimaA
#' @author Gilles Dauby & Renato A. Ferreira de Lima
#'
#' @return A list
#' \enumerate{
Expand Down
29 changes: 17 additions & 12 deletions R/EOO.computing.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#' If `exclude.area` is TRUE and country_map is not provided,
#' the world country polygons used comes from the package [rnaturalearth](https://www.rdocumentation.org/packages/rnaturalearth/versions/0.1.0/topics/ne_countries)
#'
#' By default (`mode = "spheroid"`),the area of the polygon is based
#' on a polygon in longitude/latitude coordinates considering the earth as an ellipsoid.
#' By default (`mode = "spheroid"`), the area of the polygon is based on a
#' polygon in longitude/latitude coordinates considering the earth as an
#' ellipsoid.
#'
#' To make a polygon more accurate, the function use the function `st_segmentize`
#' from the [sf](https://CRAN.R-project.org/package=sf) package.
#' This adds vertices on the great circles (in order to make shortest distances between points, see example below)
#' which can make difference for species with large distribution.
#' This adds vertices on the great circles (in order to make shortest distances
#' between points, see example below) which can make difference for species with
#' large distribution.
#'
#' An estimation of EOO based on projected data is also possible (`mode = "planar"`).
#' This allow the user to use its own projection.
Expand All @@ -52,8 +54,9 @@
#'
#' **Limitation**\cr
#'
#' For a species whose occurrences span more than 180 degrees, EOO should not be considered.
#' This is the case for example for species whose distribution span the 180th meridian.
#' For a species whose occurrences span more than 180 degrees, EOO should not be
#' considered. This is the case for example for species whose distribution span
#' the 180th meridian.
#'
#' @param XY `dataframe` see Details
#' @param exclude.area a logical, if TRUE, areas outside of `country_map`
Expand All @@ -79,12 +82,14 @@
#' @param method.less.than3 a character string. If equal to "arbitrary", will
#' give a value to species with two unique occurrences, see Details. By default
#' is "not comp"
#' @param file.name a character string. Name file for exported results in csv file. By default is "EOO.results"
#' @param parallel a logical. Whether running in parallel. By default, it is FALSE
#' @param NbeCores an integer. Register the number of cores for parallel execution. By default, it is 2
#' @param show_progress logical. Whether a progress bar should displayed. TRUE by default
#' @param mode character string either 'spheroid' or 'planar'. By default 'spheroid'
#' @param file.name a character string. Name file for exported results in csv
#' file. By default is "EOO.results"
#' @param show_progress logical. Whether progress informations should displayed.
#' TRUE by default
#' @param mode character string either 'spheroid' or 'planar'. By default
#' 'spheroid'
#' @inheritParams proj_crs
#' @inheritParams activate_parallel
#'
#' @return
#' If `export_shp` is FALSE, a `dataframe` with one field
Expand All @@ -103,7 +108,7 @@
#'
#' <https://github.com/azizka/speciesgeocodeR>
#'
#' @references Gaston & Fuller 2009 The sizes of species'geographic ranges,
#' @references Gaston & Fuller 2009 The sizes of species' geographic ranges,
#' Journal of Applied Ecology, 49 1-9
#'
#' @examples
Expand Down
2 changes: 1 addition & 1 deletion R/EOO.sensitivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#'
#' **Notes on computational time** The processing time depends on several
#' factors, including the total number of occurrences, number of confidence
#' levels rpovided and user's computer specifications. Using argument `parallel`
#' levels provided and user's computer specifications. Using argument `parallel`
#' equals `TRUE`, greatly increase the processing time, but the processing of
#' large data sets (millions of occurrences) may take hours. On a Intel Core i5,
#' CPU 1.70GHz, 64-bit OS and 16 GB RAM it took 20 min to process about 800
Expand Down
2 changes: 1 addition & 1 deletion R/ICtab.mod.select.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#' For the process of selecting the best model, we followed some basic steps.
#' By default, if two or more models had delta-AIC smaller than the cutoff
#' provided, the more parsimonious model (i.e. the model with less parameters)
#' is selected. However, this decision can be changed (i.e. slect the model
#' is selected. However, this decision can be changed (i.e. select the model
#' with best fit) by setting the argument 'parsimony' to FALSE. Next, if more
#' than one model is selected (i.e. both have the same number of parameters),
#' the selection process give preference to models that are not linear or
Expand Down
9 changes: 6 additions & 3 deletions R/Locations.estimation.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#' @param nbe_rep integer
#' @param export_shp logical
#' @param proj_type character string
#' @param Rel_cell_size numeric, if `method ="sliding_scale"`, `Cell_size_locations` is ignored and the resolution is given by the maximum distance separating two occurrences multiplied by `Rel_cell_size`. By default, it is 0.05
#' @param rel_cell_size numeric, if `method ="sliding_scale"`,
#' `Cell_size_locations` is ignored and the resolution is given by the maximum
#' distance separating two occurrences multiplied by `rel_cell_size`. By
#' default, it is 0.05
#' @param method character string
#'
#' @return a list
Expand All @@ -21,7 +24,7 @@ locations.estimation <- function(coordEAC,
export_shp = FALSE,
proj_type = proj_type,
method = "fixed_grid",
Rel_cell_size = 0.05
rel_cell_size = 0.05

) {

Expand All @@ -33,7 +36,7 @@ locations.estimation <- function(coordEAC,

pairwise_dist <- stats::dist(coordEAC[, 1:2], upper = F)

cell_size <- max(pairwise_dist) * Rel_cell_size / 1000
cell_size <- max(pairwise_dist) * rel_cell_size / 1000

} else{

Expand Down
12 changes: 8 additions & 4 deletions R/activate_parallel.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#' @title Internal function
#'
#' @description Activate paralle processing
#' @description Activate parallel processing
#'
#' @author Gilles Dauby, \email{gildauby@gmail.com}
#'
#' @param XY data.frame
#' @param mode character string either 'spheroid' or 'planar'. By default 'spheroid'
#' @param proj_type crs
#' @param parallel a logical. Whether running should be performed in parallel.
#' FALSE by default.
#' @param NbeCores an integer. Register the number of cores for parallel
#' execution. Two by default.
#'
#' @importFrom doSNOW registerDoSNOW
#' @importFrom parallel makePSOCKcluster
#' @importFrom foreach %dopar% %do% foreach
#'
#' @keywords internal
#'
#' @export
#'
activate_parallel <- function(parallel = FALSE, NbeCores = 2) {
if (parallel) {
cl <- parallel::makePSOCKcluster(NbeCores)
Expand Down
6 changes: 4 additions & 2 deletions R/ahull_to_SPLDF.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#' @param x ahull class object
#'
#' @details
#' The functions ahull_to_SPLDF and alpha.hull.poly were originally posted in the website https://casoilresource.lawr.ucdavis.edu/software/r-advanced-statistical-package/working-spatial-data/converting-alpha-shapes-sp-objects/
#' in a now broken link. It is also used in functions written by David Bucklin, see https://github.com/dnbucklin/r_movement_homerange
#' The functions ahull_to_SPLDF and alpha.hull.poly were originally posted in
#' the website https://casoilresource.lawr.ucdavis.edu/software/r-advanced-statistical-package/working-spatial-data/converting-alpha-shapes-sp-objects/
#' in a now broken link. It is also used in functions written by David Bucklin,
#' see https://github.com/dnbucklin/r_movement_homerange
#'
#' @importFrom sf st_linestring st_multilinestring st_as_sf st_sfc
#'
Expand Down
8 changes: 4 additions & 4 deletions R/cat_downlist.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#' @title Downlist Threat Categories
#'
#' @description Perform the downlisting of threat categories which is often
#' necessary for regional conservation assessmentes (IUCN 2012).
#' necessary for regional conservation assessments (IUCN 2012).
#'
#' @param cats character. The vector containing the IUCN threat categories.
#' @param down.by numerical. The number of steps to downlist the categories.
#' Default to 1.
#' @param dd logical. Should the Data Deficient (DD) category be included in the
#' downlisting? Defaut to FALSE.
#' downlisting? Default to FALSE.
#' @param sign logical. Should the degrees sign be indicated in the downlisted
#' category. Default to TRUE.
#'
Expand All @@ -17,12 +17,12 @@
#' thus to the possibility of a possible rescue effect (IUCN 2012).
#'
#' Although the IUCN (2012) considers the exceptional possibility of
#' uplisting, the function only performs downliting of the categories of
#' uplisting, the function only performs downlisting of the categories of
#' threat. Downlisting normally is a one-step change in the category (i.e.
#' from EN to VU), but this can be controlled by the argument ```down.by```
#' (default to 1).
#'
#' By default, the Data Deficient category is excluded from the donwlisting.
#' By default, the Data Deficient category is excluded from the downlisting.
#' The Least Concern category remains unaltered as well.
#'
#' Note that "if it is unknown whether or not extra-regional populations
Expand Down
25 changes: 20 additions & 5 deletions R/cat_mult_criteria.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' category ('main.criteria'). It also returns the auxiliary category provided
#' by other criteria ('aux.criteria'), separated by a ';'
#'
#' @details The definiton of the main category of threat, follows the
#' @details The definition of the main category of threat, follows the
#' recommendations of IUCN (2019) that states "Only the criteria for the
#' highest category of threat that the taxon qualifies for should be listed".
#' Therefore, the consensus category is the highest category of threat among
Expand Down Expand Up @@ -40,10 +40,21 @@
#' @export cat_mult_criteria
cat_mult_criteria <- function(assess.df = NULL, evidence.df = NULL){

assess.df$tmp.order.plantr <- 1:dim(assess.df)[1]
tmp <- assess.df
tax <- names(tmp)[1]
n.crits <- dim(tmp)[2]
assess.df$order <- 1:dim(tmp)[1]

possible_crits <- c(LETTERS[1:5], paste0(LETTERS[1], 1:4),
paste0(rep(LETTERS[2:4], each = 2), 1:2),
paste0("category_", LETTERS[1:5]), "D2.Loc", "D.AOO")
if (!any(names(tmp) %in% possible_crits))
stop("Please provide a data frame with at least one of the following column names: ",
paste0(possible_crits, collapse = ", "))
col.name.df <- names(tmp)[names(tmp) %in% possible_crits]
tmp0 <- tmp[, names(tmp) %in% possible_crits]
n.crits <- dim(tmp0)[2]

tmp <- cbind.data.frame(tax = tmp[[1]], tmp0)

## Establishing an hierarchy of data availability
hier <- c("Observed","Estimated","Projected","Inferred","Suspected")
Expand Down Expand Up @@ -92,16 +103,20 @@ cat_mult_criteria <- function(assess.df = NULL, evidence.df = NULL){
tmp$aux.criteria <- gsub(": \\+", ": ", tmp$aux.criteria, perl = TRUE)
tmp$aux.criteria <- gsub("\\+$", "", tmp$aux.criteria, perl = TRUE)
tmp$aux.criteria <- gsub("\\+;", ";", tmp$aux.criteria, perl = TRUE)
tmp$aux.criteria <- gsub("category_B", "B1+B2", tmp$aux.criteria, perl = TRUE)
tmp$aux.criteria <- gsub("category_C", "C1+C2", tmp$aux.criteria, perl = TRUE)

tmp$main.criteria[is.infinite(as.double(tmp$category))] <- ""
tmp$category[is.infinite(as.double(tmp$category))] <- ""
tmp$category <- stringr::str_replace_all(tmp$category, rpl.cds)
tmp$category <- gsub("category_B", "B1+B2", tmp$category, perl = TRUE)
tmp$category <- gsub("category_C", "C1+C2", tmp$category, perl = TRUE)

#Merging with the entry data.frame
res <- merge(assess.df, tmp[,c(tax,"category","main.criteria","aux.criteria")],
by = tax)
res <- res[order(res$order),]
res <- res[,-which(names(res) %in% "order")]
res <- res[order(res$tmp.order.plantr),]
res <- res[,-which(names(res) %in% "tmp.order.plantr")]

return(res)
}
2 changes: 1 addition & 1 deletion R/cell.occupied.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ cell.occupied <-

r2_pol <-
terra::as.polygons(
r2_)
r2_, aggregate = FALSE)

r2_pol_sf <- sf::st_as_sf(r2_pol)

Expand Down
1 change: 1 addition & 0 deletions R/coord.check.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param listing logical, whether the dataset should be splitted in a list by taxa
#' @param proj_type crs
#' @param listing_by_valid logical
#' @param cell_size numeric value
#' @param check_eoo logical
#'
#'
Expand Down
Loading

0 comments on commit 5102b3d

Please sign in to comment.