-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_mdd_map.R
85 lines (72 loc) · 2.4 KB
/
get_mdd_map.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#' Title
#'
#' @param species A vector of mammal species name.
#' @param Order A vector of mammal orders to retrive.
#' @param country A string of country name in English.
#' @param continent A string of continent name in English.
#' @param cropCountry Logical. If TRUE, the shapefiles are crop with
#' country or continent shape
#'
#' @return A SpatVector object with mammal shapefiles
#' @export
#' @source Expert range maps of global mammal distributions harmonised to
#' three taxonomic authorities
#' <https://doi.org/10.1111/jbi.14330>
#'
#' @examples
#' get_mdd_map("Notoryctes caurinus")
get_mdd_map <- function(species = NULL, Order = NULL, country = NULL, continent = NULL, cropCountry = FALSE){
if(!(is.null(species) || is.null(Order))){
stop("Please provide only species or order, not both!")
}
if(!(is.null(country) || is.null(continent))){
stop("Please provide only country or continent, not both!")
}
allOrder <- unique(mddSpList$Order)
allSp <- unique(mddSpList$MDD_SciName)
matchOrder <- match(Order, allOrder)
matchSp <- match(species, allSp)
if( any(is.na( matchOrder) ) ){
stop("Order not found. Check spell")
}
if( any(is.na(matchSp) ) ){
stop("Species not found. Check spell")
}
#matchNames <- match(species, mddSpList$MDD_SciName)
if(is.null(Order)){
Order = unique(mddSpList$Order[matchSp] )
}
if(length(Order) == 27){
shp <- get_mdd()
} else {
MapList <- Map(get_mdd_order, Order)
shp <- terra::vect(MapList)
}
if(!is.null(species)){
shp <- terra::subset(shp, shp$sciname %in% species)
}
safe_intersect <- function(shp, country, otherwise = NULL){
force(otherwise)
tryCatch(terra::intersect(shp, country), error = function(e) {
message("Error: ", conditionMessage(e))
otherwise
})
}
if((!is.null(country) || !is.null(continent))){
country_shp <- rnaturalearth::ne_countries(country = country, continent = continent, returnclass = "sp")
country_shp <- terra::vect(country_shp)
country_shp <- country_shp[, "name"]
terra::crs(country_shp) <- terra::crs(shp)
country_shp <- terra::buffer(country_shp, 0)
#shp <- terra::buffer(shp, 0)
shp_country <- safe_intersect(shp, country_shp)
shp_country <- shp_country[ ,"sciname"]
if(cropCountry){
shp <- shp_country
} else
{
shp <- subset(shp, shp$sciname %in% shp_country$sciname, NSE=TRUE)
}
}
return(shp)
}