Skip to content

Commit

Permalink
Return lat_plot and lon_plot coordinates using make_2d_grid()
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-rohan-NOAA committed Oct 19, 2023
1 parent 9abcc83 commit 67d4c7d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: akgfmaps
Type: Package
Title: Alaska Groundfish and Ecosystem Survey Area Mapping
Version: 3.2.0
Version: 3.3.0
Authors@R: c(person("Sean", "Rohan", email = "[email protected]", role = c("aut", "cre")),
person("Jason", "Conner", email = "[email protected]", role = "ctb"),
person("Liz", "Dawson", email = "[email protected]", role = "ctb"),
Expand Down
51 changes: 51 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
akgfmaps 3.3.0 (October 19, 2023)
----------------------------------------------------------------

NEW FEATURE

- Added an include_grid_cell argument to make_2d_grid() that
makes the function return columns with the center coordinates
(lon_plot, lat_plot) of each grid cell prior to intersecting
with the user-provided sf object (passed to obj argument).
Centroids for intersected grid polygons are still returned by
the function for use in spatial analyses and index production
(e.g. VAST, sdmTMB) because they are the center of grid cells
after intersecting with stratum shapefiles. The centroid prior
to intersection is more useful for plotting gridded data with-
out prior transformation.

EXAMPLE

Grid cell centroids before intersection (regularly-spaced):

+-----------+-----------+
| | |
| | |
| x | x |
| | |
| | |
+-----------+-----------+

Grid cell centroids after intersection (irregularly-spaced;
useful for analyses that require accurate distances):

+-----------+-----------+
| /| |
| x / | |
| / | x |
| / x | |
| / | |
+-----------+-----------+

Grid cell centroids for plotting after intersection (still
regularly-spaced):

+-----------+-----------+
| /| |
| / | |
| X / | x |
| / | |
| / | |
+-----------+-----------+


akgfmaps 3.2.0 (October 6, 2023)
----------------------------------------------------------------

Expand Down
20 changes: 18 additions & 2 deletions R/make_2d_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#' @param output_type Type of output as a 1L character vector ("point", "raster", "polygon).
#' @param bbox Optional. A 4L numeric vector to define the edges of the interpolation grid c(xmin, ymin, xmax, ymax).
#' @param model Character vector indicating the geometry model to use for st_intersection boundaries. Default = "semi-open"; for details see ?s2::s2_options
#' @param include_tile_center Should the center point of each tile be included in the output? This helps with plotting data because the center point for grid locations after intesecting with the stratum shapefile is not always the center of a grid cell since intersected cells are not always squares.
#' @export

make_2d_grid <- function(obj, resolution = c(3704, 3704), output_type = "point", bbox = NULL, model = "semi-open") {
make_2d_grid <- function(obj, resolution = c(3704, 3704), output_type = "point", bbox = NULL, model = "semi-open", include_tile_center = FALSE) {

# Extract CRS
obj_srid <- sf::st_crs(obj, parameters = TRUE)$srid
Expand Down Expand Up @@ -62,7 +63,22 @@ make_2d_grid <- function(obj, resolution = c(3704, 3704), output_type = "point",

# Convert to polygon and find intersection with obj
interp_polygons <- terra::as.polygons(interp_grid) |>
sf::st_as_sf(crs = obj_srid) |>
sf::st_as_sf(crs = obj_srid)

# Add tile center coordinates for plotting
if(include_tile_center) {

coords_for_plots <- sf::st_centroid(interp_polygons) |>
sf::st_coordinates() |>
as.data.frame() |>
dplyr::rename(lon_plot = X, lat_plot = Y)

interp_polygons <- interp_polygons |>
dplyr::bind_cols(coords_for_plots)

}

interp_polygons <- interp_polygons |>
sf::st_intersection(obj, model = model)

# Convert to square kilometers
Expand Down
32 changes: 32 additions & 0 deletions assets/make_plot_grid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Return coordinates for plotting gridded data using make_2d_grid()

This example show how to use the make_2d_grid() function to produce grid cell centroid
coordinates that facilitate plotting data from 2d grids that are used for spatial
analyses (e.g. using VAST or sdmTMB). Otherwise this would require post-processing
manipulation of gridded data.

The plotting coordinate columns (lon_plot and lat_plot) should only be used for
visualization.

``` r
library(akgfmaps)

ebs_layers <- akgfmaps::get_base_layers(select.region = "sebs",
set.crs = "EPSG:3338")

ebs_grid <- akgfmaps:::make_2d_grid(obj = ebs_layers$survey.strata,
resolution = res,
bbox = vast_bbox,
output_type = "point",
include_tile_center = TRUE)

p1 <- ggplot() +
geom_tile(data = ebs_grid,
mapping = aes(x = lon_plot,
y = lat_plot,
fill = Stratum))

p1
```

![](/assets/ex_2d_grid_with_plot_centroids.png)
5 changes: 4 additions & 1 deletion man/make_2d_grid.Rd

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

0 comments on commit 67d4c7d

Please sign in to comment.