Skip to content

Commit

Permalink
Updating functions
Browse files Browse the repository at this point in the history
  • Loading branch information
8Ginette8 committed Jun 26, 2024
1 parent ae8135f commit fbd1cf6
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 83 deletions.
49 changes: 23 additions & 26 deletions R/get_range.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
#' Create a species range map based on a get_gbif() output
#'
#' Estimates species ranges based on occurrence data (GBIF or not) and ecoregions
#' (external, but we provide some ecoregions, see function 'make_ecoregion').
#' (related functions 'make_ecoregion').
#' It first deletes outliers from the observation dataset and then creates a polygon
#' (convex hull) with a user
#' specified buffer around all the observations of one ecoregion. If there is only
#' one observation in an ecoregion, a buffer around this point will be created. If
#' (convex hull) with a user specified buffer around all the observations of one ecoregion.
#' If there is only one observation in an ecoregion, a buffer around this point will be created. If
#' all points in an ecoregion are on a line, the function will also create a buffer
#' around these points, however, the buffer size increases with the number of points
#' in the line. Finally, also note that in case of too many records, get_range can be
Expand All @@ -21,16 +20,15 @@
#' @param bioreg 'SpatialPolygonsDataFrame', 'SpatVector' or 'sf' object containg different
#' ecoregions (convex hulls will be classified on a bioreg basis) and of CRS WGS84. Note
#' that this parameter may be fed with an external, generated (function make_ecoregion) or
#' in-house ecoregion shapefile. Three in-house shapefiles are already included in the library:
#' 'eco.earh' (for terrestrial species; Nature conservancy version adapted from Olson & al. 2001),
#' 'eco.marine' (for marine species; Spalding & al. 2007, 2012) and 'eco.fresh' (for freshwater
#' species; Abell & al. 2008). For marine species, eco.earth may also be used if the user wants
#' to represent the terrestrial range of species that also partially settle on mainland. For
#' fresh water species, same may be done if the user considers that terrestrial ecoregions
#' should be more representtaive of the species ecology.
#' in-house ecoregion shapefile. Four shapefiles can be downloaded with the library (function get_bioreg
#' and others): eco_terra' (for terrestrial species; Nature conservancy version adapted from Olson & al. 2001),
#' 'eco_marine' and 'eco_hd_marine' (for marine species; Spalding & al. 2007, 2012) and 'eco_fresh' (for freshwater
#' species; Abell & al. 2008). For marine species, 'eco_terra' may also be used if the user wants to represent
#' the terrestrial range of species that also partially settle on mainland. For fresh water species, same may be
#' done if the user considers that terrestrial ecoregions should be more representative of the species ecology.
#' @param bioreg_name Character. How is the shapefile attribute containing the ecoregion names called?
#' Default is the very detailed level of 'eco.earth' (aka 'ECO_NAME'). Note that 'EcoRegion'
#' must always be used when using a make_ecoregion() output. See details.
#' E.g., very detailed level of 'eco_terra' is 'ECO_NAME'. Note that 'EcoRegion' (default) must always be supplied when
#' using a make_ecoregion() output. See details.
#' @param degrees_outlier Numeric. Distance threshold (degrees) for outlier classification.
#' If the nearest minimal distance to the next point is larger than this threshold, it will be
#' considered as an outlier.
Expand Down Expand Up @@ -58,17 +56,10 @@
#' Each ecoregion shapefile has one or more categories, which describe more or less precisely the
#' ecoregion world distribution (from the more to the less detailed):
#'
#' - 'eco.earth' has three different levels: 'ECO_NAME', 'WWF_MHTNAM' and 'WWF_REALM2'.
#'
#' - 'eco.fresh' has only one: 'FEOW_ID'.
#'
#' - 'eco.marine' contains a mix of two types of marine ecoregions. Either common ('PROVINC' and 'REALM')
#' or distinct levels:
#'
#' ---> For PPOW (Pelagic provinces of the world): 'BIOME'.
#'
#' ---> For MEOW (Marine ecoregions of the world): 'ECOREGION'.
#'
#' - eco_terra has three different levels: 'ECO_NAME', 'WWF_MHTNAM' and 'WWF_REALM2'.
#' - eco_fresh has only one: 'ECOREGION'.
#' - eco_marine and eco_hd_marine (very coastal-precise version) contains three distinct levels:
#' 'ECOREGION', 'PROVINCE' and 'REALM'.
#'
#' @return A 'SpatVector' or 'SpatRaster'.
#' @references
Expand Down Expand Up @@ -131,8 +122,8 @@
#' @importFrom ClusterR KMeans_rcpp
get_range <- function (sp_name = NULL,
occ_coord = NULL,
bioreg = eco.earth,
bioreg_name = "ECO_NAME",
bioreg = NULL,
bioreg_name = "EcoRegion",
degrees_outlier = 3,
clustered_points_outlier = 2,
buffer_width_point = 4,
Expand All @@ -146,6 +137,12 @@ get_range <- function (sp_name = NULL,
### Object conditions + remove duplicates
### =========================================================================

# bioreg
if (methods::is(bioreg_name,"NULL")) {
stop("An ecoregion polygon should be supplied; if unavailable see command 'bioreg_list' and
internal functions get_bioreg(), read_bioreg() and check_and_get_bioreg()")
}

# occ_coord
if (!methods::is(occ_coord,"data.frame")) {
stop("'occ_coord' is not a data.frame...")
Expand Down
1 change: 1 addition & 0 deletions inst/examples/get_doi_help.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Download worldwide the observations of Panthera tigris and Ailuropoda melanoleuca
obspt <- get_gbif("Panthera tigris")
obsam <- get_gbif("Ailuropoda melanoleuca")

\dontrun{
# Retrieve DOI for only one get_gbif() output
get_doi(obspt,title="GBIF_test1",description="A small example 1",
Expand Down
4 changes: 1 addition & 3 deletions inst/examples/get_gbif_help.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Downloading worldwide the observations of Panthera tigris
obs.pt <- get_gbif("Panthera tigris",
basis=c("OBSERVATION","HUMAN_OBSERVATION","MACHINE_OBSERVATION"))
obs.pt <- get_gbif("Panthera tigris",basis=c("OBSERVATION","HUMAN_OBSERVATION","MACHINE_OBSERVATION"))
countries <- terra::vect(rnaturalearth::ne_countries(type = "countries",returnclass = "sf"))
terra::plot(countries,col="#bcbddc")
points(obs.pt[,c("decimalLongitude","decimalLatitude")],pch=20,col="#238b4550",cex=4)

\dontrun{

# Downloading worldwide the observations of Ailuropoda melanoleuca (with a 100km grain, after 1990
# and by keeping duplicates and by adding the name of the person who collected the panda records)
obs.am <- get_gbif("Ailuropoda melanoleuca", grain = 100000 , duplicates = TRUE,
Expand Down
20 changes: 9 additions & 11 deletions inst/examples/get_range_help.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
\dontrun{
# Load available ecoregions
data(ecoregions)
check_and_get_bioreg(bioreg_name = "eco_terra", save_dir = NULL)
eco_terra = read_bioreg(bioreg_name = "eco_terra", save_dir = NULL)

# First download the worldwide observations of Panthera tigris and convert to SpatialPoints
# get occurance points from GBIF
# First download the worldwide observations of Panthera tigris from GBIF
occ <- get_gbif("Panthera tigris",
basis=c("OBSERVATION","HUMAN_OBSERVATION","MACHINE_OBSERVATION"))
# make range from occurance points
range <- get_range("Panthera tigris", occ ,eco.earth ,"ECO_NAME") #TODO change eco.earth to eco.land
## TODO download=TRUE by default, check presence of file.

# Make range from occurance points
range <- get_range("Panthera tigris", occ ,eco_terra ,"ECO_NAME") #TODO change eco.earth to eco.land

# Plot
terra::plot(range, axes = FALSE, box = FALSE, legend=FALSE, col="chartreuse4")
# plot political world boundaries

# Plot political world boundaries
terra::plot(rnaturalearth::ne_countries(returnclass = "sf")[1], add=T, col=NA)
graphics::points(occ[,c("decimalLongitude","decimalLatitude")],pch=20,col="#99340470",cex=1.5)

# plot(countries,col="#bcbddc")
# plot(ne_countries(returnclass = "sf")[4] , col="antiquewhite")
# TODO FIX THIS
# points(occ[,c("decimalLongitude","decimalLatitude")],pch=20,col="#99340470",cex=1.5)
}
2 changes: 0 additions & 2 deletions inst/examples/obs_filter_help.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ graphics::points(obs.saxi[,c("decimalLongitude","decimalLatitude")],pch=20,col="
# rbind both datasets
both.sp <- rbind(obs.arcto,obs.saxi)


# # TODO FIX THIS
# Run function
obs.filt <- obs_filter(both.sp,rst)

Expand Down
1 change: 1 addition & 0 deletions man/get_doi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions man/get_gbif.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 26 additions & 36 deletions man/get_range.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/obs_filter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fbd1cf6

Please sign in to comment.