Skip to content

Commit 08dfba8

Browse files
authored
Merge branch 'main' into epu_sf
2 parents 3e80892 + afd0b08 commit 08dfba8

File tree

59 files changed

+736
-109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+736
-109
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
^\.Rproj.user$
99
^docs$
1010
^pkgdown$
11+
^vignettes/articles$

.github/workflows/pkgdown.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ on:
66
- main
77
- master
88

9-
109
jobs:
1110
pkgdown:
1211
runs-on: macOS-latest
1312
env:
1413
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
1514
steps:
1615
- uses: actions/checkout@v3
17-
1816
- uses: r-lib/actions/setup-r@v2
19-
2017
- uses: r-lib/actions/setup-pandoc@v2
2118

2219
- name: Query dependencies
@@ -27,27 +24,31 @@ jobs:
2724
shell: Rscript {0}
2825

2926
- name: Cache R packages
30-
uses: actions/cache@v2
27+
uses: actions/cache@v3
3128
with:
3229
path: ${{ env.R_LIBS_USER }}
3330
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
3431
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
3532

36-
3733
- name: Install binary dependencies
3834
if: runner.os != 'Linux'
3935
run: |
40-
remotes::install_deps(dependencies = TRUE, type="binary")
36+
remotes::install_deps(dependencies = TRUE, type = "binary")
4137
shell: Rscript {0}
4238

4339
- name: Install pkgdown
4440
run: |
45-
install.packages("pkgdown", type = "binary")
41+
install.packages(c("pkgdown", "yaml"), type = "binary")
4642
shell: Rscript {0}
47-
43+
4844
- name: Install package
4945
run: R CMD INSTALL .
50-
46+
47+
- name: Create leaflet maps
48+
run: |
49+
source("./data-raw/create_leaflet_maps.R", echo = TRUE)
50+
shell: Rscript {0}
51+
5152
- name: Deploy package
5253
run: |
5354
git config --local user.email "[email protected]"

DESCRIPTION

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ Package: NEFSCspatial
22
Title: Spatial data package
33
Version: 0.0.1
44
Authors@R: c(person("Andy", "Beet", , "[email protected]", role = c("aut", "cre")), person("Kimberly", "Bastille", , "[email protected]", role = "aut"), person("Joshua", "Hatch", , "[email protected]", role = "aut"))
5-
URL: https://github.com/andybeet/NEFSCspatial/
6-
BugReports: https://github.com/andybeet/NEFSCspatial/issues
7-
Description: Provides spatial data as sf or raster objects
5+
URL: https://github.com/NEFSC/NEFSC-Spatial
6+
BugReports: https://github.com/NEFSC/NEFSC-Spatial/issues
7+
Description: Provides spatial data as sf objects
88
License: file LICENSE
99
Encoding: UTF-8
1010
Roxygen: list(markdown = TRUE)
1111
RoxygenNote: 7.2.3
1212
Depends:
1313
R (>= 3.5.0)
1414
Imports:
15-
sf,
16-
raster
15+
sf
1716
Suggests:
1817
curl,
1918
here,
@@ -26,6 +25,7 @@ Suggests:
2625
magick,
2726
hexSticker,
2827
ggimage,
29-
dplyr
28+
dplyr,
29+
leaflet
3030
LazyData: true
3131
VignetteBuilder: knitr

R/data_Neus_atlantis.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' Northeast US (NEUS) atlantis model domain
2+
#'
3+
#' An sf object containing the box level boundaries used in the Northeast Atlantis ecosystem end to end model
4+
#'
5+
#' @format An \code{sf} object:
6+
#' \describe{
7+
#' \item{Geometry type}{POLYGON}
8+
#' \item{Dimension}{XY}
9+
#' \item{Bounding box}{xmin: -76.45919 ymin: 34.76128 xmax: -63.42171 ymax: 46.11478}
10+
#' \item{Geodetic CRS}{WGS 83}
11+
#' \item{Features}{30 x 10}
12+
#' \item{Metadata}{See source}
13+
#' }
14+
#'
15+
#' @docType data
16+
#' @name Neus_atlantis
17+
#' @usage data("Neus_atlantis")
18+
#' @keywords datasets
19+
#'
20+
#' @source \url{https://github.com/NOAA-EDAB/neus-atlantis/wiki/Spatial-extent-of-model}
21+
NULL

R/data_amapps_grid.R

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
#' @title AMAPPS 10 km Grid
22
#'
3-
#' @description A \code{raster} object containing the Atlantic Marine Assessment Program for Protected Species (AMAPPS) 10 km grid.
3+
#' @description An \code{sf} object containing the Atlantic Marine Assessment Program for Protected Species (AMAPPS) 10 km grid.
44
#' The AMAPPS grid has a cell size of 10 km x 10 km, with an oblique Mercator projection.
55
#' Grid cell values represent grid cell IDs.
66
#'
7-
#' @format A \code{raster} object:
7+
#' @format An \code{sf} object:
88
#' \describe{
9-
#' \item{class}{RasterLayer}
10-
#' \item{dimensions}{328, 240, 78720 (nrow, ncol, ncell)}
11-
#' \item{resolution}{10000 m, 10000 m (x, y)}
12-
#' \item{extent}{-820443.2, 1579557, -1234728, 2045272 (xmin, xmax, ymin, ymax)}
13-
#' \item{crs}{+proj=omerc +lat_0=35 +lonc=-75 +alpha=40 +gamma=40 +k=0.9996 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs}
14-
#' \item{source}{data-raw/gis/AMAPPS_10km_grid.tif}
9+
#' \item{Geometry type}{POLYGON}
10+
#' \item{Dimension}{XY}
11+
#' \item{Bounding box}{xmin: -820443.2 ymin: -1234728 xmax: 1579557 ymax: 2045272}
12+
#' \item{Projected CRS}{unnamed}
13+
#' \item{Features}{78720 x 1}
14+
#' \item{Source}{data-raw/gis/AMAPPS_10km_grid.tif}
1515
#' }
16-
#'
16+
#'
1717
#' @docType data
1818
#' @name amapps_grid
1919
#' @usage data("amapps_grid")
2020
#' @keywords datasets
2121
#' @source \url{https://www.fisheries.noaa.gov/new-england-mid-atlantic/population-assessments/atlantic-marine-assessment-program-protected}
22+
#' @examples
23+
#' \dontrun{
24+
#' # load terra
25+
#' library(terra)
26+
#'
27+
#' # convert sf to SpatVector
28+
#' amapps_vec <- NEFSCspatial::amapps_grid |> as('SpatVector')
29+
#'
30+
#' # empty raster from amapps_vec
31+
#' amapps_ras <- rast(amapps_vec, nrow = 328, ncol = 240)
32+
#'
33+
#' # rasterize
34+
#' amapps <- rasterize(amapps_vec, amapps_ras, field = 'AMAPPS_10km_grid')
35+
#'
36+
#' # visualize
37+
#' plot(amapps)
38+
#' }
2239
NULL
40+

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# NEFSCspatial <img src="man/figures/logo.png" style="float:right; height:200px;" />
1+
# NEFSCspatial <img src="man/figures/logo.png" align="right" height="200" />
22

33
[![gitleaks](https://github.com/NEFSC/NEFSC-Spatial/actions/workflows/secretScan.yml/badge.svg)](https://github.com/NEFSC/NEFSC-Spatial/actions/workflows/secretScan.yml) [![gh-pages](https://github.com/NEFSC/NEFSC-Spatial/actions/workflows/pkgdown.yml/badge.svg)](https://github.com/NEFSC/NEFSC-Spatial/actions/workflows/pkgdown.yml)
44

5-
A repository of spatial data (vectors and rasters) in simple features [`sf`](https://r-spatial.github.io/sf/) or [`raster`](https://github.com/rspatial/raster) format.
5+
A repository of spatial data (vectors) in simple features [`sf`](https://r-spatial.github.io/sf/) format.
66

77
### Installation
88

data-raw/create_leaflet_maps.R

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# needed libraries
2+
library(NEFSCspatial)
3+
4+
# delete leaflet maps
5+
file.remove(list.files(here::here('pkgdown/leaflet_maps'), full.names = TRUE))
6+
7+
# delete leaflet *.Rmds
8+
leaflet_rmd <- here::here('vignettes/articles') |>
9+
list.files(full.names = TRUE)
10+
keep_rmd <- grep(pattern = "ContributeSpatialData|teamMembers", x = leaflet_rmd)
11+
leaflet_rmd <- leaflet_rmd[-keep_rmd]
12+
file.remove(leaflet_rmd)
13+
14+
# list of exported data objects
15+
exp_data <- ls("package:NEFSCspatial")
16+
17+
# ignore certain data sets, leaflet fails to render for some reason (maybe too big)
18+
exp_data <- exp_data[-which(exp_data %in% c("amapps_grid"))]
19+
20+
# loop through data and make leaflet maps
21+
for (i in exp_data) {
22+
23+
# load data
24+
data(list = i)
25+
26+
# get data
27+
sf_data <- get(i)
28+
29+
# make valid, if needed
30+
if (any(sf::st_is_valid(sf_data) != TRUE)) {
31+
sf_data <- sf_data |>
32+
sf::st_make_valid()
33+
}
34+
35+
# change crs, if needed
36+
if (sf::st_crs(sf_data) != sf::st_crs(4326)) {
37+
sf_data <- sf_data |>
38+
sf::st_transform(4326)
39+
}
40+
41+
# bounding box
42+
bbox <- sf_data |>
43+
sf::st_bbox() |>
44+
as.vector()
45+
46+
# leaflet map
47+
leaflet_map <- leaflet::leaflet(width = '100%') |>
48+
leaflet::addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") |>
49+
leaflet::fitBounds(lng1 = bbox[1], lat1 = bbox[2], lng2 = bbox[3], lat2 = bbox[4]) |>
50+
leaflet::addPolygons(data = sf_data, popup = i)
51+
52+
# save the map
53+
saveRDS(leaflet_map, here::here(paste0("pkgdown/leaflet_maps/", i, "_leaflet.rds")))
54+
55+
# write *.Rmd file
56+
sink(file = here::here(paste0("vignettes/articles/", i, "_leaflet.Rmd")))
57+
58+
# header
59+
cat("--- \n")
60+
cat(paste0("title: '", i, "' \n"))
61+
cat("output: html_document \n")
62+
cat("--- \n\n")
63+
64+
# load map
65+
cat("```{r, echo = FALSE, warning = FALSE} \n")
66+
cat("library(leaflet) \n")
67+
cat(paste0("m <- readRDS(here::here('pkgdown/leaflet_maps/", i, "_leaflet.rds')) \n"))
68+
cat("``` \n\n")
69+
70+
# map
71+
cat("```{r, echo = FALSE} \n")
72+
cat("m \n")
73+
cat("```")
74+
75+
sink()
76+
77+
}
78+
79+
# modify _pkgdown.yml
80+
yaml_file <- yaml::read_yaml(here::here('pkgdown/_pkgdown.yml'))
81+
82+
# add menu items
83+
mod_menu = lapply(X = exp_data, FUN = function(X) { list(text = X, href = paste0('articles/', X, '_leaflet.html')) })
84+
yaml_file$navbar$components$map$menu <- mod_menu
85+
86+
# output
87+
yaml::write_yaml(x = yaml_file, file = here::here('pkgdown/_pkgdown.yml'))

data-raw/create_sf_data_from_shp.R

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
#' Read in shape files as sf object and save as rda
2-
#'
31
#' All shapefiles are read in as sf objects then saved as rda objects in the data folder.
42
#' (To use these files locally the package will need to be rebuilt Ctrl+Shift+B)
53
#' The saved rda object will use the name of the given shapefile
64
#'
75
#' @param filePath Character string. Full path to file (eg. here::here("data-raw/gis/shapefile.shp"))
86
#' @param overwite Boolean. Export to "data" folder (Default = F)
7+
#' @param addCentroids Boolean. Calculate and add centroids (X,Y fields) to shapefile
8+
#' @param shapefilenm Character string. Custom name for sf object. Default = shapefile name
99
#'
1010
#' @return creates sf object in data folder. Package then needs to be rebuilt (Ctrl+shift+B)
1111
#'
1212

1313
library(magrittr)
14-
create_sf_data_from_shp <- function(filePath,overwrite=F) {
14+
create_sf_data_from_shp <- function(filePath,overwrite=F,addCentroids=T,shapefilenm=NULL) {
1515

1616
# turn off spherical geometry
1717
sf::sf_use_s2(F)
1818

1919
# get filename
20-
fileName <- gsub(".shp$","",filePath)
21-
fileName <- tail(unlist(strsplit(fileName,"/")),1)
20+
#overwrite filename with custom name
21+
if(!is.null(shapefilenm)) {
22+
fileName <- shapefilenm
23+
} else {
24+
fileName <- gsub(".shp$","",filePath)
25+
fileName <- tail(unlist(strsplit(fileName,"/")),1)
26+
}
2227
fn <- fileName
2328

2429
# read in shapefile
2530
layer <- sf::st_read(dsn=filePath)
2631
# add XY locations for center of each polygon
27-
centroids <- sf::st_coordinates(sf::st_centroid(layer))
28-
layer <- cbind(layer,centroids)
29-
32+
if(addCentroids) {
33+
centroids <- sf::st_coordinates(sf::st_centroid(layer))
34+
layer <- cbind(layer,centroids)
35+
}
3036
# assigns name to layer object
3137
assign(fileName,layer)
3238

39+
40+
3341
print(here::here("data",paste0(fn,".rdata")))
3442
# exports
3543
if(overwrite) {
3644
save(list=fileName,file=here::here("data",paste0(fn,".rda")))
45+
3746
}
3847

3948
#do.call(myfun, list(as.name(fileName), overwrite = TRUE))

data-raw/get_amapps_grid_raster.R

Lines changed: 0 additions & 30 deletions
This file was deleted.

data-raw/get_amapps_grid_sf.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' Read in GeoTIFF of AMAPPS 10 km grid, convert to an sf object, and save as an rda file.
2+
#'
3+
#' @param save_clean Boolean. TRUE / FALSE to save data as an rda file or return \code{sf} object.
4+
#'
5+
#' @return An \code{sf} object if \code{save_clean = FALSE}, otherwise \code{NULL}.
6+
#'
7+
8+
# function
9+
get_amapps_grid_sf <- function(save_clean = TRUE) {
10+
11+
# read in .tif file
12+
amapps_grid <- here::here('data-raw/gis/AMAPPS_10km_grid.tif') |>
13+
terra::rast() |>
14+
terra::as.polygons() |>
15+
sf::st_as_sf()
16+
17+
# save or not
18+
if (save_clean) {
19+
usethis::use_data(amapps_grid, overwrite = TRUE)
20+
} else {
21+
return(amapps_grid)
22+
}
23+
24+
}
25+
26+
# run
27+
get_amapps_grid_sf()

0 commit comments

Comments
 (0)