-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_prepare_covariate_data.R
168 lines (140 loc) · 6.7 KB
/
02_prepare_covariate_data.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
# 02_prepare_covariate_data.R
# Author: Benjamin R. Goldstein
# Date: 2/23/2021
### Script 2: Associate eBird checklists with spatial covariate data
# (0) Load inputs
# (1) Get data rasters
# (2) Create buffers for landcover data extraction
# (3) Assign data to checklists
# (4) Observations are aggregated to a 50m spatial grid
library(sp)
library(raster)
library(tidyverse)
library(lubridate)
library(foreign)
##### 0. Load data #####
CA_checklists <- read_csv("intermediate/CA_checklists_processed.csv")
evt1 <- raster("data/lf27569570_US_200EVT/US_200EVT/us_200evt/w001001.adf")
evt2 <- raster("data/lf48292806_US_200EVT/US_200EVT/us_200evt/w001001.adf")
evt3 <- raster("data/lf66819691_US_200EVT/US_200EVT/us_200evt/w001001.adf")
evt4 <- raster("data/lf69779486_US_200EVT/US_200EVT/us_200evt/w001001.adf")
evt_table <-
foreign::read.dbf("data/lf27569570_US_200EVT/US_200EVT/US_200EVT.dbf")
centers <- read_csv("intermediate/chosen_subregions.csv")
center_pts <- SpatialPointsDataFrame(coords = centers[, c("x", "y")],
data = centers,
proj = CRS("+proj=aea +lat_1=34 +lat_2=40.5
+lat_0=0 +lon_0=-120
+x_0=0 +y_0=-4000000
+ellps=GRS80 +datum=NAD83
+units=m +no_defs")) %>%
spTransform(crs(evt1))
CA_map <- maps::map(database = "state", regions = "CA", fill = T, plot = F)
CA_poly <- maptools::map2SpatialPolygons(
CA_map, IDs = "california", proj4string = CRS("+proj=longlat +datum=WGS84")
) %>% spTransform(crs(evt1))
center_buffers <- rgeos::gBuffer(center_pts, width = 7500,
byid = TRUE, id = center_pts$cID)
r <- raster::getData("worldclim", var = "alt", res = 0.5, path = "data",
lon = -125, lat = 45)
r2 <- raster::getData("worldclim", var = "alt", res = 0.5, path = "data",
lon = -115, lat = 45)
target_raster <- raster(extent(CA_poly), resolution = 150, crs = crs(CA_poly))
r_elev <- raster::mosaic(r, r2, fun = mean) %>%
raster::projectRaster(target_raster)
##### 1. Get rasters at each subregion #####
# Get data for each buffer
checklists_with_data <- list()
for (ctr in 1:length(center_buffers)) {
### Get the cover data needed for this raster
needed_base_rasters <- list(evt1, evt2, evt3, evt4)[which(
unlist(lapply(c(evt1, evt2, evt3, evt4), function (xraster) {
!is.null(raster::intersect(extent(xraster), extent(center_buffers[ctr,])))
})))]
for (j in 1:length(needed_base_rasters)) {
needed_base_rasters[[j]] <-
raster::crop(needed_base_rasters[[j]], extent(center_buffers[ctr,]))
}
if (length(needed_base_rasters) == 1) {
cover_raster <- needed_base_rasters[[1]]
} else if (length(needed_base_rasters) == 2) {
cover_raster <- raster::merge(needed_base_rasters[[1]],
needed_base_rasters[[2]])
} else if (length(needed_base_rasters) == 3) {
cover_raster <- raster::merge(needed_base_rasters[[1]],
needed_base_rasters[[2]],
needed_base_rasters[[3]])
} else if (length(needed_base_rasters) == 4) {
cover_raster <- raster::merge(needed_base_rasters[[1]],
needed_base_rasters[[2]],
needed_base_rasters[[3]],
needed_base_rasters[[4]])
}
elev_raster <- crop(r_elev, extent(center_buffers[ctr,]))
values(elev_raster)[is.na(values(elev_raster))] <- 0
### Get a raster of each landcover type: water, vegetation, agriculture, urban
water_raster <- cover_raster
values(water_raster) <- values(cover_raster) %in%
unique(evt_table$VALUE[evt_table$EVT_LF == "Water"])
ag_raster <- cover_raster
values(ag_raster) <- values(cover_raster) %in%
unique(evt_table$VALUE[evt_table$EVT_LF == "Agriculture"])
veg_raster <- cover_raster
values(veg_raster) <- values(cover_raster) %in%
unique(evt_table$VALUE[evt_table$EVT_LF %in% c("Shrub", "Herb")])
urban_raster <- cover_raster
values(urban_raster) <- values(cover_raster) %in%
unique(evt_table$VALUE[evt_table$EVT_LF == "Developed"])
tree_raster <- cover_raster
values(tree_raster) <- values(cover_raster) %in%
unique(evt_table$VALUE[evt_table$EVT_LF == "Tree"])
### Get a 600m buffer around each point of each covariate
buffer <- 600
buffer_mtx <- matrix(0, buffer / 30 + 1, buffer / 30 + 1)
for (i in 1:nrow(buffer_mtx)) for (j in 1:nrow(buffer_mtx)) {
if ((i - (nrow(buffer_mtx)/2+0.5))^2 +
(j - (nrow(buffer_mtx)/2+0.5))^2 <=
(nrow(buffer_mtx)/2 - 0.49)^2)
buffer_mtx[i, j] <- 1
else buffer_mtx[i, j] <- 0
}
buffer_mtx <- buffer_mtx / sum(buffer_mtx)
water_neigh <- focal(water_raster, buffer_mtx)
ag_neigh <- focal(ag_raster, buffer_mtx)
urban_neigh <- focal(urban_raster, buffer_mtx)
tree_neigh <- focal(tree_raster, buffer_mtx)
veg_neigh <- focal(veg_raster, buffer_mtx)
### Now I need to associate the data for this center with its checklists...
checklists_this_ctr <- CA_checklists %>%
filter(center == center_buffers[ctr,]$cID)
this_ctr_points <- SpatialPointsDataFrame(
coords = checklists_this_ctr[, c("gx", "gy")],
data = checklists_this_ctr,
proj4string = CRS("+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120
+x_0=0 +y_0=-4000000 +ellps=GRS80 +datum=NAD83 +units=m
+no_defs")) %>%
spTransform(CRSobj = crs(evt1))
checklists_this_ctr$elevation <- raster::extract(elev_raster, this_ctr_points)
checklists_this_ctr$pct_veg <- raster::extract(veg_neigh, this_ctr_points)
checklists_this_ctr$pct_water <- raster::extract(water_neigh, this_ctr_points)
checklists_this_ctr$pct_urban <- raster::extract(urban_neigh, this_ctr_points)
checklists_this_ctr$pct_ag <- raster::extract(ag_neigh, this_ctr_points)
checklists_this_ctr$pct_tree <- raster::extract(tree_neigh, this_ctr_points)
checklists_this_ctr$pct_veg_comb <- checklists_this_ctr$pct_tree +
checklists_this_ctr$pct_veg
checklists_with_data[[ctr]] <- checklists_this_ctr
}
#### 2. Prepare and write output ####
checklists_with_data_df <- do.call(bind_rows, checklists_with_data)
if (nrow(checklists_with_data_df) != nrow(CA_checklists)) stop("Lost some data...")
checklists_with_data_df <- checklists_with_data_df %>%
filter(
!is.na(elevation),
!is.na(pct_veg),
!is.na(pct_water),
!is.na(pct_urban),
!is.na(pct_tree),
!is.na(pct_veg_comb),
)
write_csv(checklists_with_data_df,
"intermediate/CA_checklists_w_covariates.csv")