From 532596a89b392b39206b2b511d5fbf0080b872e7 Mon Sep 17 00:00:00 2001 From: Paul Carvalho <143020124+Paul-Carvalho@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:48:33 -0700 Subject: [PATCH] 151 vignette for making spatial grids (#154) * add vignette for making a spatial grid file * update spatial grid vignette * update information on zone ID information in making spatial grid vignette * updated spatial grid vignette * update final line in description file --- DESCRIPTION | 2 +- vignettes/making_a_spatial_grid.Rmd | 90 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 vignettes/making_a_spatial_grid.Rmd diff --git a/DESCRIPTION b/DESCRIPTION index 212ee46a..42fd27df 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -107,4 +107,4 @@ Encoding: UTF-8 RoxygenNote: 7.3.1 LazyData: true Config/testthat/edition: 3 -GithubSHA1: d65b9a34f1ca5d334fbb9b749da6acc382df8f61 \ No newline at end of file +GithubSHA1: d65b9a34f1ca5d334fbb9b749da6acc382df8f61 diff --git a/vignettes/making_a_spatial_grid.Rmd b/vignettes/making_a_spatial_grid.Rmd new file mode 100644 index 00000000..0ba2b31c --- /dev/null +++ b/vignettes/making_a_spatial_grid.Rmd @@ -0,0 +1,90 @@ +--- +title: "Making a spatial grid file" +author: "Paul Carvalho" +date: "`r Sys.Date()`" +output: + rmarkdown::html_vignette: + fig_width: 6 + fig_height: 4 +vignette: > + %\VignetteIndexEntry{Making a spatial grid} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +editor_options: + markdown: + wrap: 72 +--- + + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +## Load packages +```{r packages} +library(sf) +library(ggplot2) +``` + +## Create a spatial grid + +```{r grid, echo=TRUE} +# Define the bounding box of the grid +# latitude: 10 to 40 degrees N +# longitude: -170 to -130 degrees W +bbox <- c(xmin = -170, xmax = -130, ymin = 10, ymax = 40) + +# Define the size of each grid cell +# 5 degree x 5 degree grid +cell_size <- c(x = 5, y = 5) + +# Create the grid +grid_sf <- st_make_grid( + st_as_sfc(st_bbox(bbox)), # Bounding box for the grid + cellsize = cell_size, # Size of each cell + what = "polygons" # Grid cells as polygons +) + +# Convert to an sf object +grid_sf <- st_sf(geometry = grid_sf, crs = 4326) +``` +If you have a spatial data table loaded as a dataframe, use the [st_as_sf()](https://www.rdocumentation.org/packages/sf/versions/1.0-16/topics/st_as_sf) function from the [sf](https://www.rdocumentation.org/packages/sf/versions/1.0-16) package to convert the dataframe to an sf object. + +### Add zone ID variable to the spatial object + +The zone ID variable in the primary data table can be reassigned to match the zone IDs in the spatial grid object based on the lat/lon of fishing location in the primary data table. + +```{r zoneID, echo=TRUE} +# Assign zone ID variable +# Here the ID is just a sequence from 1 to the number of grids in the spatial object +grid_sf$zoneID <- seq(1:length(grid_sf$geometry)) +``` + +### Plot +```{r plot_grid, echo=TRUE} +# Plot just the spatial grid object using the ggplot2 package +ggplot() + + geom_sf(data = grid_sf) + +# Add a world map layer to the plot +base_map <- ggplot2::map_data("world", + xlim = c(bbox["xmin"], bbox["xmax"]), + ylim = c(bbox["ymin"], bbox["ymax"])) + +base_map <- FishSET::dat_to_sf(base_map, lon = "long", lat = "lat", id = "group", + cast = "POLYGON", multi = TRUE, crs = 4326) + +ggplot() + + geom_sf(data = base_map) + + geom_sf(data = grid_sf, fill=NA) + + theme_minimal() + + labs(title = "5x5 Degree Spatial Grid") +``` + +### Save spatial grid +```{r save_grid, echo=TRUE} +# Uncomment the code below to save the spatial grid as an RDS file to be used in FishSET +# Note: the second input should include the filepath and filename to be saved. See help documentation for saveRDS(). + +# saveRDS(grid_sf, "fiveByFiveGrid.rds") +``` \ No newline at end of file