-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path01_dataLoad.v2.R
263 lines (203 loc) · 9.88 KB
/
01_dataLoad.v2.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
# maybe necessary if some raster aren't loading
# unlink(".RData")
######################################
## DATA COLLECTION & PRE-PROCESSING ##
######################################
#-------------------------------------------------------------------------------
## Function to load features, set common crs, and fix any invalid geometries
load_f <- function(f) {
proj.crs <- "+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
read_sf(f) %>%
st_transform(proj.crs) %>%
st_make_valid() %>%
st_buffer(dist = 0)
}
proj.crs <- "+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
#-------------------------------------------------------------------------------
# Load CONUS; retain western states.
usa <- load_f(paste0(data.dir, "/working/tl_2012_us_state.shp"))
keeps <- c("Washington", "Oregon", "California", "Idaho", "Montana",
"Wyoming", "Nevada", "Utah", "Colorado", "Arizona", "New Mexico")
west <- usa %>% filter(NAME %in% keeps)
wyo <- usa %>% filter(NAME == "Wyoming") %>% as_Spatial()
mt <- usa %>% filter(NAME == "Montana") %>% as_Spatial()
wyoArea <- terra::area(wyo) %>% sum()/1000000
# unique(west$NAME) ; plot(west)
remove(usa, keeps)
#-------------------------------------------------------------------------------
# Load sagebrush biome; clip to west
# sb <- load_f(paste0(local.data.dir,"eco/US_Sagebrush_Biome_2019.shp")) %>% st_crop(west)
#-------------------------------------------------------------------------------
# Load blm; convert to polygon using stars package; fix spatial issues (see links 00_setup)
# Ref: https://r-spatial.github.io/stars/
# Ref: https://gis.stackexchange.com/questions/192771/how-to-speed-up-raster-to-polygon-conversion-in-r
# sf::sf_use_s2(FALSE)
#
# blmWest <- raster(paste0(data.dir, "working/blm_west.tif"))
# blmWest <- sf::as_Spatial(sf::st_as_sf(stars::st_as_stars(blmWest),
# as_points = FALSE, merge = TRUE)) %>%
# st_as_sf() %>%
# st_make_valid() %>%
# ensure_multipolygons() %>%
# st_buffer(dist = 0) %>%
# st_transform(proj.crs)
#
# # Set non-BLM lands to NA (currently 0)
# blmWest[blmWest$blm_west == 0,] <- NA
# # Remove empty geometries
# blmWest <- blmWest %>% filter(!st_is_empty(.))
#
# # Generate Wyo-only layer (crop only gives bounding rectangle; intersect instead)
# blmWyo <- blmWest %>% st_intersection(st_as_sf(wyo))
# blmMT <- blmWest %>% st_intersection(st_as_sf(mt))
#
#
# # Write BLMWest and BLM Wyo layer
# shapefile(as_Spatial(blmWest), paste0(data.dir,"working/blm_west.shp"))
# shapefile(as_Spatial(blmWyo), paste0(data.dir,"working/blm_wyo.shp"))
# shapefile(as_Spatial(blmMT), paste0(data.dir,"working/blm_mt.shp"))
blmWest <- load_f(paste0(data.dir, "working/blm_west.shp"))
blmWyo <- load_f(paste0(data.dir,"working/blm_wyo.shp"))
blmMT <- load_f(paste0(data.dir, "working/blm_mt.shp"))
#-------------------------------------------------------------------------------
## Load AOIs
# Rock Springs Wyo: Red Desert all designations AND special mgmt area removed AND other areas
rd_allx <- load_f(paste0(data.dir,
"RockSpringsFO_Wyo/RedDesert-LittleSandyLandscape/RD_IBA_EraseAll2.shp")) %>%
as_Spatial()
# Rock Springs Wyo: Little Sandy
ls <- load_f(paste0(data.dir,
"RockSpringsFO_Wyo/RedDesert-LittleSandyLandscape/RD_LS_IBA_RSFO - Copy.shp")) %>%
filter(SITE_NAME == "Little Sandy Landscape") %>%
as_Spatial()
# shapefile(rd_allx, paste0(data.dir, "RockSpringsFO_Wyo/RedDesert-LittleSandyLandscape/red_desert_final.shp"))
# shapefile(ls, paste0(data.dir, "RockSpringsFO_Wyo/RedDesert-LittleSandyLandscape/little_sandy_final.shp"))
# Lewistown MT (only retain BLM areas within priortiy sage grouse habitat)
# lewis <- load_f(paste0(data.dir, "LewistownFO_MT/Lewistown ACEC files/Priority Sage-Grouse Habitat/Lewistown_GreaterSageGrouse_PriorityHabitat.shp")) %>%
# as_Spatial() %>%
# aggregate() #%>%
# lewis <- blmMT %>% st_intersection(st_as_sf(lewis)) %>%
# as_Spatial()
# shapefile(lewis, paste0(data.dir,"LewistownFO_MT/blm_in_lewistown_prioritySG.shp"))
# lewis <- load_f(paste0(data.dir,"LewistownFO_MT/blm_in_lewistown_prioritySG.shp")) %>%
# as_Spatial() %>%
# aggregate() #b/c multi-part
#-------------------------------------------------------------------------------
## Load indicators
setwd("G:/My Drive/2Pew ACEC/Pew_ACEC/data/working")
(list <- list.files(pattern= ".tif"))
# Load and process spp richness. These are orig from:
# https://www.sciencebase.gov/catalog/item/5bef2935e4b045bfcadf732c
# See notes at bottom re: mis-matched CRS from GEE.
# Only amph is given as an eample below, but did amph, bird, mamm, rept.
#
# start <- Sys.time()
# (amph_west <- raster(paste0(local.data.dir,
# "spp/amphibian_richness_habitat30m.tif")) %>% crop(west) %>% mask(west))
# writeRaster(amph_west, "amph_west.tif")
# (end <- start - Sys.time())
# start <- Sys.time()
# amph_west_270m <- amph_west %>% aggregate(fact = 9, fun = mean) # 30m --> 270m
# writeRaster(amph_west_270m, "amph_west_270m.tif", overwrite = TRUE)
# (end <- start - Sys.time())
(amph <- raster("amph_west_270m.tif"))
(bird <- raster("bird_west_270m.tif"))
(mamm <- raster("mamm_west_270m.tif"))
(rept <- raster("rept_west_270m.tif"))
(impSpp <- raster("impSppNorm.tif")) ; crs(impSpp) <- proj.crs
# Ecological connectivity, intactness, system div.
(connect <- raster("connNorm.tif"))
(intact <- raster("intactNorm.tif"))
(ecoRar <- raster("ecorarityaggto270norm.tif"))
(vegDiv <- raster("gapdiv270mnorm.tif"))
# Sage & annual herb
# RCMAP layers via GEE (sageNorm, annHerbNorm) had goofy CRS. Went to source at SciBase (even tho 30m)
# Both requisite layers for 2019 and 2020 errored, hence using 2018.
# Below, loaded orig layers at SciBase then cropped and masked to sagebrush biome.
# start <- Sys.time()
# (sage <- raster(paste0(local.data.dir,
# "eco/Sagebrush_2009_2020/rcmap_sagebrush_2018.img")) %>% crop(sb) %>% mask(sb))
# writeRaster(sage, "sage.tif")
# (end <- start - Sys.time())
#
# start <- Sys.time()
# (annHerb <- raster(paste0(local.data.dir,
# "eco/Annual_Herbaceous_2009_2020/rcmap_annual_herbaceous_2018.img")) %>% crop(sb) %>% mask(sb))
# writeRaster(annHerb, "annHerb.tif")
# (end <- start - Sys.time())
#
# (sage <- raster("sage.tif"))
# (annHerb <- raster("annHerb.tif"))
#
# start <- Sys.time()
# boo <- sage %>% resample(amph)
# writeRaster(boo, "sage_270m.tif")
# (end <- start - Sys.time())
#
# start <- Sys.time()
# foo <- annHerb %>% resample(amph)
# writeRaster(foo, "annHerb_270m.tif")
# (end <- start - Sys.time())
(sage <- raster("sage_270m.tif"))
(annHerb <- raster("annHerb_270m.tif"))
# Clim
(climAcc <- raster("ClimAccNorm.tif"))
(climStab <- raster("ClimStabNorm.tif"))
# Geophys
(geoDiv <- raster("div_ergo_lth270mnorm.tif"))
(geoRar <- raster("georarity270mnorm.tif"))
# Water
(waterAvail <- raster("wateravail_allwater2.tif"))
(waterFut <- raster("wateruseddwaterdist2norm.tif"))
# Nat res
(geotherm <- raster("geotherm_lt10pslop_nourbFWPAspldist.tif"))
(oilGas <- raster("oilgas5k6cellmean270mnorm_PAs0UrbH20.tif"))
(mineral <- raster("mrdsPA5kmeanmnormPAs0UrbH20.tif"))
(solar <- raster("maxdnighi_lt5pslope_ddpowerline4normPAs0UrbH20.tif"))
(wind <- raster("windprobi_lt30pslope_ddpowerline4normPAs0UrbH20MULT.tif"))
# Misc
(nightDark <- raster("virrs2011.tif"))
# Migration routes.
w1 <- load_f(paste0(data.dir,"source/WGFD_MuleDeerCorridorComplexes/MuleDeerMigrationBaggsWGFDCorridor.shp")) %>% as_Spatial()
w2 <- load_f(paste0(data.dir,"source/WGFD_MuleDeerCorridorComplexes/MuleDeerMigrationPlatteValleyWGFDCorridor.shp")) %>% as_Spatial()
w3 <- load_f(paste0(data.dir,"source/WGFD_MuleDeerCorridorComplexes/MuleDeerMigrationSubletteWGFDCorridor.shp")) %>% as_Spatial()
# wyoMule <- raster::bind(w1, w2, w3) %>% st_as_sf() %>% fasterize(amph)
migr <- union(w1, w2) %>% union(w3)
remove(w1, w2, w3)
#IBA
# iba <- load_f(paste0(local.data.dir,"/IBA_CONUS/Important_Bird_Areas_Polygon_Public_View.shp"))
# iba_mt <- st_as_sf(iba) %>% st_intersection(st_as_sf(mt)) %>% as_Spatial()
# shapefile(iba_mt, paste0(local.data.dir,"/IBA_CONUS/iba_mt.shp"))
# iba_mt <- load_f(paste0(local.data.dir,"/IBA_CONUS/iba_mt.shp"))
iba_wyo <- load_f(paste0(data.dir, "RockSpringsFO_Wyo/audiba_WY_20150818/audiba_WY_20150818.shp")) %>% as_Spatial()
setwd("G:/My Drive/2Pew ACEC/Pew_ACEC/")
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
## Spp richness CRS conundrum.
# These are orig from https://www.sciencebase.gov/catalog/item/5bef2935e4b045bfcadf732c
# I had downloaded, from SciBase, uploaded to GEE, then re-downloaded clipped and at lower res via Colab.
# In GEE/Colab, projection is given as:
# 'PROJCS["NAD_1983_Albers",
# \n GEOGCS["NAD83",
# \n DATUM["North_American_Datum_1983",
# \n SPHEROID["GRS 1980", 6378137.0, 298.2572221010042, AUTHORITY["EPSG","7019"]],
# \n AUTHORITY["EPSG","6269"]],
# \n PRIMEM["Greenwich", 0.0],
# \n UNIT["degree", 0.017453292519943295],
# \n AXIS["Longitude", EAST],
# \n AXIS["Latitude", NORTH],
# \n AUTHORITY["EPSG","4269"]],
# \n PROJECTION["Albers_Conic_Equal_Area"],
# \n PARAMETER["central_meridian", -96.0],
# \n PARAMETER["latitude_of_origin", 23.0],
# \n PARAMETER["standard_parallel_1", 29.5],
# \n PARAMETER["false_easting", 0.0],
# \n PARAMETER["false_northing", 0.0],
# \n PARAMETER["standard_parallel_2", 45.5],
# \n UNIT["m", 1.0],
# \n AXIS["x", EAST],
# \n AXIS["y", NORTH]]'}
# Yet loading the GEE/Colab export in here gives a crs of:
# +proj=aea +lat_0=0 +lon_0=0 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs
# So somehow export screwed this up? Assigning crs that matches GEE/Colab AND USGS orig works:
# +proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs