-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping-kelp-results.qmd
484 lines (390 loc) · 19.4 KB
/
mapping-kelp-results.qmd
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
---
title: "Mapping Locations for Kelp Aquaculture in R"
author: "Elke Windschitl"
date: "2023-11-11"
format: html
editor: source
---
Description: In this qmd, I create a publishable map of locations that have habitat that is likely suitable for kelp aquaculture near Santa Barbara.
<div style="text-align:center; border-radius: 10px; overflow: hidden;">
<img src="images/kelp-map.png" alt="Map of locations with habitat likely suitable for kelp aquaculture in the Santa Barbara Channel." width="90%" style="border-radius: 10px;">
</div>
## Introduction
While earning my master's of environmental data science from the University of California, Santa Barbara, I completed a group capstone project with three other peers. Our clients included Natalie Dornan, a researcher at UCSB, and Ocean Rainforest, an aquaculture company with operations in Santa Barbara. We were tasked with combining open source data sets on giant kelp and various oceanographic factors in the region to create one synthesized data product. We were asked to use that data product to model habitat suitability for giant kelp to aid Ocean Rainforest in siting aquaculture projects in the future. Here, I map the final results of those efforts.
## The Data
I used six types of data in this process. I used the model results from our species distribution habitat suitability modeling, kelp data, substrate data, and boundary data of state boundaries, federal boundaries, and marine protected areas. Metadata on these data sources can be found in the [kelpGeoMod data repository](https://drive.google.com/drive/u/2/folders/1sJq_9RnsARR9mkmrcrn4O_1630VD-e-t).
## Methods
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
warning = FALSE,
message = FALSE)
```
First I load all of the libraries I will need. I use *sf* for handling shapefiles and vector data. I use *terra* and *raster* for handling raster data. *readr* is used to read in data, and *dplyr* is used for any data frame manipulation. I use *ggplot* to make my plots, but I leverage *maptiles* to get basemaps and *tidyterra* to use the basemap with ggplot. *ggspatial* is used to place a scale and arrow on the maps, and *cowplot* is used to plot maps together.
```{r message=FALSE}
# Load libraries
library(sf)
library(terra)
library(readr)
library(raster)
library(dplyr)
library(ggplot2)
library(maptiles)
library(tidyterra)
library(ggspatial)
library(cowplot)
#---- Set up the data directory
#data_dir <- insert your file path to your data
```
```{r include=FALSE}
data_dir <- "/Users/elkewindschitl/Documents/MEDS/kelpGeoMod/final-data"
```
I read in all of the necessary datasets from my local machine.
```{r output=FALSE}
# Read in data
# Read in project AOI
aoi <- st_read(file.path(data_dir, "02-intermediate-data/02-aoi-sbchannel-shapes-intermediate/aoi-sbchannel.shp"))
# Read in federal boundaries
fed_bounds <- st_read(file.path(data_dir, "01-raw-data/federal_boundaries/USMaritimeLimitsNBoundaries.shp")) %>%
filter(REGION == "Pacific Coast")
# Read in the 3-nautical mile state boundary
state_bounds <- st_read(file.path(data_dir, "01-raw-data/state_boundaries/stanford-sg211gq3741-shapefile/sg211gq3741.shp"))
# Read in MPAs
mpas <- st_read(file.path(data_dir, "01-raw-data/04-mpa-boundaries-raw/California_Marine_Protected_Areas_[ds582]/California_Marine_Protected_Areas_[ds582].shp"))
# Read in sandy-bottom raster
sandy_raster <- raster(file.path(data_dir, "03-analysis-data/05-substrate-analysis/sandy-bottom-1km.tif"))
# Read in kelp brick
kelp <- brick(file.path(data_dir, "02-intermediate-data/05-kelp-area-biomass-intermediate/kelp-area-brick.tif"))
# Read in the maxent modeling outputs
maxent_quarter_1 <- raster(file.path(data_dir, "03-analysis-data/04-maxent-analysis/results/maxent-quarter-1-output.tif"))
maxent_quarter_2 <- raster(file.path(data_dir, "03-analysis-data/04-maxent-analysis/results/maxent-quarter-2-output.tif"))
maxent_quarter_3 <- raster(file.path(data_dir, "03-analysis-data/04-maxent-analysis/results/maxent-quarter-3-output.tif"))
maxent_quarter_4 <- raster(file.path(data_dir, "03-analysis-data/04-maxent-analysis/results/maxent-quarter-4-output.tif"))
```
For the mapping, I want to find locations from the model output where raster grid cells...
1. Have a minimum of 0.4 habitat suitability in ALL quarters
2. Did not have any kelp seen in them (as of 2014-2022 from remotely sensed data)
3. Have sandy-bottom substrate
Because our client wouldn't want to place an aquaculture effort where kelp already exists, we need to define where kelp has been seen growing in the past 6 years. My teammate, Erika, perfomed analyses to find the average amount of kelp in each grid cell by season. Here is that code:
```{r}
# (Code from Erika)
# Create function to average kelp area per grid cell over all years by quarter
calc_seasonal_means_brick <- function(rast_to_convert) {
quarter_sets <- list(seq(from = 1, to = 36, by = 4), # Q1s (winter)
seq(from = 2, to = 36, by = 4), # Q2s (spring)
seq(from = 3, to = 36, by = 4), # Q3s (summer)
seq(from = 4, to = 36, by = 4)) # Q4s (fall)
all_seasons_brick <- brick() # set up brick to hold averaged layers for each season (will have 4 layers at the end)
for (i in seq_along(quarter_sets)) {
season_brick_holder <- brick() # hold all layers for one season, then reset for next season
for (j in quarter_sets[[i]]) {
season_brick <- brick() # hold single layer in a season, then reset for next layer
season_brick <- addLayer(season_brick, rast_to_convert[[j]]) # add single layer to initialized brick
season_brick_holder <- addLayer(season_brick_holder, season_brick) # add this layer to the holder for this season, and repeat until have all layers from season
}
season_averaged_layer <- calc(season_brick_holder, mean) # after having all layers from season, take the mean
all_seasons_brick <- addLayer(all_seasons_brick, season_averaged_layer) # add mean to the brick holding all averaged layers, and then repeat for the next season
}
return(all_seasons_brick) # return the resulting brick object
}
kelp_all <- calc_seasonal_means_brick(rast_to_convert = kelp)
kelp_quarter1 <- kelp_all[[1]]
kelp_quarter2 <- kelp_all[[2]]
kelp_quarter3 <- kelp_all[[3]]
kelp_quarter4 <- kelp_all[[4]]
```
### Masking the model output
As you can see from the quick plot below, the model output shows where along the Santa Barbara coastline has the highest predicted habitat suitability for kelp in each quarter. However, that information alone is not necessarily helpful in citing a new aquaculture farm.
```{r}
# quick plot maxent output
plot(maxent_quarter_1)
```
Also, you might note that there are gaps in the output. Unfortunately available data did not cover the entire Santa Barbara Channel. Our results are therefore limited.
To find locations that match our clients' needs, we need to do some further analysis and masking. First, I reclassify areas that have less than 40% likelihood of habitability to NA to remove them from our raster output. I do this for each quarter. I also mask out areas where kelp has already been seen growing. I stack and mask the quarters in a way that cells where there is existing kelp or a <0.4 probability of suitability in ANY of the quarters gets removed from the raster. Then I create one raster with the mean probability of suitability for elegible locations thus far. Here is that process and the resulting raster.
```{r}
# Prep data
# Reclassify values <0.4 to NA for each quarter and mask out areas with kelp
rcl_1 <- maxent_quarter_1 %>%
reclassify(cbind(0, 0.4, NA), right=FALSE) %>%
mask(mask = kelp_quarter1, inverse = TRUE)
rcl_2 <- maxent_quarter_2 %>%
reclassify(cbind(0, 0.4, NA), right=FALSE) %>%
mask(mask = kelp_quarter2, inverse = TRUE)
rcl_3 <- maxent_quarter_3 %>%
reclassify(cbind(0, 0.4, NA), right=FALSE) %>%
mask(mask = kelp_quarter3, inverse = TRUE)
rcl_4 <- maxent_quarter_4 %>%
reclassify(cbind(0, 0.4, NA), right=FALSE) %>%
mask(mask = kelp_quarter4, inverse = TRUE)
good_cells_stack <- stack(rcl_1, rcl_2, rcl_3, rcl_4) #stack
# Mask to areas that meet a minimum of 0.4 for all quarters
# Create an empty raster with the same extent and resolution as the input rasters
maxent_mask <- raster(good_cells_stack[[1]])
values(maxent_mask) <- 1 # Set all cells in the mask raster to a common value
# Iterate over each raster and update the mask raster where they overlap
for (i in 1:4) {
maxent_mask <- maxent_mask * (good_cells_stack[[i]]) # Update the mask where raster values are zero
}
# Mask rcl_1
masked_rcl_1 <- mask(rcl_1, maxent_mask)
# Mask rcl_2
masked_rcl_2 <- mask(rcl_2, maxent_mask)
# Mask rcl_3
masked_rcl_3 <- mask(rcl_3, maxent_mask)
# Mask rcl_4
masked_rcl_4 <- mask(rcl_4, maxent_mask)
# Take the mean value of all four quarters
mean_masked_maxent <- mean(masked_rcl_1, masked_rcl_2, masked_rcl_3, masked_rcl_4)
plot(mean_masked_maxent)
```
I'm most of the way there! Rocky-bottom seafloor is important habitat, so our client needs sandy-bottom seafloor to place aquaculture infrastructure and start a kelp farm. I only keep raster grid cells where sandy-bottom seafloor is present.
```{r}
# Convert to package terra
sandy_terra <- rast(sandy_raster)
maxent_terra <- rast(mean_masked_maxent)
# Mask maxent to areas with sandy-bottom
sub_masked_model <- terra::mask(x = maxent_terra, mask = sandy_terra, inverse = FALSE)
```
Next, I prepare the other layers of my map. I ensure that their coordinate reference system matches my habitat layer, and I intersect to retrieve shapes only in the area of interest (plus a slight buffer).
```{r}
# Transform to get every layer in same crs
aoi <- st_transform(aoi, crs = 4326)
mpas <- st_transform(mpas, crs = 4326)
fed_bounds <- st_transform(fed_bounds, crs = 4326)
state_bounds <- st_transform(state_bounds, crs = 4326)
# Define the amount of aoi buffer I want
buffer_size <- 10000
# Create a buffer around the AOI
buffered_aoi <- st_buffer(aoi, buffer_size)
# Reduce shape file data sets to only in my area of interest + buffer
aoi_mpas <- st_intersection(mpas, buffered_aoi)
aoi_fed_bounds <- st_intersection(fed_bounds, buffered_aoi)
aoi_state_bounds <- st_intersection(state_bounds, buffered_aoi)
```
I use the MPA layer to remove locations from the habitat map within any Marine Protected Area. In this case, there was only one suitable location within an MPA. I then convert to point data for better mapping.
```{r}
# Remove na values from raster
no_na <- mask(sub_masked_model, !is.na(sub_masked_model))
# Mask out locations within mpas
no_na_no_mpa <- mask(no_na, aoi_mpas, inverse = TRUE)
# Convert to point data
target_points <- as.data.frame(no_na_no_mpa, xy = TRUE, na.rm = TRUE) %>%
st_as_sf(coords = c("x", "y"),
crs = st_crs(4326))
```
I map my results using ggplot with a baselayer, state boundaries, federal boundaries, and marine protected areas. I create two maps -- one showing the entire area of interest, and one zoomed in to where the suitable habitat is located. I also make a custom legend to showcase what various boundaries in the map represent. Below I show each of these individual pieces.
```{r fig.width=10.19, fig.height=6.22, out.width="100%", out.height="100%"}
# Dowload tiles and compose raster (SpatRaster)
basemap <- get_tiles(
x = buffered_aoi, provider = "CartoDB.DarkMatterNoLabels", crop = TRUE,
cachedir = tempdir(), verbose = TRUE
)
# Get smaller box for inset map bounds
small_box <- tribble(
~lat, ~lon,
34.35, -119.54412,
34.5, -120.30751,
34.35, -120.30751,
34.5, -119.54412
) |>
st_as_sf(coords = c("lon", "lat"),
crs = "EPSG: 4326") |>
st_bbox() |>
st_as_sfc()
# Plot the full AOI
big_plot <- ggplot() +
geom_spatraster_rgb(
mapping = aes(),
basemap,
interpolate = TRUE,
r = 1,
g = 2,
b = 3
) +
geom_sf(data = aoi_fed_bounds,
linetype = "dotdash") +
geom_sf(data = aoi_state_bounds,
col = "black",
alpha = 0,
linetype = "solid",
linewidth = 0.3) +
geom_sf(data = aoi_mpas,
fill = "#a6a6a6",
colour = "#a6a6a6",
alpha = 0.1) +
geom_sf(data = aoi,
color = "#203e63",
alpha = 0,
linewidth = 0.8) +
geom_sf(data = target_points,
color = "#849638",
size = 0.05) +
geom_sf(data = small_box,
colour = "#de8728",
alpha = 0,
linewidth = 0.1) +
theme(panel.background = element_blank(),
plot.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
panel.grid = element_blank()) +
annotation_scale(pad_x = unit(1.1, "in"),
pad_y = unit(0.55, "in"),
bar_cols = c("black", "#262626"),
height = unit(0.25, "cm"),
text_col = "#a6a6a6") +
annotation_north_arrow(pad_x = unit(0.6, "in"),
pad_y = unit(0.45, "in"),
height = unit(0.25, "in"),
width = unit(0.3, "in"),
style = north_arrow_orienteering(fill = c("black", "#262626"),
text_col = "#a6a6a6"))
big_plot
```
```{r fig.width=5.91, fig.height=4.73, out.width="50%", out.height="50%"}
# Create a custom legend plot
legend_plot <- ggplot() +
geom_segment(aes(x = 0, xend = 1, y = 0, yend = 0, linetype = "1"), color = "black", linewidth = 2) +
geom_segment(aes(x = 0, xend = 1, y = -1, yend = -1, linetype = "2"), color = "black", linewidth = 2) +
geom_segment(aes(x = 0, xend = 1, y = -2, yend = -2, linetype = "3"), color = "#a6a6a6", linewidth = 2) +
geom_segment(aes(x = 0, xend = 1, y = -3, yend = -3, linetype = "4"), color = "#203e63", linewidth = 2) +
scale_linetype_manual(values = c("solid", "dotdash", "solid", "solid")) +
theme(panel.background = element_blank(),
plot.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
panel.margin = margin(c(0,0,500,0)),
plot.margin = margin(c(0,0,10,0))) +
coord_cartesian(clip = "off") +
theme(legend.position = "none") +
annotate("text",
x = .33,
y = -.3,
label = "State Boundary",
color = "black",
size = 12)+
annotate("text",
x = .38,
y = -1.3,
label = "Federal Boundary",
color = "black",
size = 12)+
annotate("text",
x = .47,
y = -2.3,
label = "Marine Protected Area",
color = "#a6a6a6",
size = 12) +
annotate("text",
x = .39,
y = -3.3,
label = "Project Study Area",
color = "#203e63",
size = 12)
legend_plot
# Save the legend using this trick:
# In RStudio, click on "Zoom", and then zoom the plot to the size and aspect ratio that satisfy you;
# Right click on the plot, then "Copy Image Address", paste the address somewhere, you get the width (www) and height (hhh) information in the address;
# ggsave("plot.png", width = www/90, height = hhh/90, dpi = 900) ggsave("plot.pdf", width = www/90, height = hhh/90)
#https://stackoverflow.com/questions/75020376/save-plot-exactly-as-previewed-in-the-plots-panel
# http://127.0.0.1:35585/graphics/plot_zoom_png?width=520&height=400
ggsave("images/kelp_legend.png", plot = legend_plot, width = 502/90, height = 400/90, units = "in", dpi = 900)
```
```{r fig.width=10.18, fig.height=5.46, out.width="100%", out.height="100%"}
# Crop mpas and state bounds to the inset map
aoi_mpas2 <- st_crop(mpas, small_box)
aoi_state_bounds2 <- st_crop(state_bounds, small_box)
# Dowload tiles and compose raster (SpatRaster)
basemap2 <- get_tiles(
x = small_box, provider = "CartoDB.DarkMatterNoLabels", crop = TRUE,
cachedir = tempdir(), verbose = TRUE
)
# Plot the inset map
small_plot <- ggplot() +
geom_spatraster_rgb(
mapping = aes(),
basemap2,
interpolate = TRUE,
r = 1,
g = 2,
b = 3
) +
geom_sf(data = aoi_state_bounds2,
col = "black",
alpha = 0,
linetype = "solid",
linewidth = 0.3) +
geom_sf(data = aoi_mpas2,
fill = "#a6a6a6",
colour = "#a6a6a6",
alpha = 0.1) +
geom_sf(data = target_points,
color = "#849638",
size = 2) +
geom_sf(data = small_box,
colour = "#de8728",
alpha = 0,
linewidth = 1) +
annotate("text",
x = -119.6982,
y = 34.43,
label = "Santa Barbara",
color = "#a6a6a6") +
annotate("text",
x = -119.86,
y = 34.43,
label = "Goleta",
color = "#a6a6a6") +
annotate("text",
x = -120.2149,
y = 34.478,
label = "Gaviota",
color = "#a6a6a6") +
theme(panel.background = element_blank(),
plot.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank()) +
annotation_scale(pad_x = unit(0.7, "in"),
pad_y = unit(0.2, "in"),
bar_cols = c("black", "#262626"),
height = unit(0.25, "cm"),
text_col = "#a6a6a6")
small_plot
```
I use cowplot to combine these maps.
```{r fig.width=13.09, fig.height=7.96, out.width="100%", out.height="100%"}
# Define the plot background color
bg_color <- "#262626"
# Grab legend from big plot
# legend <- get_legend(big_plot)
# Arrange the plots using plot_grid
combined_plot <- plot_grid(small_plot, big_plot, ncol = 1, axis = "l", rel_heights = c(1, 2.2))
# Set the background color for the entire combined plot + add lines
combined_plot <- combined_plot +
theme(plot.background = element_blank(),
axis.ticks = element_blank()) +
draw_line(
x = c(0.316, 0.167),
y = c(0.523, 0.715),
color = "#de8728", size = 0.2
) +
draw_line(
x = c(0.56, 0.836),
y = c(0.523, 0.715),
color = "#de8728", size = 0.2
)
# Display the combined plot
print(combined_plot)
# Save the plot
#http://127.0.0.1:35585/graphics/plot_zoom_png?width=1309&height=796
ggsave("images/kelp_plot.png", plot = combined_plot, width = 1309/90, height = 796/90, units = "in", dpi = 900)
```
## Results
Now that I have the combined map, and a custom legend, I chose to use a graphics editing software to finish off the map. I chose to use Inkscape, which is essentially an open-source version of Adobe Illustrator. In Inkscape I added a title, credits, a background, and our capstone hex logo. Here is the final product!
<div style="text-align:center; border-radius: 10px; overflow: hidden;">
<img src="images/kelp-map.png" alt="Map of locations with habitat likely suitable for kelp aquaculture in the Santa Barbara Channel." width="80%" style="border-radius: 10px;">
</div>
*Please note that these locations are based on habitat suitability -- a metric derived from environmental factors. There may be other factors that need to be considered when citing an aquaculture farm that relate to the activities already occurring in those parts of the ocean (such as fishing and boating). Permits must be obtained to conduct aquaculture efforts within state waters. The results of this project are only intended to inform our clients where habitat is most likely suitable for kelp growth.*