diff --git a/DataCleaningScripts/update_ndvi.R b/DataCleaningScripts/update_ndvi.R index 5353f294..14d2b275 100644 --- a/DataCleaningScripts/update_ndvi.R +++ b/DataCleaningScripts/update_ndvi.R @@ -3,7 +3,7 @@ # https://www.usgs.gov/landsat-missions/landsat-collection-2-level-2-science-products `%>%` <- magrittr::`%>%` -library(raster) +library(terra) #' @title create_portal_area #' @@ -50,15 +50,15 @@ extract_and_mask_raster <- function(records, targetpath = tempdir()) { # read in raster files; crop to portal_box; apply scaling factor and offset; delete full-size raster files # In Landsat 8-9, NDVI = (Band 5 – Band 4) / (Band 5 + Band 4). - B4 <- raster::raster(paste0(targetpath,"/", records["display_id"], "_SR_B4.TIF")) %>% - raster::crop(portal_area) * 0.0000275 + -0.2 - B5 <- raster::raster(paste0(targetpath,"/", records["display_id"], "_SR_B5.TIF")) %>% - raster::crop(portal_area) * 0.0000275 + -0.2 + B4 <- terra::rast(paste0(targetpath,"/", records["display_id"], "_SR_B4.TIF")) %>% + terra::crop(portal_area) * 0.0000275 + -0.2 + B5 <- terra::rast(paste0(targetpath,"/", records["display_id"], "_SR_B5.TIF")) %>% + terra::crop(portal_area) * 0.0000275 + -0.2 sr_ndvi <- (B5 - B4)/(B5 + B4) - pixelqa <- raster::raster(paste0(targetpath,"/", records["display_id"], "_QA_PIXEL.TIF")) %>% - raster::crop(portal_area) + pixelqa <- terra::rast(paste0(targetpath,"/", records["display_id"], "_QA_PIXEL.TIF")) %>% + terra::crop(portal_area) # mask ndvi data # "clear" values of pixel_qa are derived from the CFMask algorithm version 3.3.1 @@ -66,8 +66,7 @@ extract_and_mask_raster <- function(records, targetpath = tempdir()) { clearvalues = c(21824, 21826, 22080, 23888, 30048, 54596, 54852) pixelqa[!(pixelqa %in% clearvalues)] <- NA - ndvi_masked <- raster::mask(x=sr_ndvi, mask=pixelqa) - # raster::writeRaster(s,paste0(targetpath,"/", record_id,'_ndvi_masked.tif'), overwrite=TRUE) + ndvi_masked <- terra::mask(x=sr_ndvi, mask=pixelqa) return(ndvi_masked) } diff --git a/install-packages.R b/install-packages.R index eb54084b..50ca6925 100644 --- a/install-packages.R +++ b/install-packages.R @@ -1,18 +1,35 @@ -# Install pacman if it isn't already installed - -if ("pacman" %in% rownames(installed.packages()) == FALSE) install.packages("pacman") - remove.packages("stringi") remove.packages("stringr") -install.packages(c("Rcpp","stringi","stringr","RCurl","curl","Hmisc", "EML", "sf","rgdal","lwgeom", "git2r","remotes"), type="source", repos="https://cran.rstudio.com") +install.packages(c("curl", + "EML", + "git2r", + "Hmisc", + "lwgeom", + "Rcpp", + "RCurl", + "remotes", + "rgdal", + "stringi", + "stringr", + "sf"), type="source", repos="https://cran.rstudio.com") install.packages('terra', repos='https://rspatial.r-universe.dev') remotes::install_github("htmltab/htmltab") # Install analysis packages using pacman +if ("pacman" %in% rownames(installed.packages()) == FALSE) install.packages("pacman") -# pacman::p_load(units, sf, rgdal, lwgeom, shiny, stringi, stringr, Hmisc, dplyr, git2r, openxlsx, lubridate, htmltab, -# lunar, jsonlite, devtools, sp, sqldf, raster, RCurl, EML, testthat, zoo, tidyr, semver, yaml) - - -pacman::p_load(units, shiny, dplyr, openxlsx, lubridate, htmltab, - lunar, jsonlite, devtools, sp, sqldf, raster, testthat, zoo, tidyr, semver, yaml) +pacman::p_load(devtools, + dplyr, + jsonlite, + lubridate, + lunar, + openxlsx, + semver, + shiny, + sp, + sqldf, + testthat, + tidyr, + units, + yaml, + zoo)