Skip to content

Commit

Permalink
add MSSM 2x2 survey grid data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Dunic committed Oct 5, 2023
1 parent 2087664 commit 7ffda01
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
^scratch$
^NEWS\.md$
^\.github$
^data-raw$
19 changes: 19 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Multispecies Small-Mesh Bottom Trawl (MSSM) Survey Grid
#'
#' A 2x2 km grid for the Multispecies Small-Mesh Bottom Trawl Survey (MSSM; formerly
#' known as 'shrimp survey'). This grid covers WCVI Shrimp Survey Areas 124 and 125.
#' The `last_samp_year is the last year a grid cell was sampled as of 2023, with
#' the most consistent resampling (spatially) occurring in grid cells last sampled
#' after 2008 (GFBioField used used starting in 2009).
#'
#' @format ## `mssm_grid`
#' A data frame with 493 rows and 5 columns:
#' \describe{
#' \item{survey_abbrev}{Survey abbreviation}
#' \item{longitude, latitude}{Longitude and latitude of the centroid of 2x2 km
#' grid cells}
#' \item{area}{Area of the grid cells, in km^2}
#' \item{last_samp_year}{The last year a grid cell was sampled as of the date
#' this dataset was created (Oct 2023)}
#' }
"mssm_grid"
66 changes: 66 additions & 0 deletions data-raw/mssm_grid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
library(magrittr)

# Spherical geometries problematic
sf::sf_use_s2(FALSE)

# Query GFBio - need DFO VPN access
#pcod_dat <- gfdata::get_survey_sets(species = "pacific cod", ssid = 7)
#saveRDS(pcod_dat, file.path('data-raw', 'pcod-mssm-survey-sets.rds'))
pcod_dat <- readRDS(file.path('data-raw', 'pcod-mssm-survey-sets.rds'))
pcod_dat <- pcod_dat |>
dplyr::filter(!is.na(longitude)) |>
sdmTMB::add_utm_columns(c('longitude', 'latitude')) |>
dplyr::mutate(row_id = dplyr::row_number())

# Use equal distance projection
pcod_sf <-
pcod_dat |>
dplyr::select(year, X, Y) |>
sf::st_as_sf(coords = c('X', 'Y'), crs = 32609)

pcod_years <- dplyr::select(pcod_dat, row_id, year)

# Use 2x2 km grid size in units of our polygon shape file
grid_spacing <- 2

# Create grid over the bounding box of the polygon
full_grid <- pcod_sf |>
sf::st_make_grid(cellsize = c(grid_spacing, grid_spacing)) |>
sf::st_as_sf() |>
dplyr::rename(geometry = x)

# Get grid cells that overlap with at least one sampling point
intersected <- sf::st_intersects(full_grid, pcod_sf)

id_intersect <- intersected |> purrr::map_dbl(length) > 0
last_year <- intersected |>
purrr::map_dbl(\(row) if (length(row) > 0) {max(pcod_years[row, ]$year)} else {NA})

full_grid$last_samp_year <- last_year

mssm_grid_sf <- full_grid[id_intersect, ] |>
dplyr::mutate(survey_abbrev = "MSSM WCVI")

mssm_grid <- mssm_grid_sf |>
sf::st_centroid() %>%
dplyr::mutate(survey_abbrev = "MSSM WCVI",
#ssid = 7,
longitude = sf::st_coordinates(.)[,1],
latitude = sf::st_coordinates(.)[,2],
area = grid_spacing * grid_spacing) |>
sf::st_drop_geometry() |>
dplyr::as_tibble() |>
dplyr::select(survey_abbrev, longitude, latitude, area, last_samp_year)

# Option to save an SF object
#saveRDS(mssm_grid_sf, file.path('data-raw', 'mssm_grid_sf.rds'))
#usethis::use_data(mssm_grid)
# Visualise grid before and after 2009
# mssm_grid_sf |>
# mutate(last_samp_year = ifelse(last_samp_year >= 2009, ">= 2009", " < 2009")) |>
# ggplot(data = _) +
# geom_sf(aes(fill = last_samp_year), alpha = 0.8) +
# viridis::scale_fill_viridis(discrete = TRUE) +
# gfplot::theme_pbs()

usethis::use_data(mssm_grid, overwrite = TRUE)
Binary file added data-raw/pcod-mssm-survey-sets.rds
Binary file not shown.
31 changes: 31 additions & 0 deletions man/mssm_grid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7ffda01

Please sign in to comment.