Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.3.0 #37

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
inst/doc
doc
Meta
*.png
*.lock

/vignettes/*.html
/vignettes/*.R
/vignettes/*.md
/vignettes/*.md
/vignettes/*.png
/vignettes/figure
/plots/
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
55 changes: 55 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
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 |
| / | |
| / | |
+-----------+-----------+

USAGE

See https://github.com/afsc-gap-products/akgfmaps/blob/main/assets/make_plot_grid.md


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
Binary file added assets/ex_2d_grid_with_plot_centroids.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions assets/make_plot_grid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Return coordinates for plotting gridded data using make_2d_grid()

### _Feature added in 3.3.0 (see [NEWS](/NEWS))_

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.

221 changes: 0 additions & 221 deletions test_3_0_0.R

This file was deleted.