forked from Jaynabergman/Amphibian_study_extent_SDMs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.1_extract_continuous_surface_predictions_WLNP_sites.R
366 lines (173 loc) · 10.2 KB
/
7.1_extract_continuous_surface_predictions_WLNP_sites.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
########################## GOALS AND NOTES ###########################
### Project: Amphibian SDMs
### Author: Jayna Bergman
### Script name: extract_continuous_surface_predictions_WLNP_sites
### Goal of this Script:
# Extracts data from the predicted habitat suitability layers From within WLNP (from MaxEnt models made with random background points) for the species that we have independent data for
# One csv created with all species predictions at each site
### Notes:
# Only have data for BCFR, CSFR, LTSA, WETO
# The prediction surfaces from script "6.2_Maxent_random_pts_all_localities" were cropped in ArcGIS Pro to the WLNP extents BEFORE this script
# This was done to get an exact crop of the surfaces to the WLNP extent
# Last ran Feb 10, 2023
### Date: September 30, 2022
### Version of R: R version 4.0.3
########################### END SECTION ##############################
############################ LIBRARIES ###############################
library(dismo)
library(sf)
library(terra)
library(rgdal)
library(dplyr)
rm(list=ls())
########################### END SECTION ##############################
###################### FUNCTION DEFINITIONS ##########################
## NA
########################### END SECTION ##############################
########################## DATA AND ANALYSIS #########################
## Set working directory
setwd("C:/Users/jayna/OneDrive/Desktop/School/Lee_Yaw_Lab/Amphibian_SDMs/Data_Analysis")
## read in csv for independent survey site locations
WLNP_sites <- read.csv("./Independent_data_and_AUCs/WLNP_data/WLNP_Sites.csv")
SDM_predictions <- subset(WLNP_sites, select = c("SiteName"))
################################# LTSA ################################
##### range #####
## read in prediction raster
LTSA_range_predict_rast <- rast("./Maxent_Random_pts/LTSA/range/LTSA_range_all_locs/LTSA_range_prediction_surface_random.tif")
plot(LTSA_range_predict_rast)
## extract values from prediction layer under WLNP Sites
LTSA_range_predictions <- terra::extract(LTSA_range_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
LTSA_range_predictions <- LTSA_range_predictions %>%
rename(LTSA_range = layer)
SDM_predictions <- cbind(SDM_predictions, LTSA_range_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### political #####
## read in prediction raster
LTSA_political_predict_rast <- rast("./Maxent_Random_pts/LTSA/political/LTSA_political_all_locs/LTSA_political_prediction_surface_random.tif")
plot(LTSA_political_predict_rast)
## extract values from prediction layer under WLNP Sites
LTSA_political_predictions <- terra::extract(LTSA_political_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
LTSA_political_predictions <- LTSA_political_predictions %>%
rename(LTSA_political = LTSA_political_prediction_surface_random)
SDM_predictions <- cbind(SDM_predictions, LTSA_political_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### ecotone #####
## read in prediction raster
LTSA_ecotone_predict_rast <- rast("./Maxent_Random_pts/LTSA/ecotone/LTSA_ecotone_all_locs/LTSA_ecotone_prediction_surface_random.tif")
plot(LTSA_ecotone_predict_rast)
## extract values from prediction layer under WLNP Sites
LTSA_ecotone_predictions <- terra::extract(LTSA_ecotone_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
LTSA_ecotone_predictions <- LTSA_ecotone_predictions %>%
rename(LTSA_ecotone = layer)
SDM_predictions <- cbind(SDM_predictions, LTSA_ecotone_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
################################# BCFR ################################
##### range #####
## read in prediction raster
BCFR_range_predict_rast <- rast("./Maxent_Random_pts/BCFR/range/BCFR_range_all_locs/BCFR_range_prediction_surface_random.tif")
plot(BCFR_range_predict_rast)
## extract values from prediction layer under WLNP Sites
BCFR_range_predictions <- terra::extract(BCFR_range_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
BCFR_range_predictions <- BCFR_range_predictions %>%
rename(BCFR_range = BCFR_range_prediction_surface_random)
SDM_predictions <- cbind(SDM_predictions, BCFR_range_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### political #####
## read in prediction raster
BCFR_political_predict_rast <- rast("./Maxent_Random_pts/BCFR/political/BCFR_political_all_locs/BCFR_political_prediction_surface_random.tif")
plot(BCFR_political_predict_rast)
## extract values from prediction layer under WLNP Sites
BCFR_political_predictions <- terra::extract(BCFR_political_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
BCFR_political_predictions <- BCFR_political_predictions %>%
rename(BCFR_political = BCFR_political_prediction_surface_random)
SDM_predictions <- cbind(SDM_predictions, BCFR_political_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### ecotone #####
## read in prediction raster
BCFR_ecotone_predict_rast <- rast("./Maxent_Random_pts/BCFR/ecotone/BCFR_ecotone_all_locs/BCFR_ecotone_prediction_surface_random.tif")
plot(BCFR_ecotone_predict_rast)
## extract values from prediction layer under WLNP Sites
BCFR_ecotone_predictions <- terra::extract(BCFR_ecotone_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
BCFR_ecotone_predictions <- BCFR_ecotone_predictions %>%
rename(BCFR_ecotone = BCFR_ecotone_prediction_surface_random)
SDM_predictions <- cbind(SDM_predictions, BCFR_ecotone_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
################################# CSFR ################################
##### range #####
## read in prediction raster
CSFR_range_predict_rast <- rast("./Maxent_Random_pts/CSFR/range/CSFR_range_all_locs/CSFR_range_prediction_surface_random.tif")
plot(CSFR_range_predict_rast)
## extract values from prediction layer under WLNP Sites
CSFR_range_predictions <- terra::extract(CSFR_range_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
CSFR_range_predictions <- CSFR_range_predictions %>%
rename(CSFR_range = layer)
SDM_predictions <- cbind(SDM_predictions, CSFR_range_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### political #####
## read in prediction raster
CSFR_political_predict_rast <- rast("./Maxent_Random_pts/CSFR/political/CSFR_political_all_locs/CSFR_political_prediction_surface_random.tif")
plot(CSFR_political_predict_rast)
## extract values from prediction layer under WLNP Sites
CSFR_political_predictions <- terra::extract(CSFR_political_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
CSFR_political_predictions <- CSFR_political_predictions %>%
rename(CSFR_political = layer)
SDM_predictions <- cbind(SDM_predictions, CSFR_political_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### ecotone #####
## read in prediction raster
CSFR_ecotone_predict_rast <- rast("./Maxent_Random_pts/CSFR/ecotone/CSFR_ecotone_all_locs/CSFR_ecotone_prediction_surface_random.tif")
plot(CSFR_ecotone_predict_rast)
## extract values from prediction layer under WLNP Sites
CSFR_ecotone_predictions <- terra::extract(CSFR_ecotone_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
CSFR_ecotone_predictions <- CSFR_ecotone_predictions %>%
rename(CSFR_ecotone = layer)
SDM_predictions <- cbind(SDM_predictions, CSFR_ecotone_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
################################# WETO ################################
##### range #####
## read in prediction raster
WETO_range_predict_rast <- rast("./Maxent_Random_pts/WETO/range/WETO_range_all_locs/WETO_range_prediction_surface_random.tif")
plot(WETO_range_predict_rast)
## extract values from prediction layer under WLNP Sites
WETO_range_predictions <- terra::extract(WETO_range_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
WETO_range_predictions <- WETO_range_predictions %>%
rename(WETO_range = layer)
SDM_predictions <- cbind(SDM_predictions, WETO_range_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### political #####
## read in prediction raster
WETO_political_predict_rast <- rast("./Maxent_Random_pts/WETO/political/WETO_political_all_locs/WETO_political_prediction_surface_random.tif")
plot(WETO_political_predict_rast)
## extract values from prediction layer under WLNP Sites
WETO_political_predictions <- terra::extract(WETO_political_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
WETO_political_predictions <- WETO_political_predictions %>%
rename(WETO_political = layer)
SDM_predictions <- cbind(SDM_predictions, WETO_political_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
##### ecotone #####
## read in prediction raster
WETO_ecotone_predict_rast <- rast("./Maxent_Random_pts/WETO/ecotone/WETO_ecotone_all_locs/WETO_ecotone_prediction_surface_random.tif")
plot(WETO_ecotone_predict_rast)
## extract values from prediction layer under WLNP Sites
WETO_ecotone_predictions <- terra::extract(WETO_ecotone_predict_rast, WLNP_sites[,c("Long_m", "Lat_m")])
## cleaning csv
WETO_ecotone_predictions <- WETO_ecotone_predictions %>%
rename(WETO_ecotone = layer)
SDM_predictions <- cbind(SDM_predictions, WETO_ecotone_predictions)
SDM_predictions <- subset(SDM_predictions, select = -c(ID))
#### Saving csv with all species predictions at all study extents with site names
write.csv(SDM_predictions, "./Independent_data_and_AUCs/WLNP_data_predictions/predictions_for_WLNP_sites_continuous_surfaces.csv", row.names = FALSE)
########################### END SECTION ##############################
########################## FINAL COMMENTS ############################
########################### END SCRIPT ###############################