diff --git a/.github/PULL_REQUEST_TEMPLATE/dev_pr.md b/.github/PULL_REQUEST_TEMPLATE/dev_pr.md new file mode 100644 index 0000000..7a1155a --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE/dev_pr.md @@ -0,0 +1,25 @@ +PRs into `dev` contained all necessary info. They have already been approved. +Please provide the information below so `main` can be synced with `dev`: + +### List PRs + +List the PRs that were merged into `dev` that will be pulled into `main` and any issues they fix: + +* PR #? which fixes #? + +### Types of changes + +What types of changes does this pull request introduce? Put an `x` in the boxes that apply. +This will inform the new release number. + +- [ ] Fix (non-breaking change which fixes a bug) +- [ ] Feature (non-breaking change which adds or changes functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] Other change (if none of the other choices apply) + +### Reviewer instructions + +The underlying PRs should have been reviewed. Please check that the proposed merge is correct before approving. +If any checkboxes other than "Other change" have been selected please prepare the following file changes for a new release version: +- [ ] NEWS.md - add release notes +- [ ] DESCRIPTION - increment the version number according to [Semantic Versioning](https://semver.org) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..fae769f --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,36 @@ +Your commits explain the `who`, `what`, `where` and `when` of these changes. Your code shows the `how`. You do not need to reiterate this. This PR should complete the picture by explaining `why` these changes are necessary. +Please complete the fields below, replacing or removing placeholder text where necessary: + +### Justification + +Please describe the importance/impact of the problem and a description of how the changes in this pull request will address, resolve or improve it. This section should sufficiently explain to reviewers why these changes should be approved. +If the proposed changes should close an issue, be sure to link to it below: +Fixes #? + +### Types of changes + +What types of changes does this pull request introduce? Put an `x` in the boxes that apply. +This will inform the new release number. + +- [ ] Fix (non-breaking change which fixes a bug) +- [ ] Feature (non-breaking change which adds or changes functionality) +- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) +- [ ] Other change (if none of the other choices apply) + +### Further comments + +If this is a relatively large or complex change, kick off a discussion by explaining why you chose the solution you did, what alternatives you considered, problems you may have encountered, etc. + +### Reviewer instructions + +Assign at least 2 reviewers: +* one of which should be a maintainer of this repo, +* the others should be familiar with the subject or problem. + +Please detail the process reviewers will need to follow to properly test these changes. It is especially helpful to identify the exact code needed to conduct a local build/review/test and to specify any aspects of the testing that may not be obvious. + +### Formatting + +This repo contains an `air.toml` file that automatically formats code to a set of standards. +It is preferred that contributors and reviewers install the [Air](https://posit-dev.github.io/air/) formatting tool. +Code submitted in this pull request will be automatically checked for correct formatting. diff --git a/.github/workflows/format-check.yml b/.github/workflows/format-check.yml new file mode 100644 index 0000000..9ba132d --- /dev/null +++ b/.github/workflows/format-check.yml @@ -0,0 +1,24 @@ +# Workflow derived from https://github.com/posit-dev/setup-air/tree/main/examples + +on: + push: + branches: [main, dev] + pull_request: + workflow_dispatch: + +name: format-check.yaml + +permissions: read-all + +jobs: + format-check: + name: format-check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + + - name: Install + uses: posit-dev/setup-air@v1 + + - name: Check + run: air format . --check \ No newline at end of file diff --git a/.github/workflows/format-suggest.yml b/.github/workflows/format-suggest.yml new file mode 100644 index 0000000..63f7c02 --- /dev/null +++ b/.github/workflows/format-suggest.yml @@ -0,0 +1,31 @@ +# Workflow derived from https://github.com/posit-dev/setup-air/tree/main/examples + +on: + pull_request: + +name: format-suggest.yaml + +jobs: + format-suggest: + name: format-suggest + runs-on: ubuntu-latest + + permissions: + # Required to push suggestion comments to the PR + pull-requests: write + + steps: + - uses: actions/checkout@v6 + + - name: Install + uses: posit-dev/setup-air@v1 + + - name: Format + run: air format . + + - name: Suggest + uses: reviewdog/action-suggester@v1 + with: + level: error + fail_level: error + tool_name: air diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..e731ba0 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,68 @@ +# NOAA Fisheries Open Science Code of Conduct + +This code of conduct was developed and adapted from the Atom code of conduct in October 2021, +and revised from NOAA-NMFS-Brand-Resources in June 2024 + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported +by contacting the project team. All complaints will be reviewed and investigated +and will result in a response that is deemed necessary and appropriate to the +circumstances. Further details of specific enforcement policies may be posted +separately. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/), version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..41f9051 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,161 @@ +# Contributing Guidelines + +*Pull requests, bug reports, and all other forms of contribution are welcomed and highly encouraged!* + +### Contents + +- [Code of Conduct](#book-code-of-conduct) +- [Asking Questions](#paperclip-asking-questions) +- [Acceptable Types](#whale-acceptable-types) +- [Opening an Issue](#fishing_pole_and_fish-opening-an-issue) +- [Bug Reports and Other Issues](#blowfish-bug-reports-and-other-issues) +- [Feature Requests](#tropical_fish-feature-requests) +- [Creating a Branch](#anchor-creating-a-branch) +- [Writing Commit Messages](#memo-writing-commit-messages) +- [Submitting Pull Requests](#fish_cake-submitting-pull-requests) +- [Coding Style](#shark-coding-style) +- [Certificate of Origin](#crab-certificate-of-origin) +- [Credits](#pray-credits) + +> **This guide serves to set clear expectations for everyone involved with the project so that we can improve it together while also creating a welcoming space for everyone to participate. Following these guidelines will help ensure a positive experience for contributors and maintainers.** + +## :book: Code of Conduct + +Please review our [Code of Conduct](CODE_OF_CONDUCT.md). It is in effect at all times. We expect it to be honored by everyone who contributes to this project. + +## :paperclip: Asking Questions + +GitHub issues are not the appropriate place to debug your specific project, but should be reserved for filing bugs and feature requests. + +## :whale: Acceptable Types + +Throughout this project, we use standard `types` to communicate more effectively. These types are used when labeling issues, creating branches, committing changes and naming pull requests. Please reference and use the `types` below when contributing to this project: + +- **fix:** Patch an unexpected or undesirable behavior + +- **build:** Add or modify build-related components (e.g. workflows, versioning, etc.) + +- **chore:** Maintenance of product or repository (e.g. gitignore, templates, etc.) + +- **docs:** Add or modify documentation + +- **feature:** Add or modify a feature or functionality + +- **refactor:** Rewrite or restructure code without altering behavior + +- **style:** Reformat code style without altering behavior + +- **test:** Add or modify tests + +All issues, branches, commits and pull requests should align with one of the above `types`. If you have questions or need assistance, please reach out to a maintainer. + +## :fishing_pole_and_fish: Opening an Issue + +Before [creating an issue](https://help.github.com/en/github/managing-your-work-on-github/creating-an-issue), check that you are using the latest version of the project. If you are not up-to-date, see if updating fixes your issue first. + +### :blowfish: Bug Reports and Other Issues + +A great way to contribute to the project is to send a detailed issue when you encounter a problem. +This repo has a 'Bug Report' issue template that will guide you in submitting a well-written, thorough bug report. If possible, please also create a [reprex](https://reprex.tidyverse.org/) and include it in your issue. This helps us quickly identify and fix the problem. + +When opening an issue, please follow these guidelines: + +- **Review the documentation** before opening a new issue. + +- **Be specific.** Describe the problem in detail. What did you expect to happen? What actually happened? What were you doing when the problem occurred? What version of the library are you using? What version of the OS are you running? + +- **Provide a reproducible example (e.g. [reprex](https://reprex.tidyverse.org/))** If possible, provide a minimal, complete, and verifiable example that reproduces the issue. This is often the most important part of a bug report. If you can provide a sample project that reproduces the issue, that is even better! + +- **Prefer using [reactions](https://github.blog/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/)**, not comments, if you simply want to "+1" an existing issue. + +- **Use [GitHub-flavored Markdown](https://help.github.com/en/github/writing-on-github/basic-writing-and-formatting-syntax).** Especially put code blocks and console outputs in backticks (```). This improves readability. In short, since you are most likely a developer, **provide a ticket that you would like to receive**. + +- **Do not open a duplicate issue!** Search through existing issues to see if your issue has previously been reported. If your issue exists, comment with any additional information you have. You may simply leave a reaction to bump the issue, which helps prioritize the most common problems and requests. + +- **Fully complete the provided issue template.** The bug report template requests all the information we need to quickly and efficiently address your issue. Be clear, concise, and descriptive. Provide as much information as you can, including steps to reproduce, stack traces, compiler errors, library versions, OS versions, and screenshots (if applicable). + +### :tropical_fish: Feature Requests + +Feature requests are more than welcome! While we will consider all requests, we cannot guarantee your request will be accepted or provide the timeline for implementation and release. + +- **Do not open a duplicate feature request.** Search for existing feature requests first. If you find your feature (or one very similar) previously requested, comment on or add a reaction to that issue. + +- **Fully complete the provided issue template.** The 'Feature Request' template asks for all necessary information for us to begin a productive conversation. + +- **Be precise** about the proposed outcome of the feature and how it relates to existing features. Include all implementation details. + +## :anchor: Creating a Branch + +Many contributions to this project will make use of [branching](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-branches). This repo has two permanent branches: `main` and `dev`. To contribute via branching, you should create a branch from and pull request to the `dev` branch. Your branch name should follow these conventions: + +- Use `type/i#-describe-branch-purpose`, where # is the number of the target issue +- Prefix with the `type` of change the branch will introduce (see [acceptable `types`](#whale-acceptable-types) above) +- Include an issue number in your branch name (your changes should relate to an [existing issue](#fishing_pole_and_fish-opening-an-issue)) +- Provide a clear but concise description of the branch's purpose + +## :memo: Writing Commit Messages + +Please write a **conventional** commit message: + +1. Use `type(optional scope): commit message` structure (see above for [acceptable `types`](#whale-acceptable-types)) +1. Separate changes into their own commits with unique messages +1. Write using imperative mood (example: "fix: correct the food web") +1. Include a `scope` if a commit serves a larger purpose (example: "refactor(food web fix): reorder data ingestion") +1. Be as brief as possible without losing clarity + +## :fish_cake: Submitting Pull Requests + +We appreciate pull requests! Before [forking the repo](https://help.github.com/en/github/getting-started-with-github/fork-a-repo) and [creating a pull request](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/proposing-changes-to-your-work-with-pull-requests) for non-trivial changes, it is usually best to first open an issue to discuss the changes, or discuss your intended approach for solving the problem in the comments for an existing issue. + +*Note: All contributions will be licensed under the project's license.* + +**Guidelines for happy pull requests:** + +- **Title meaningfully.** Consistent with the convention elsewhere, please title your pull request: "Type(#i): Describe Change Purpose", where "i" is the number of the issue addressed by the PR. Please reference the [acceptable types](#whale-acceptable-types) above. + +- **Request appropriate reviewers.** At the very least, please request review from one or all of the project maintainers (maintainers are listed on the [README](README.md)). You should include additional reviews from people familiar with the subject or problem your proposed changes address. + +- **Communication is the key to success.** If you are unsure about something, ask! We are happy to help. We have an open channel of communication, make sure to reach out and further develop your ideas or changes before working on a pull request. + +- **Smaller is better.** Submit **one** pull request per bug fix or feature. A pull request should contain isolated changes pertaining to a single bug fix or feature implementation. **Do not** refactor or reformat code that is unrelated to your change. It is better to **submit many small pull requests** rather than a single large one. Enormous pull requests will take enormous amounts of time to review, or may be rejected altogether. + +- **Coordinate bigger changes.** For large and non-trivial changes, open an issue to discuss a strategy with the maintainers. Otherwise, you risk doing a lot of work for nothing! + +- **Prioritize understanding over cleverness.** Write code **clearly** and **concisely**, please supply comments when it is needed. Remember that source code usually gets written once and read often. Ensure the code is clear to the reader. The purpose and logic should be obvious to a reasonably skilled developer, otherwise you should add a comment that explains it. + +- **Follow the existing architecture.** If you are adding new functionality, try to follow the existing architecture and patterns in the code base. If you are unsure, ask for guidance. + +- **Include test coverage.** Add unit tests or UI tests when possible. Follow existing patterns for implementing tests. + +- **Update the example project** if one exists to exercise any new functionality you have added. + +- **Add documentation.** Document your changes with code comments or other documentation. + +- **Use the correct branch.** Branch from and [submit your pull request](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request-from-a-fork) to the correct branch (see above for [branching instructions](#anchor-creating-a-branch)). In this repo, it will be the `dev` branch. + +- **[Resolve any merge conflicts](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-on-github)** that occur. + +## :shark: Coding Style + +Consistent formatting is an essential part of effective collaboration and communication. Reading, writing and reviewing code all benefit from following the standards and conventions of a project. Failure to do so will result in a prolonged review process that has to focus on updating the superficial aspects of your code, rather than improving its functionality and performance. This repo uses and adheres to the [tidyverse style guide](https://style.tidyverse.org/) and the [Air autoformatting tool](https://posit-dev.github.io/air/) that follows it. Please read and follow these guides as you make contributions to the project. + +## :crab: Certificate of Origin + +*Developer's Certificate of Origin 1.1* + +By making a contribution to this project, I certify that: + +> 1. The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or +> 1. The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or +> 1. The contribution was provided directly to me by some other person who certified (1), (2) or (3) and I have not modified it. +> 1. I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. + +## :fish: Thank You! + +If you are reading this, thank you! We appreciate your interest in contributing to this project. + +To confirm that you have read this guide and are following it as best as possible, **include this emoji at the top** of your issue or pull request: :fish: `:fish:` + +## :pray: Credits + +This document was inspired by [@jessesquires](https://github.com/jessesquires). diff --git a/R/data_amapps_grid.R b/R/data_amapps_grid.R index bd9b2a3..33a6c3e 100644 --- a/R/data_amapps_grid.R +++ b/R/data_amapps_grid.R @@ -1,7 +1,7 @@ #' @title AMAPPS 10 km Grid #' -#' @description An \code{sf} object containing the Atlantic Marine Assessment Program for Protected Species (AMAPPS) 10 km grid. -#' The AMAPPS grid has a cell size of 10 km x 10 km, with an oblique Mercator projection. +#' @description An \code{sf} object containing the Atlantic Marine Assessment Program for Protected Species (AMAPPS) 10 km grid. +#' The AMAPPS grid has a cell size of 10 km x 10 km, with an oblique Mercator projection. #' Grid cell values represent grid cell IDs. #' #' @format An \code{sf} object: @@ -23,18 +23,17 @@ #' \dontrun{ #' # load terra #' library(terra) -#' +#' #' # convert sf to SpatVector -#' amapps_vec <- NEFSCspatial::amapps_grid |> as('SpatVector') -#' +#' amapps_vec <- NEFSCspatial::amapps_grid |> as('SpatVector') +#' #' # empty raster from amapps_vec #' amapps_ras <- rast(amapps_vec, nrow = 328, ncol = 240) -#' +#' #' # rasterize #' amapps <- rasterize(amapps_vec, amapps_ras, field = 'AMAPPS_10km_grid') -#' +#' #' # visualize #' plot(amapps) #' } NULL - diff --git a/air.toml b/air.toml new file mode 100644 index 0000000..e69de29 diff --git a/data-raw/create_hexsticker.R b/data-raw/create_hexsticker.R index e234b82..4bf2acb 100644 --- a/data-raw/create_hexsticker.R +++ b/data-raw/create_hexsticker.R @@ -4,40 +4,57 @@ library(hexSticker) library(ggimage) ### Option 1 -img1<- ggimage::image_read2(here::here("data-raw/images/hexstickerV1_ppt.PNG")) -res1<- img1 %>% +img1 <- ggimage::image_read2(here::here("data-raw/images/hexstickerV1_ppt.PNG")) +res1 <- img1 %>% #image_convert("png") %>% - magick::image_resize("1080 x 600")#%>% - #magick::image_fill(color="#062047", point="+45") %>% - #magick::image_annotate("NEFSCspatial", size=38, location = "-10+100", color="black") + magick::image_resize("1080 x 600") #%>% +#magick::image_fill(color="#062047", point="+45") %>% +#magick::image_annotate("NEFSCspatial", size=38, location = "-10+100", color="black") -res_final1<- hexSticker::sticker(res1, package = "NEFSCspatial", p_size = 16, - p_y = 0.45, p_color = "#003087", - s_x = 1, s_y=1.2, s_width = 1.5, - s_height = 14, h_fill = "white", h_color="#003087", - u_x = 0.18, - u_y=1.47, - url = "nefsc.github.io/NEFSCspatial", - u_size = 5.8, - filename = here::here("man/NEFSCspatial_hexsticker_v1.png")) - #filename = here::here("man/figures/logo.png")) +res_final1 <- hexSticker::sticker( + res1, + package = "NEFSCspatial", + p_size = 16, + p_y = 0.45, + p_color = "#003087", + s_x = 1, + s_y = 1.2, + s_width = 1.5, + s_height = 14, + h_fill = "white", + h_color = "#003087", + u_x = 0.18, + u_y = 1.47, + url = "nefsc.github.io/NEFSCspatial", + u_size = 5.8, + filename = here::here("man/NEFSCspatial_hexsticker_v1.png") +) +#filename = here::here("man/figures/logo.png")) ### Option 2 -img2<- ggimage::image_read2(here::here("data-raw/images/hexstickerV2_ppt.PNG")) -res2<- img2 %>% +img2 <- ggimage::image_read2(here::here("data-raw/images/hexstickerV2_ppt.PNG")) +res2 <- img2 %>% #image_convert("png") %>% - magick::image_resize("1080 x 600")#%>% + magick::image_resize("1080 x 600") #%>% #magick::image_fill(color="#062047", point="+45") %>% #magick::image_annotate("NEFSCspatial", size=38, location = "-10+100", color="black") -res_final2<- hexSticker::sticker(res2, package = "NEFSCspatial", p_size = 16, - p_y = 0.65, p_color = "#003087", - s_x = 0.95, s_y=1.05, s_width = 1.2, - s_height = 12, h_fill = "white", h_color="#003087", - u_x = 1.02, - u_y=.07, - url = "github.io/NEFSCspatial", - u_size = 5.8, - filename = here::here("data-raw/NEFSCspatial_hexsticker_v2.png")) - #filename = here::here("man/figures/logo.png")) - +res_final2 <- hexSticker::sticker( + res2, + package = "NEFSCspatial", + p_size = 16, + p_y = 0.65, + p_color = "#003087", + s_x = 0.95, + s_y = 1.05, + s_width = 1.2, + s_height = 12, + h_fill = "white", + h_color = "#003087", + u_x = 1.02, + u_y = .07, + url = "github.io/NEFSCspatial", + u_size = 5.8, + filename = here::here("data-raw/NEFSCspatial_hexsticker_v2.png") +) +#filename = here::here("man/figures/logo.png")) diff --git a/data-raw/create_sf_data_from_ftp.R b/data-raw/create_sf_data_from_ftp.R index 362986d..9bd177b 100644 --- a/data-raw/create_sf_data_from_ftp.R +++ b/data-raw/create_sf_data_from_ftp.R @@ -15,13 +15,16 @@ #' library(magrittr) -source(here::here("data-raw","list_files_ftp.r")) +source(here::here("data-raw", "list_files_ftp.r")) # list files found on ftp site ftpsite <- "ftp://ftp.nefsc.noaa.gov/pub/gis" #fileList <- NEFSCspatial::list_files_ftp(ftpsite) -create_sf_data_from_ftp <- function(ftpsite,file = NULL,tempDir=here::here("data-raw/temp")) { - +create_sf_data_from_ftp <- function( + ftpsite, + file = NULL, + tempDir = here::here("data-raw/temp") +) { # create temp dir if not already present if (!dir.exists(tempDir)) { dir.create(tempDir) @@ -37,43 +40,39 @@ create_sf_data_from_ftp <- function(ftpsite,file = NULL,tempDir=here::here("data allFiles <- list_files_ftp(ftpsite) # filter shape files - shpFiles <- grep("shp$",allFiles,value=T) + shpFiles <- grep("shp$", allFiles, value = T) if (is.null(file)) { # process all files } else { - isPresent <- grepl(file,shpFiles) + isPresent <- grepl(file, shpFiles) if (any(isPresent)) { # process single file shpFiles <- shpFiles[isPresent] } else { - stop(paste0("Shapefile: ",file, " doesnt seem to be on the ftp server")) + stop(paste0("Shapefile: ", file, " doesnt seem to be on the ftp server")) } } - # now create sf object for each shpfile # first find all files associated with shapefile, download, then make sf - for (afile in shpFiles){ - shp <- tail(unlist(strsplit(afile,split = "\\/")),1) - fstr <- gsub(".shp","",shp) - message(paste0("Processing files related to = ",shp)) + for (afile in shpFiles) { + shp <- tail(unlist(strsplit(afile, split = "\\/")), 1) + fstr <- gsub(".shp", "", shp) + message(paste0("Processing files related to = ", shp)) # select all files to download for this shapefile - filesToDownload <- allFiles[grepl(fstr,allFiles)] + filesToDownload <- allFiles[grepl(fstr, allFiles)] for (ashp in filesToDownload) { - ashpfile <- gsub(paste0(ftpsite,"/"),"",ashp) - curl::curl_download(ashp,destfile = paste0(tempDir,"/",ashpfile)) + ashpfile <- gsub(paste0(ftpsite, "/"), "", ashp) + curl::curl_download(ashp, destfile = paste0(tempDir, "/", ashpfile)) } - layer <- sf::st_read(dsn=paste0(tempDir,"/",shp)) + layer <- sf::st_read(dsn = paste0(tempDir, "/", shp)) - centroids <- sf::st_coordinates(sf::st_centroid(layer)) - layer <- cbind(layer,centroids) + centroids <- sf::st_coordinates(sf::st_centroid(layer)) + layer <- cbind(layer, centroids) - assign(fstr,layer) + assign(fstr, layer) do.call(myfun, list(as.name(fstr), overwrite = TRUE)) - } - - } diff --git a/data-raw/create_sf_data_from_url.R b/data-raw/create_sf_data_from_url.R index 27ce504..91b20d6 100644 --- a/data-raw/create_sf_data_from_url.R +++ b/data-raw/create_sf_data_from_url.R @@ -18,62 +18,61 @@ library(magrittr) ##options(warn=0) create_sf_data_from_url <- function(url) { - # set up function for use in do.call myfun <- get("use_data", asNamespace("usethis")) - # get zip file, catch error for missing file - result <- tryCatch( - { - message("Grabbing file: from ",url) - temp <- base::tempfile() - download.file(url=url,destfile=temp, quiet=TRUE) - res <- TRUE - }, - error = function(e){ - message(e) - return(FALSE) - } , - warning = function(w){ - return(FALSE) - } - ) - - if (!result) { # failed to download file - message(paste0("File ",url, " can not be downloaded. Please check the url and try again")) - base::unlink(temp) - next - } - - - - # find names of all files in zip file - filesInZip <- unzip(temp,list=T) - temp2 <- base::tempfile() - #unzip all files to a temporary directory - unzip(zipfile = temp, exdir = temp2) - - # pick out all .shp files and their dir struct - for (ifile in 1:nrow(filesInZip)) { - afile <- filesInZip$Name[ifile] - if (grepl("shp$",afile)) { - ## read in shp file , create sf object, and export to data folder - message(paste0("Shapefile = ",afile)) - exportName <- tail(unlist(strsplit(afile,"/")),1) - exportName <- head(unlist(strsplit(exportName,"\\.")),1) - ## replace any hyphens with underscores, hyphens can cause issues - exportName <- gsub('-', '_',exportName) - layer <- sf::st_read(dsn=paste0(temp2,"/",afile),quiet=T) - centroids <- sf::st_coordinates(sf::st_centroid(layer)) - layer <- cbind(layer,centroids) - assign(exportName,layer) - do.call(myfun, list(as.name(exportName), overwrite = TRUE)) - } + # get zip file, catch error for missing file + result <- tryCatch( + { + message("Grabbing file: from ", url) + temp <- base::tempfile() + download.file(url = url, destfile = temp, quiet = TRUE) + res <- TRUE + }, + error = function(e) { + message(e) + return(FALSE) + }, + warning = function(w) { + return(FALSE) + } + ) - } + if (!result) { + # failed to download file + message(paste0( + "File ", + url, + " can not be downloaded. Please check the url and try again" + )) + base::unlink(temp) + next + } - base::unlink(temp) - base::unlink(temp2) + # find names of all files in zip file + filesInZip <- unzip(temp, list = T) + temp2 <- base::tempfile() + #unzip all files to a temporary directory + unzip(zipfile = temp, exdir = temp2) + # pick out all .shp files and their dir struct + for (ifile in 1:nrow(filesInZip)) { + afile <- filesInZip$Name[ifile] + if (grepl("shp$", afile)) { + ## read in shp file , create sf object, and export to data folder + message(paste0("Shapefile = ", afile)) + exportName <- tail(unlist(strsplit(afile, "/")), 1) + exportName <- head(unlist(strsplit(exportName, "\\.")), 1) + ## replace any hyphens with underscores, hyphens can cause issues + exportName <- gsub('-', '_', exportName) + layer <- sf::st_read(dsn = paste0(temp2, "/", afile), quiet = T) + centroids <- sf::st_coordinates(sf::st_centroid(layer)) + layer <- cbind(layer, centroids) + assign(exportName, layer) + do.call(myfun, list(as.name(exportName), overwrite = TRUE)) + } + } + base::unlink(temp) + base::unlink(temp2) } diff --git a/data-raw/find_mdeb_data.R b/data-raw/find_mdeb_data.R index 4e3292e..7abe795 100644 --- a/data-raw/find_mdeb_data.R +++ b/data-raw/find_mdeb_data.R @@ -20,7 +20,7 @@ find_mdeb_data <- function() { # url of the MDEB GIS Data Hub search API - mdeb_hub = "https://mdeb-nefsc-noaa.hub.arcgis.com/api/search/v1/collections/all/items" + mdeb_hub <- "https://mdeb-nefsc-noaa.hub.arcgis.com/api/search/v1/collections/all/items" # scrape json to get all feature services catalog_items <- mdeb_hub |> jsonlite::fromJSON() @@ -46,7 +46,7 @@ find_mdeb_data <- function() { wp <- dataset_json_url |> jsonlite::fromJSON() # create tibble for output - av = wp$layers |> + av <- wp$layers |> dplyr::mutate(dataset_url) |> dplyr::mutate(url = paste0(url, '/', id)) |> dplyr::select(title, name, id, url) |> diff --git a/data-raw/generate_roxygen_mdeb.R b/data-raw/generate_roxygen_mdeb.R index a68e179..174cea6 100644 --- a/data-raw/generate_roxygen_mdeb.R +++ b/data-raw/generate_roxygen_mdeb.R @@ -1,34 +1,37 @@ # function to generate a consistent Roxygen file for data documentation generate_roxygen_mdeb <- function(dataset_name, title, description) { - # check that dataset_name has an *.rda ext stopifnot(endsWith(dataset_name, '.rda')) - + # load data load(here::here('data', dataset_name)) - + # re-assign mdeb_obj <- dataset_name |> tools::file_path_sans_ext() |> get() - + # extract metadata - geometry_type <- sf::st_geometry_type(mdeb_obj) |> - unique() |> + geometry_type <- sf::st_geometry_type(mdeb_obj) |> + unique() |> as.character() dimension <- sf::st_dimension(mdeb_obj) |> - unique() + unique() dimension_type <- ifelse(dimension == 2, 'XY', NA_character_) bbox <- sf::st_bbox(mdeb_obj) projection <- sf::st_crs(mdeb_obj, parameters = TRUE)$Name num_features <- nrow(mdeb_obj) num_columns <- ncol(mdeb_obj) - + # build the file path - file_path <- file.path("R", paste0("data-", tools::file_path_sans_ext(dataset_name), ".R")) - + file_path <- file.path( + "R", + paste0("data-", tools::file_path_sans_ext(dataset_name), ".R") + ) + # substitute values into the template - roxygen_content <- glue::glue(" + roxygen_content <- glue::glue( + " #' { title } #' #' @description { description } @@ -57,10 +60,12 @@ generate_roxygen_mdeb <- function(dataset_name, title, description) { #' plot(sf::st_geometry({ tools::file_path_sans_ext(dataset_name) })) #' }} NULL -") - +" + ) + # write content to file writeLines(roxygen_content, con = file_path) - message(glue::glue("Generated Roxygen file for '{ tools::file_path_sans_ext(dataset_name) }' at: { file_path }")) - + message(glue::glue( + "Generated Roxygen file for '{ tools::file_path_sans_ext(dataset_name) }' at: { file_path }" + )) } diff --git a/data-raw/get_amapps_grid_sf.R b/data-raw/get_amapps_grid_sf.R index 233d013..c81227e 100644 --- a/data-raw/get_amapps_grid_sf.R +++ b/data-raw/get_amapps_grid_sf.R @@ -7,20 +7,18 @@ # function get_amapps_grid_sf <- function(save_clean = TRUE) { - # read in .tif file amapps_grid <- here::here('data-raw/gis/AMAPPS_10km_grid.tif') |> terra::rast() |> terra::as.polygons() |> sf::st_as_sf() - + # save or not if (save_clean) { usethis::use_data(amapps_grid, overwrite = TRUE) } else { return(amapps_grid) } - } # run diff --git a/data-raw/get_epu_sf.R b/data-raw/get_epu_sf.R index 48e4220..73f682a 100644 --- a/data-raw/get_epu_sf.R +++ b/data-raw/get_epu_sf.R @@ -7,17 +7,20 @@ library(sf) library(rgdal) -gis.dir <- here::here('data-raw','gis') +gis.dir <- here::here('data-raw', 'gis') -get_epu_sf <- function(save_clean){ +get_epu_sf <- function(save_clean) { crs <- "+proj=longlat +lat_1=35 +lat_2=45 +lat_0=40 +lon_0=-77+x_0=0 +y_0=0 +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0" - epu_shp <- sf::st_as_sf(rgdal::readOGR(file.path(gis.dir, "EPU_Extended.shp"), verbose = F)) + epu_shp <- sf::st_as_sf(rgdal::readOGR( + file.path(gis.dir, "EPU_Extended.shp"), + verbose = F + )) sf::st_crs(epu_shp) <- crs epu_sf <- as(epu_shp, "sf") - - if (save_clean){ + + if (save_clean) { usethis::use_data(epu_sf, overwrite = TRUE) } else { return(epu_sf) @@ -25,4 +28,3 @@ get_epu_sf <- function(save_clean){ } get_epu_sf(save_clean = TRUE) - diff --git a/data-raw/get_mdeb_data.R b/data-raw/get_mdeb_data.R index 0ba3173..5105966 100644 --- a/data-raw/get_mdeb_data.R +++ b/data-raw/get_mdeb_data.R @@ -49,7 +49,7 @@ get_mdeb_data <- function(dataset_name = NULL, dataset_id = NULL) { httr2::req_url_query(where = "1=1", outFields = "*", f = "geoJSON") |> httr2::req_perform() if (dataset$status_code == 200) { - dataset_sf = dataset |> + dataset_sf <- dataset |> httr2::resp_body_string() |> sf::read_sf() return(dataset_sf) diff --git a/data-raw/get_nmfs_remote_sf.R b/data-raw/get_nmfs_remote_sf.R index 4a87d36..b62bbe5 100644 --- a/data-raw/get_nmfs_remote_sf.R +++ b/data-raw/get_nmfs_remote_sf.R @@ -3,5 +3,9 @@ # Download and process spatial data from NOAA Fisheries (NMFS) websites (New England/Mid-Atlantic) # download, process, and save -create_sf_data_from_url(url = 'https://media.fisheries.noaa.gov/2022-08/LMA1_Restricted_Area_FR_2.zip') -create_sf_data_from_url(url = 'https://media.fisheries.noaa.gov/2022-08/Great_South_Channel_Restricted_Trap-Pot_Area_2.zip') +create_sf_data_from_url( + url = 'https://media.fisheries.noaa.gov/2022-08/LMA1_Restricted_Area_FR_2.zip' +) +create_sf_data_from_url( + url = 'https://media.fisheries.noaa.gov/2022-08/Great_South_Channel_Restricted_Trap-Pot_Area_2.zip' +) diff --git a/data-raw/list_files_ftp.R b/data-raw/list_files_ftp.R index 3c694dd..092f523 100644 --- a/data-raw/list_files_ftp.R +++ b/data-raw/list_files_ftp.R @@ -23,14 +23,11 @@ #' #' @export #' -list_files_ftp <- function(url, - sort = FALSE, - verbose = FALSE) { - +list_files_ftp <- function(url, sort = FALSE, verbose = FALSE) { # Do x <- url %>% purrr::map( - ~list_files_ftp_worker( + ~ list_files_ftp_worker( url = ., credentials = "", sleep = NA, @@ -40,17 +37,19 @@ list_files_ftp <- function(url, purrr::flatten_chr() # Sort remote file names - if (sort) x <- sort(x) + if (sort) { + x <- sort(x) + } return(x) } - list_files_ftp_worker <- function(url, credentials, sleep, verbose) { - # Message to user - if (verbose) message(date_message(), "`", url, "`...") + if (verbose) { + message(date_message(), "`", url, "`...") + } # url must be prefixed with ftp or sftp if (!grepl("^ftp://|^sftp://", url)) { @@ -62,20 +61,21 @@ list_files_ftp_worker <- function(url, credentials, sleep, verbose) { # Get the file list # If credentials are blank, this will still work - file_list <- tryCatch({ - - RCurl::getURL( - url, - userpwd = credentials, - ftp.use.epsv = FALSE, - dirlistonly = TRUE, - forbid.reuse = TRUE, - .encoding = "UTF-8" - ) - - }, error = function(e) { - as.character() - }) + file_list <- tryCatch( + { + RCurl::getURL( + url, + userpwd = credentials, + ftp.use.epsv = FALSE, + dirlistonly = TRUE, + forbid.reuse = TRUE, + .encoding = "UTF-8" + ) + }, + error = function(e) { + as.character() + } + ) # Make a vector if (length(file_list) != 0) { @@ -83,8 +83,9 @@ list_files_ftp_worker <- function(url, credentials, sleep, verbose) { file_list <- stringr::str_trim(file_list) } - if (!is.na(sleep[1])) Sys.sleep(sleep) + if (!is.na(sleep[1])) { + Sys.sleep(sleep) + } return(file_list) - } diff --git a/data-raw/standardize_data_dims.R b/data-raw/standardize_data_dims.R index b3c084d..ac4aacc 100644 --- a/data-raw/standardize_data_dims.R +++ b/data-raw/standardize_data_dims.R @@ -5,39 +5,41 @@ all_data <- data(package = 'NEFSCspatial') ## extract names data_names <- all_data$results[, 'Item'] -data_names <- data_names[!data_names %in% c('ecomon_stations')] +data_names <- data_names[!data_names %in% c('ecomon_stations')] ## loop through each dataset for (i in data_names) { - # load data load(here::here('data', paste0(i, '.rda'))) - + # re-assign data_obj <- i |> get() - + # dimensions num_features <- nrow(data_obj) num_columns <- ncol(data_obj) - data_dim <- glue::glue("#' \\item{{Features}}{{{ num_features } x { num_columns }}") - + data_dim <- glue::glue( + "#' \\item{{Features}}{{{ num_features } x { num_columns }}" + ) + # build the file path file_path <- file.path("R", paste0("data_", i, ".R")) - if (!file.exists(file_path)) file_path <- file.path("R", paste0("data-", i, ".R")) + if (!file.exists(file_path)) { + file_path <- file.path("R", paste0("data-", i, ".R")) + } # read in data documentation data_doc <- readLines(con = file_path) - + # find Features - replace_id = grep(pattern = 'Features', x = data_doc) + replace_id <- grep(pattern = 'Features', x = data_doc) stopifnot(length(replace_id) == 1) - + # replace data_doc[replace_id] <- data_dim - + # write writeLines(data_doc, con = file_path) message(glue::glue("Updated Roxygen file for '{ i }' at: { file_path }")) - }