-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecoregion_background_sampler.Rmd
99 lines (75 loc) · 2.3 KB
/
ecoregion_background_sampler.Rmd
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
86
87
88
89
90
91
92
93
94
95
96
97
---
title: "Background Points"
author: "Fiona Spooner"
date: "30/10/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
```{r}
library(dplyr)
library(ggplot2)
library(here)
library(lwgeom)
library(readr)
library(sf)
```
```{r}
ecoreg <-
st_read(here::here("WWF_Ecoregions/official/wwf_terr_ecos.shp"))
ecoreg <- ecoreg %>%
select(OBJECTID, ECO_NAME) ##just selecting out the columns we're interested in
occ_files <-
list.files(
here::here("GBIF/Occurences/Network_Groups/"),
pattern = "*.csv",
full.names = TRUE,
recursive = TRUE
)
head(read.csv(occ_files[1]))
```
The function is in two stages, the first stage pulls out the unique longitude and latitude species occurrence points and identifies which ecoregions they are in.
The second stage creates a set number (no_pnts) of random points in these ecoregions which can be used as background points.
```{r}
background_sampler <- function(occ_file, no_pnts) {
sf_int <- read_csv(occ_file) %>%
dplyr::select("decimalLongitude", "decimalLatitude") %>%
distinct() %>%
st_as_sf(.,
coords = c("decimalLongitude", "decimalLatitude"),
crs = 4326) %>%
st_intersection(., ecoreg)
bkg_ecoreg <- ecoreg %>%
filter(ECO_NAME %in% sf_int$ECO_NAME) %>%
st_sample(., size = no_pnts, type = "random")
print(basename(occ_file))
return(bkg_ecoreg)
}
```
Checking the background points are where you expect for a random species
```{r}
rsp <- sample(1:length(occ_files),1)
check <- background_sampler(occ_files[rsp], 10000)
pnt_buff <- 0.15
xy <- read_csv(occ_files[rsp]) %>%
dplyr::select("decimalLongitude", "decimalLatitude") %>%
distinct() %>%
st_as_sf(.,
coords = c("decimalLongitude", "decimalLatitude"),
crs = 4326) %>%
st_intersection(., ecoreg)
ggplot(data = ecoreg) +
geom_sf(aes(fill = ECO_NAME, alpha = 0.6)) +
geom_sf(data = check, alpha = 0.1) +
geom_sf(data = xy,
colour = "red",
size = 2) +
coord_sf(
xlim = c(st_bbox(check)[1] - pnt_buff, st_bbox(check)[3] + pnt_buff),
ylim = c(st_bbox(check)[2] - pnt_buff, st_bbox(check)[4] + pnt_buff)
) +
theme_bw() +
theme(legend.position = "none") +
ggtitle(gsub(".csv", "", paste0(basename(occ_files[rsp]))))
```