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 d9f45e95..74ac7088 100644 --- a/install-packages.R +++ b/install-packages.R @@ -12,8 +12,7 @@ install.packages(c("curl", "lubridate", "lunar", "lwgeom", - "openxlsx", - "raster", + "openxlsx", "Rcpp", "RCurl", "remotes",