-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
175 lines (175 loc) · 5.96 KB
/
.Rhistory
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
head(mcube)
# Required packages: tidyverse, rotl, sf, gdalUtilities, ape, rnaturalearth
library(tidyverse)
# Load config file
source("config.R")
# Load functions
source("./taxonmatch.R")
source("./pdmap.R")
source("./pdindicator.R")
source("./convert_multipolygons.R")
# Load tree
tree <- ape::read.tree(tree_path)
plot(tree, cex=0.45)
# Load datacube
# To do: find most recent common ancestor of tree, generate matching datacube
# through GBIF API
cube <- read.csv(cube_path, stringsAsFactors = FALSE)
# Match tree labels with gbif id's
matched <- taxonmatch(tree)
# Append OTT id's to occurrence cube
speciesKeys <- cube["speciesKey"] %>% distinct()
mtable <- speciesKeys %>% left_join(matched[,c("ott_id","gbif_id")],
by = join_by(speciesKey == gbif_id))
mcube <- cube %>% left_join(mtable[,c("speciesKey", "ott_id")],
by = join_by(speciesKey == speciesKey))
head(mcube)
View(mcube)
# Required packages: tidyverse, rotl, sf, gdalUtilities, ape, rnaturalearth
library(tidyverse)
# Load config file
source("config.R")
# Load functions
source("./taxonmatch.R")
source("./pdmap.R")
source("./pdindicator.R")
source("./convert_multipolygons.R")
# Load tree
tree <- ape::read.tree(tree_path)
plot(tree, cex=0.45)
# Load datacube
# To do: find most recent common ancestor of tree, generate matching datacube
# through GBIF API
cube <- read.csv(cube_path, stringsAsFactors = FALSE)
# Match tree labels with gbif id's
matched <- taxonmatch(tree)
# Append OTT id's to occurrence cube
speciesKeys <- cube["speciesKey"] %>% distinct()
mtable <- speciesKeys %>% left_join(matched[,c("ott_id","gbif_id")],
by = join_by(speciesKey == gbif_id))
mcube <- cube %>% left_join(mtable[,c("speciesKey", "ott_id")],
by = join_by(speciesKey == speciesKey))
# or user-input for highest taxon and generation/lookup for tree and cube
# eg tree_induced <- rotl::tol_induced_subtree(ott_id(taxa), label="name")
## To do: generate datacube through GBIF SQL API for species in user uploaded tree
#####################################################
## Run Biodiverse and feed tree and datacube to it.##
## Output metrics are stored as a cube ##############
#####################################################
# Output PD metrics are appended to the cube as new attribute 'PD'
PDcube <- mcube %>% select(year, eeaCellCode) %>% distinct(year, eeaCellCode,
.keep_all = TRUE) %>% mutate(PD = (rnorm(388, mean=5, sd=1.5)))
################################################
## Visualize PD on a map & calculate indicator##
################################################
# To do: build grid without loading external .shp file
# Merge grid cell geometry to cube
EEA_filepath <- "./shpfiles/EEA-reference-GRID-2013.gpkg"
sf::st_layers(EEA_filepath)
be_EEA <- sf::st_read(EEA_filepath, layer = "be_1km_polygon")
sf::st_crs(be_EEA)
PDcube_geo <- right_join(be_EEA, PDcube, by = join_by(CELLCODE == eeaCellCode ))
# Create map
## To do: get protected area polygons through google earth api
## Read in protected area polygons
pa_filepath <- "./shpfiles/20240528_protected_areas_BE.gpkg"
sf::st_layers(pa_filepath)
pa_natura <- sf::st_read(pa_filepath, layer = "NaturaSite_polygon")
# Plot PD map (use https://epsg.io/ for determining coordinates of desired
# geographic area)
map_PD <- pdmap(PDcube_geo, taxon, xmin, xmax, ymin, ymax)
print(map_PD)
# Calculate PD indicator
indicator <- pdindicator(PDcube_geo, cutoff)
print(paste(round(indicator), " % of high PD grid cells is currently located within a protected area."))
View(map_PD)
View(map_PD)
View(map_PD)
print(mp_PD)
print(map_PD)
getwd()
devtools::create("myfirstpackage")
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
devtools::document()
# add dplyr functions to namespace
#' @import dplyr
library(dplyr)
# Load config file
source("./config.R")
getwd()
# Load config file
source("./R/config.R")
# Load tree
tree <- ape::read.tree(tree_path)
tree
plot(tree, cex=0.45)
cube_path2 <- "./data/0114887-240626123714530.csv"
# Load datacube
# To do: generate matching datacube for user-uploaded tree through GBIF SQL API
cube2 <- read.csv(cube_path2, stringsAsFactors = FALSE)
# Load datacube
# To do: generate matching datacube for user-uploaded tree through GBIF SQL API
cube2 <- read.csv(cube_path2, stringsAsFactors = FALSE)
data.table::fread(cube_path2, stringsAsFactors = FALSE)
data.table::fread(cube_path2, stringsAsFactors = FALSE)
# Load config file
source("./R/config.R")
data.table::fread(cube_path2, stringsAsFactors = FALSE)
cube2 <- read.csv(cube_path2, stringsAsFactors = FALSE)
cube2
data.table::fread(cube_path2, stringsAsFactors = FALSE)
cube2 <- read.csv(cube_path, stringsAsFactors = FALSE)
cube2head(cube)
cube2
cube1<- data.table::fread(cube_path2, stringsAsFactors = FALSE)
str(cube2)
str(cube1)
cube1<- data.table::fread(cube_path2, stringsAsFactors = FALSE)
cube2 <- read.csv(cube_path, stringsAsFactors = FALSE)
ncol(cube1)
ncol(cube2)
ncol(cube_path)
ncol(cube_path2)
cube1 <- read.csv(cube_path, stringsAsFactors = FALSE)
cube2 <- read.csv(cube_path2, stringsAsFactors = FALSE, sep="\t")
head(cube1)
head(cube2)
cube <- cube2
tree2 <- ape::read.tree(tree_path2)
# Load config file
source("./R/config.R")
# Load config file
source("./R/config.R")
tree2 <- ape::read.tree(tree_path2)
plot(tree2, cex=0.45)
cube1 <- read.csv(cube_path, stringsAsFactors = FALSE)
cube <- cube2
tree <- tree2
# Match leaf labels of tree with GBIF id's and append OTT_id's to cube
mcube <- append_ott_id(tree, cube)
source("./R/taxonmatch.R")
source("./R/append_ott_id.R")
source("./R/pdmap.R")
source("./R/pdindicator.R")
source("./R/convert_multipolygons.R")
source("./R/calculate_PD.R")
mcube <- append_ott_id(tree, cube)
cube <- cube1
tree <- tree1
# Match leaf labels of tree with GBIF id's and append OTT_id's to cube
mcube <- append_ott_id(tree, cube)
tree
tree <- ape::read.tree(tree_path)
tree
taxonmatch(tree)
tree$tip.label
any(stringr::str_detect(tree_labels,'ott\\d+'))
tree_labels <- tree$tip.label
any(stringr::str_detect(tree_labels,'ott\\d+'))