forked from Jaynabergman/Amphibian_study_extent_SDMs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.3_removing_duplicates_input_localities_dismo.R
218 lines (102 loc) · 5.67 KB
/
3.3_removing_duplicates_input_localities_dismo.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
########################## GOALS AND NOTES ###########################
### Project: Amphibian SDMs
### Author: Jayna Bergman
### Script name: 3.3_removing_duplicates_input_localities_dismo
### Goal of this Script:
# Read filtered input localities and remove duplicates from within the same raster grid cell using dismo package
### Notes:
# Raster is for all of North America and at 1km resolution (in aea)
### Date: May 22, 2022
### Version of R: R version 4.0.3
########################### END SECTION ##############################
############################ LIBRARIES ###############################
library(dismo)
library(sf)
library(terra)
library(tidyverse)
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 Raster to define the study extent and the resolution (using all of North america and 1km resolution)
r <- raster("./envi_variables/ClimateNA_variables/aea/Normal_1991_2020_MAP_aea.tif")
plot(r)
################################# LTSA ################################
## Read in all LTSA sites & get into a two column Matrix
LTSAsites <- read.csv("./input_localities/pre_processing/LTSA/LTSA_filtered_aea.csv")
LTSApts <- LTSAsites[,c("Long_m", "Lat_m")]
## Thin for localities in the same cell
LTSA_thinned <- gridSample(LTSApts, r, n=1)
plot(LTSA_thinned)
## Adding other columns back onto thinned coordinates
LTSA_thinned_pts <- LTSAsites[row.names(LTSA_thinned),]
## saving a new CSV
write.csv(LTSA_thinned_pts,file="./input_localities/pre_processing/LTSA/LTSA_dupl_rm.csv",row.names=FALSE)
########################### END SECTION ###############################
################################# BCFR ################################
## Read in all BCFR sites & get into a two column Matrix
BCFRsites <- read.csv("./input_localities/pre_processing/BCFR/BCFR_filtered_aea.csv")
BCFRpts <- BCFRsites[,c("Long_m", "Lat_m")]
## Thin for localities in the same cell
BCFR_thinned <- gridSample(BCFRpts, r, n=1)
plot(BCFR_thinned)
## Adding other columns back onto thinned coordinates
BCFR_thinned_pts <- BCFRsites[row.names(BCFR_thinned),]
## saving a new CSV
write.csv(BCFR_thinned_pts,file="./input_localities/pre_processing/BCFR/BCFR_dupl_rm.csv",row.names=FALSE)
########################### END SECTION ###############################
################################# CATO ################################
## Read in all CATO sites & get into a two column Matrix
CATOsites <- read.csv("./input_localities/pre_processing/CATO/CATO_filtered_aea.csv")
CATOpts <- CATOsites[,c("Long_m", "Lat_m")]
## Thin for localities in the same cell
CATO_thinned <- gridSample(CATOpts, r, n=1)
plot(CATO_thinned)
## Adding other columns back onto thinned coordinates
CATO_thinned_pts <- CATOsites[row.names(CATO_thinned),]
## saving a new CSV
write.csv(CATO_thinned_pts,file="./input_localities/pre_processing/CATO/CATO_dupl_rm.csv",row.names=FALSE)
########################### END SECTION ###############################
################################# CSFR ################################
## Read in all CSFR sites & get into a two column Matrix
CSFRsites <- read.csv("./input_localities/pre_processing/CSFR/CSFR_filtered_aea.csv")
CSFRpts <- CSFRsites[,c("Long_m", "Lat_m")]
## Thin for localities in the same cell
CSFR_thinned <- gridSample(CSFRpts, r, n=1)
plot(CSFR_thinned)
## Adding other columns back onto thinned coordinates
CSFR_thinned_pts <- CSFRsites[row.names(CSFR_thinned),]
## saving a new CSV
write.csv(CSFR_thinned_pts,file="./input_localities/pre_processing/CSFR/CSFR_dupl_rm.csv",row.names=FALSE)
########################### END SECTION ###############################
################################# TISA ################################
## Read in all TISA sites & get into a two column Matrix
TISAsites <- read.csv("./input_localities/pre_processing/TISA/TISA_filtered_aea.csv")
TISApts <- TISAsites[,c("Long_m", "Lat_m")]
## Thin for localities in the same cell
TISA_thinned <- gridSample(TISApts, r, n=1)
plot(TISA_thinned)
## Adding other columns back onto thinned coordinates
TISA_thinned_pts <- TISAsites[row.names(TISA_thinned),]
## saving a new CSV
write.csv(TISA_thinned_pts,file="./input_localities/pre_processing/TISA/TISA_dupl_rm.csv",row.names=FALSE)
########################### END SECTION ###############################
################################# WETO ################################
## Read in all WETO sites & get into a two column Matrix
WETOsites <- read.csv("./input_localities/pre_processing/WETO/WETO_filtered_aea.csv")
WETOpts <- WETOsites[,c("Long_m", "Lat_m")]
## Thin for localities in the same cell
WETO_thinned <- gridSample(WETOpts, r, n=1)
plot(WETO_thinned)
## Adding other columns back onto thinned coordinates
WETO_thinned_pts <- WETOsites[row.names(WETO_thinned),]
## saving a new CSV
write.csv(WETO_thinned_pts,file="./input_localities/pre_processing/WETO/WETO_dupl_rm.csv",row.names=FALSE)
########################### END SECTION ##############################
########################## FINAL COMMENTS ############################
########################### END SCRIPT ###############################