Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sf and terra [patch] #522

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions DataCleaningScripts/update_ndvi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand Down Expand Up @@ -50,24 +50,23 @@ 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
# https://www.usgs.gov/media/files/landsat-8-9-collection-2-level-2-science-product-guide
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)
}
Expand Down
39 changes: 28 additions & 11 deletions install-packages.R
Original file line number Diff line number Diff line change
@@ -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)
Loading