diff --git a/DESCRIPTION b/DESCRIPTION index a3fbf0a..c550c2d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: SELECTRdata Title: Download and Format Spatially Explicit Load Enrichment Calculation Tool ('SELECT') Data -Version: 0.1.2 +Version: 0.1.3 Authors@R: person("Michael", "Schramm", , "mpschramm@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-1876-6592")) diff --git a/NEWS.md b/NEWS.md index 3218b7b..3f28039 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# SELECTRdata 0.1.3 (2025-11-04) + +## New Features + + * template arguments accept SpatVector or SpatRaster objects. + # SELECTRdata 0.1.2 (2025-10-22) ## New Features diff --git a/R/download_NPDES_permits.R b/R/download_NPDES_permits.R index 78c60b4..8f6ab71 100644 --- a/R/download_NPDES_permits.R +++ b/R/download_NPDES_permits.R @@ -3,7 +3,7 @@ #' Downloads NPDES permits from EPA ECHO API services within the bounds of the #' SpatRast template. #' -#' @param template A SpatRaster object. +#' @param template A SpatRaster or SpatVector object. The extent of the returned object will match `template`. #' @param permit_component A character vector with one or more of the following: `PRE` (pretreatment), `CAF` (CAFO), `CSO` (CSO), `POT` (Publicly Owned Treatment Works, the default), `BIO` (Biosolids), `SWS` (Stormwater Small MS4), `SWM` (Stormwater Medium/Large MS4), `SWI` (Stormwater Industrial), `SWC` (Stormwater Construction). #' @param permit_status A character vector with one or more of the following: `EFF` (effective, the default), `EXP` (expired), `PND` (pending), `TRM` (terminated), `RET` (retired), `NON` (not needed), `ADC` (administratively continued). #' @param output A character file path specifying where the raster file should be stored. Defaults to a temporary file. @@ -21,8 +21,8 @@ download_NPDES_permits <- function(template, permit_status = "EFF", output = tempfile(fileext = ".gpkg")) { - ## check that DEM is SpatRaster - check_spat_ras(template) + ## check that DEM is SpatRaster or is SpatVector + check_terra_spat_obj(template) ## get bbox of the template. need lat, lons in decimal degrees ## create a bbox object from DEM diff --git a/R/download_buildings.R b/R/download_buildings.R index 40e7215..33eb08a 100644 --- a/R/download_buildings.R +++ b/R/download_buildings.R @@ -3,7 +3,7 @@ #' Downloads building footprints and attribute data from FEMA's #' USA Structures dataset (https://fema.maps.arcgis.com/home/item.html?id=0ec8512ad21e4bb987d7e848d14e7e24#overview). #' -#' @param template A SpatRaster object. The extent of the returned object will match `template`. +#' @param template A SpatRaster or SpatVector object. The extent of the returned object will match `template`. #' @param return A character object, either `SpatVector` or `sf`. Defaults to `SpatVector`. #' @param output A character file path specifying where the `SpatVector` file should be written. Defaults to a temporary file. #' @@ -28,8 +28,8 @@ download_buildings <- function(template, return(invisible(NULL)) } - ## check that DEM is SpatRaster - check_spat_ras(template) + ## check that DEM is SpatRaster or is SpatVector + check_terra_spat_obj(template) ## check that return is one of "SpatVector" or "sf" check_string(return) diff --git a/R/download_census_blocks.R b/R/download_census_blocks.R index a388bac..1711e9c 100644 --- a/R/download_census_blocks.R +++ b/R/download_census_blocks.R @@ -3,7 +3,7 @@ #' Downloads U.S. Census block boundaries and associated information for #' blocks within the spatial extent of the template SpatRaster. #' -#' @param template A SpatRaster object. +#' @param template A SpatRaster or SpatVector object. #' @param year A character value. Any of the following values should work: `c("2000","2010","2020")`. #' @param page_size a numeric value passed to `arcgislayers::arcselect()`. Defaults to NULL. Useful when the requests returns a 500 error code. #' @param output A character file path specifying where the raster file should be stored. Defaults to a temporary file. @@ -30,8 +30,8 @@ download_census_blocks <- function(template, if (!isTRUE(check_connectivity("services.arcgis.com"))) { return(invisible(NULL)) } - ## check template if a spatraster - check_spat_ras(template) + ## check that DEM is SpatRaster or is SpatVector + check_terra_spat_obj(template) ## check years year <- rlang::arg_match(year, diff --git a/R/download_counties.R b/R/download_counties.R index e61eec5..7275261 100644 --- a/R/download_counties.R +++ b/R/download_counties.R @@ -3,7 +3,7 @@ #' Shortcut function that downloads and extracts TIGER U.S. County boundaries #' and returns them as a terra SpatVector object. #' -#' @param template A SpatRaster object. +#' @param template A SpatRaster or SpatVector object. The extent of the returned object will match `template`. #' @param output A character file path specifying where the raster file should be stored. Defaults to a temporary file. #' #' @return A terra SpatVector object. If API resources are not available an invisible `NULL` is returned. @@ -28,8 +28,8 @@ download_counties <- function(template, return(invisible(NULL)) } - ## check template if a spatraster - check_spat_ras(template) + ## check that DEM is SpatRaster or is SpatVector + check_terra_spat_obj(template) furl <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Census_Counties/FeatureServer" diff --git a/R/download_nlcd.R b/R/download_nlcd.R index a13fe74..bb025a0 100644 --- a/R/download_nlcd.R +++ b/R/download_nlcd.R @@ -5,7 +5,7 @@ #' #' Downloads and writes an NLCD SpatRaster to file with extents defined by `template`. This function downloads the annualized NLCD data products. See [https://www.mrlc.gov/data/project/annual-nlcd](https://www.mrlc.gov/data/project/annual-nlcd) for more information. #' -#' @param template A SpatRaster object defining the spatial extent of the returned NLCD raster. +#' @param template A SpatRaster or SpatVector object. The extent of the returned object will match `template`. #' @param year character, expects a value between `1986:2024`. #' @param dataset Character. Expects `c("LndCov","LndChg","LndCnf","FctImp","ImpDsc","SpcChg")`. Only `"LndCov"` is supported at this time. #' @param landmass Depreciated. Character, one of: `c("CU", "AK", "HI")`. @@ -39,8 +39,8 @@ download_nlcd <- function(template, return(invisible(NULL)) } - ## check template if a spatraster - check_spat_ras(template) + ## check that DEM is SpatRaster or is SpatVector + check_terra_spat_obj(template) ## need to check year, dataset, and landmass are valid values ## check landmass @@ -121,7 +121,7 @@ download_nlcd <- function(template, wrap = TRUE) template_ext <- terra::project(template_ext, from = template_srs, - to = nlcd_epsg) + to = paste0("epsg:",nlcd_epsg)) } diff --git a/R/download_urban_areas.R b/R/download_urban_areas.R index e28df95..9216394 100644 --- a/R/download_urban_areas.R +++ b/R/download_urban_areas.R @@ -2,7 +2,7 @@ #' #' Downloads a U.S. Census designated urban areas intersecting the spatial extent of the template SpatRaster. #' -#' @param template A SpatRaster object. +#' @param template A SpatRaster or SpatVector object. #' @param page_size a numeric value passed to `arcgislayers::arcselect()`. Defaults to NULL. Useful when the requests returns a 500 error code. #' @param output A character file path specifying where the `SpatVector` output object should be written. Defaults to a temporary file. #' @@ -29,8 +29,8 @@ download_urban_areas <- function(template, if (!isTRUE(check_connectivity("services.arcgis.com"))) { return(invisible(NULL)) } - ## check template if a spatraster - check_spat_ras(template) + ## check that DEM is SpatRaster or is SpatVector + check_terra_spat_obj(template) furl <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Census_Urban_Areas/FeatureServer" diff --git a/R/helpers.R b/R/helpers.R index b95106c..f4e4cec 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -49,6 +49,16 @@ check_spat_ras <- function(x, } } + +check_terra_spat_obj <- function(x, + arg = rlang::caller_arg(x), + call = rlang::caller_env()) { + if(!inherits(x, c("SpatRaster", "SpatRasterCollection", "SpatVector"))) { + cli::cli_abort("The object supplied to {.arg {arg}} must be a {.cls SpatRaster}, {.cls SpatRasterCollection}, or {.cls SpatVector} object. Try loading the raster with {.code terra::rast()} or {.code terra::vect()}.", + call = call) + } +} + check_string <- function(x, arg = rlang::caller_arg(x), call = rlang::caller_env()) { diff --git a/README.Rmd b/README.Rmd index 5aedad4..cabcf08 100644 --- a/README.Rmd +++ b/README.Rmd @@ -45,14 +45,38 @@ You can install the development version of SELECTRdata like so: install.packages("SELECTRdata", repos = c("https://txwri.r-universe.dev", "https://cloud.r-project.org")) ``` -## Example +## Examples -## MRLC National Land Cover Dataset +### USGS DEM -```{r example} +```{r dem} library(SELECTRdata) library(terra) +## our location of interest +location_of_interest <- system.file("extdata", "thompsoncreek.tif", package = "SELECTRdata") +location_of_interest <- terra::rast(location_of_interest) +## grab the extent +extent <- ext(location_of_interest) +## create a extent or object that an extent and srs +## can be grabbed +extent <- vect(extent, crs = crs(location_of_interest)) +## project the extent to something used by USGS Seamless data +extent <- project(extent, "EPSG:6579") +## grab the epsg code from our extent +auth <- crs(extent, describe = TRUE) +auth <- paste0(auth$authority, ":", auth$code) +extent <- ext(extent) + +## download DEM +example_dem <- download_dem(x = extent, srs = auth) +plot(example_dem) +``` + + +### MRLC National Land Cover Dataset + +```{r example} ## we need a template file, this is the thomsoncreek watershed in Texas dem <- system.file("extdata", "thompsoncreek.tif", package = "SELECTRdata") dem <- terra::rast(dem) diff --git a/README.md b/README.md index c92d3fa..7491286 100644 --- a/README.md +++ b/README.md @@ -41,16 +41,41 @@ You can install the development version of SELECTRdata like so: install.packages("SELECTRdata", repos = c("https://txwri.r-universe.dev", "https://cloud.r-project.org")) ``` -## Example +## Examples -## MRLC National Land Cover Dataset +### USGS DEM ``` r library(SELECTRdata) library(terra) #> Warning: package 'terra' was built under R version 4.3.3 -#> terra 1.8.10 +#> terra 1.8.29 + +## our location of interest +location_of_interest <- system.file("extdata", "thompsoncreek.tif", package = "SELECTRdata") +location_of_interest <- terra::rast(location_of_interest) +## grab the extent +extent <- ext(location_of_interest) +## create a extent or object that an extent and srs +## can be grabbed +extent <- vect(extent, crs = crs(location_of_interest)) +## project the extent to something used by USGS Seamless data +extent <- project(extent, "EPSG:6579") +## grab the epsg code from our extent +auth <- crs(extent, describe = TRUE) +auth <- paste0(auth$authority, ":", auth$code) +extent <- ext(extent) + +## download DEM +example_dem <- download_dem(x = extent, srs = auth) +plot(example_dem) +``` + + +### MRLC National Land Cover Dataset + +``` r ## we need a template file, this is the thomsoncreek watershed in Texas dem <- system.file("extdata", "thompsoncreek.tif", package = "SELECTRdata") dem <- terra::rast(dem) @@ -81,9 +106,9 @@ buildings <- download_buildings(template = dem) #> Registered S3 method overwritten by 'jsonify': #> method from #> print.json jsonlite -#> Iterating ■■■■■ 12% | ETA: 19sIterating ■■■■■■■■■ 25% | ETA: 11sIterating -#> ■■■■■■■■■■■■■■■■ 50% | ETA: 4sIterating ■■■■■■■■■■■■■■■■■■■■■■■ 75% | ETA: -#> 2sIterating ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 88% | ETA: 1s +#> Iterating ■■■■■ 12% | ETA: 8sIterating ■■■■■■■■■ 25% | ETA: 6sIterating +#> ■■■■■■■■■■■■ 38% | ETA: 4sIterating ■■■■■■■■■■■■■■■■■■■■ 62% | ETA: 2sIterating +#> ■■■■■■■■■■■■■■■■■■■■■■■■■■■ 88% | ETA: 0s plot(buildings) plot(wbd, add = TRUE) ``` diff --git a/man/download_NPDES_permits.Rd b/man/download_NPDES_permits.Rd index 919969f..f3d8818 100644 --- a/man/download_NPDES_permits.Rd +++ b/man/download_NPDES_permits.Rd @@ -12,7 +12,7 @@ download_NPDES_permits( ) } \arguments{ -\item{template}{A SpatRaster object.} +\item{template}{A SpatRaster or SpatVector object. The extent of the returned object will match \code{template}.} \item{permit_component}{A character vector with one or more of the following: \code{PRE} (pretreatment), \code{CAF} (CAFO), \code{CSO} (CSO), \code{POT} (Publicly Owned Treatment Works, the default), \code{BIO} (Biosolids), \code{SWS} (Stormwater Small MS4), \code{SWM} (Stormwater Medium/Large MS4), \code{SWI} (Stormwater Industrial), \code{SWC} (Stormwater Construction).} diff --git a/man/download_buildings.Rd b/man/download_buildings.Rd index 5abdb4c..9c830bb 100644 --- a/man/download_buildings.Rd +++ b/man/download_buildings.Rd @@ -11,7 +11,7 @@ download_buildings( ) } \arguments{ -\item{template}{A SpatRaster object. The extent of the returned object will match \code{template}.} +\item{template}{A SpatRaster or SpatVector object. The extent of the returned object will match \code{template}.} \item{return}{A character object, either \code{SpatVector} or \code{sf}. Defaults to \code{SpatVector}.} diff --git a/man/download_census_blocks.Rd b/man/download_census_blocks.Rd index 37af738..693a348 100644 --- a/man/download_census_blocks.Rd +++ b/man/download_census_blocks.Rd @@ -12,7 +12,7 @@ download_census_blocks( ) } \arguments{ -\item{template}{A SpatRaster object.} +\item{template}{A SpatRaster or SpatVector object.} \item{year}{A character value. Any of the following values should work: \code{c("2000","2010","2020")}.} diff --git a/man/download_counties.Rd b/man/download_counties.Rd index 33d6db9..9ac8370 100644 --- a/man/download_counties.Rd +++ b/man/download_counties.Rd @@ -7,7 +7,7 @@ download_counties(template, output = tempfile(fileext = ".gpkg")) } \arguments{ -\item{template}{A SpatRaster object.} +\item{template}{A SpatRaster or SpatVector object. The extent of the returned object will match \code{template}.} \item{output}{A character file path specifying where the raster file should be stored. Defaults to a temporary file.} } diff --git a/man/download_nlcd.Rd b/man/download_nlcd.Rd index acc17d6..1f8c7a7 100644 --- a/man/download_nlcd.Rd +++ b/man/download_nlcd.Rd @@ -16,7 +16,7 @@ download_nlcd( ) } \arguments{ -\item{template}{A SpatRaster object defining the spatial extent of the returned NLCD raster.} +\item{template}{A SpatRaster or SpatVector object. The extent of the returned object will match \code{template}.} \item{year}{character, expects a value between \code{1986:2024}.} diff --git a/man/download_urban_areas.Rd b/man/download_urban_areas.Rd index 32ab2c2..a10dc2e 100644 --- a/man/download_urban_areas.Rd +++ b/man/download_urban_areas.Rd @@ -11,7 +11,7 @@ download_urban_areas( ) } \arguments{ -\item{template}{A SpatRaster object.} +\item{template}{A SpatRaster or SpatVector object.} \item{page_size}{a numeric value passed to \code{arcgislayers::arcselect()}. Defaults to NULL. Useful when the requests returns a 500 error code.} diff --git a/man/figures/README-dem-1.png b/man/figures/README-dem-1.png new file mode 100644 index 0000000..df331c4 Binary files /dev/null and b/man/figures/README-dem-1.png differ diff --git a/man/figures/README-unnamed-chunk-7-1.png b/man/figures/README-unnamed-chunk-7-1.png index cb317e7..8687dab 100644 Binary files a/man/figures/README-unnamed-chunk-7-1.png and b/man/figures/README-unnamed-chunk-7-1.png differ