From b24df9751dbcfb57bb349cd65734a1be21ed9a09 Mon Sep 17 00:00:00 2001 From: istfer Date: Wed, 1 Nov 2023 14:05:05 +0200 Subject: [PATCH 1/8] rgdal to sf in gis.functions --- modules/data.land/DESCRIPTION | 1 - modules/data.land/R/gis.functions.R | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/data.land/DESCRIPTION b/modules/data.land/DESCRIPTION index 5ec23616b0f..eb187ac1667 100644 --- a/modules/data.land/DESCRIPTION +++ b/modules/data.land/DESCRIPTION @@ -61,7 +61,6 @@ Suggests: PEcAn.settings, redland, raster, - rgdal, RPostgreSQL, testthat (>= 1.0.2) License: BSD_3_clause + file LICENSE diff --git a/modules/data.land/R/gis.functions.R b/modules/data.land/R/gis.functions.R index 8f9434aea3f..0fa83941f88 100644 --- a/modules/data.land/R/gis.functions.R +++ b/modules/data.land/R/gis.functions.R @@ -68,14 +68,13 @@ shp2kml <- function(dir, ext, kmz = FALSE, proj4 = NULL, color = NULL, NameField # Read in shapefile(s) & get coordinates/projection info shp.file <- # readShapeSpatial(file.path(dir,i),verbose=TRUE) coordinates(test) <- ~X+Y - layers <- rgdal::ogrListLayers(file.path(dir, i)) - info <- rgdal::ogrInfo(file.path(dir, i), layers) + layers <- sf::st_layers(file.path(dir, i)) # shp.file <- readOGR(file.path(dir,i),layer=layers) # no need to read in file # Display vector info to the console print("") - print(paste0("Input layers: ", layers)) - print(paste0("Input projection info: ", info$p4s)) + print(paste0("Input layers: ", layers$name)) + print(paste0("Input projection info: ", layers$crs[[1]]$input)) print("") # Write out kml/kmz using plotKML package if (is.null(color)){ color <- 'grey70' } @@ -133,6 +132,7 @@ get.attributes <- function(file, coords) { #library(fields) #require(rgdal) + # note that OGR support is now provided by the sf and terra packages among others # print('NOT IMPLEMENTED YET') subset_layer(file,coords) } # get.attributes From 87559d7c6130b629ca45cfe8fe68db02126515f4 Mon Sep 17 00:00:00 2001 From: istfer Date: Wed, 1 Nov 2023 14:25:19 +0200 Subject: [PATCH 2/8] removed rgdal call from data.remote as well --- modules/data.remote/DESCRIPTION | 1 - modules/data.remote/R/NLCD.R | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/data.remote/DESCRIPTION b/modules/data.remote/DESCRIPTION index 68e3ee22e89..c994d1cd476 100644 --- a/modules/data.remote/DESCRIPTION +++ b/modules/data.remote/DESCRIPTION @@ -36,7 +36,6 @@ Suggests: dplyr, ggplot2, lubridate, - rgdal, reshape, testthat (>= 1.0.2), tibble diff --git a/modules/data.remote/R/NLCD.R b/modules/data.remote/R/NLCD.R index dfc712f2e09..98fc995fa1a 100644 --- a/modules/data.remote/R/NLCD.R +++ b/modules/data.remote/R/NLCD.R @@ -69,8 +69,7 @@ download.NLCD <- function(outdir, year = 2011, con = NULL) { ##' @description Based on codes from Christy Rollinson and from Max Joseph (http://mbjoseph.github.io/2014/11/08/nlcd.html) extract_NLCD <- function(buffer, coords, data_dir = NULL, con = NULL, year = 2011) { library(raster) - require(rgdal) - + if (!is.null(con)) { library(PEcAn.DB) if (year == 2001) { From c97edb155c0465a2666afd0f95df9590b26427fd Mon Sep 17 00:00:00 2001 From: istfer Date: Wed, 1 Nov 2023 16:34:00 +0200 Subject: [PATCH 3/8] attempt to change raster with terra --- modules/data.remote/R/NLCD.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/data.remote/R/NLCD.R b/modules/data.remote/R/NLCD.R index 98fc995fa1a..6c7e1ff71cb 100644 --- a/modules/data.remote/R/NLCD.R +++ b/modules/data.remote/R/NLCD.R @@ -103,21 +103,21 @@ extract_NLCD <- function(buffer, coords, data_dir = NULL, con = NULL, year = 201 print(paste("File not found:", filename)) return(NULL) } - nlcd <- raster(filename) + nlcd <- terra::rast(filename) # transform points - sites <- SpatialPoints(coords = coords, proj4string = CRS("+proj=longlat +datum=WGS84")) - sites <- spTransform(sites, crs(nlcd)) + sites <- terra::vect(coords, geom=c("long", "lat"), crs="+proj=longlat +datum=WGS84") + sites <- terra::buffer(x, width=buffer) # extract - sum.raw <- table(extract(nlcd, sites, buffer = buffer)) + sum.raw <- table(terra::extract(nlcd, sites)) summ <- prop.table(sum.raw) - mydf <- data.frame(cover = names(summ), percent = as.vector(summ), count = as.vector(sum.raw)) + mydf <- data.frame(cover.name = colnames(summ), percent = as.vector(summ), count = as.vector(sum.raw)) + mydf <- mydf[mydf$count!=0,] - # land cover number to name conversions - cover.table <- nlcd@data@attributes[[1]] - cover.names <- cover.table[as.numeric(as.character(mydf$cover)) + 1, grep("Land", names(cover.table))] - mydf$cover.name <- cover.names + # land cover name to number conversions + nlcd_levels <- terra::levels(nlcd)[[1]] + mydf$cover <- nlcd_levels$value[nlcd_levels$`Land Cover Class` %in% mydf$cover.name] return(mydf) } # extract_NLCD From 1114f56796a157b55a12d4cfa4b5afb31fa5e9fb Mon Sep 17 00:00:00 2001 From: istfer Date: Wed, 1 Nov 2023 16:39:38 +0200 Subject: [PATCH 4/8] add warning --- modules/data.remote/R/NLCD.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/data.remote/R/NLCD.R b/modules/data.remote/R/NLCD.R index 6c7e1ff71cb..01005ddc479 100644 --- a/modules/data.remote/R/NLCD.R +++ b/modules/data.remote/R/NLCD.R @@ -103,6 +103,9 @@ extract_NLCD <- function(buffer, coords, data_dir = NULL, con = NULL, year = 201 print(paste("File not found:", filename)) return(NULL) } + + # WARNING: the following extraction previously used raster and sp package functions + # this new implementation with terra functions has not been thoroughly tested nlcd <- terra::rast(filename) # transform points From bfb03a83141bf131b23bc490c416f6e9962f838e Mon Sep 17 00:00:00 2001 From: istfer Date: Wed, 1 Nov 2023 16:50:46 +0200 Subject: [PATCH 5/8] remove library call to raster --- modules/data.remote/R/NLCD.R | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/data.remote/R/NLCD.R b/modules/data.remote/R/NLCD.R index 01005ddc479..a0416cdebbb 100644 --- a/modules/data.remote/R/NLCD.R +++ b/modules/data.remote/R/NLCD.R @@ -68,7 +68,6 @@ download.NLCD <- function(outdir, year = 2011, con = NULL) { ##' ##' @description Based on codes from Christy Rollinson and from Max Joseph (http://mbjoseph.github.io/2014/11/08/nlcd.html) extract_NLCD <- function(buffer, coords, data_dir = NULL, con = NULL, year = 2011) { - library(raster) if (!is.null(con)) { library(PEcAn.DB) From 20315900ada53664babefe13f957899f45fd7cc8 Mon Sep 17 00:00:00 2001 From: istfer Date: Wed, 1 Nov 2023 16:51:45 +0200 Subject: [PATCH 6/8] fix typo --- modules/data.remote/R/NLCD.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/data.remote/R/NLCD.R b/modules/data.remote/R/NLCD.R index a0416cdebbb..0e891aec2eb 100644 --- a/modules/data.remote/R/NLCD.R +++ b/modules/data.remote/R/NLCD.R @@ -109,7 +109,7 @@ extract_NLCD <- function(buffer, coords, data_dir = NULL, con = NULL, year = 201 # transform points sites <- terra::vect(coords, geom=c("long", "lat"), crs="+proj=longlat +datum=WGS84") - sites <- terra::buffer(x, width=buffer) + sites <- terra::buffer(sites, width=buffer) # extract sum.raw <- table(terra::extract(nlcd, sites)) From 8263c71e923cac88e92431b4be36c72d96d0a898 Mon Sep 17 00:00:00 2001 From: istfer Date: Thu, 2 Nov 2023 17:09:10 +0200 Subject: [PATCH 7/8] remove library and require calls from data.remote --- modules/data.remote/R/NLCD.R | 4 ++-- modules/data.remote/R/download.thredds.R | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/data.remote/R/NLCD.R b/modules/data.remote/R/NLCD.R index 0e891aec2eb..32517eff2ae 100644 --- a/modules/data.remote/R/NLCD.R +++ b/modules/data.remote/R/NLCD.R @@ -23,7 +23,7 @@ download.NLCD <- function(outdir, year = 2011, con = NULL) { ## before downloading, check if the file already exists on this host if (!is.null(con)) { - library(PEcAn.DB) + chk <- dbfile.check(type = "Input", id = input.id, con = con) if (nrow(chk) > 0) { machines <- db.query(paste("SELECT * from machines where id in (", @@ -70,7 +70,7 @@ download.NLCD <- function(outdir, year = 2011, con = NULL) { extract_NLCD <- function(buffer, coords, data_dir = NULL, con = NULL, year = 2011) { if (!is.null(con)) { - library(PEcAn.DB) + if (year == 2001) { input.id <- 1000000482 } else if (year == 2011) { diff --git a/modules/data.remote/R/download.thredds.R b/modules/data.remote/R/download.thredds.R index 09d44ac1337..04fc4b99923 100755 --- a/modules/data.remote/R/download.thredds.R +++ b/modules/data.remote/R/download.thredds.R @@ -82,7 +82,7 @@ download.thredds.AGB <- function(outdir = NULL, site_ids, run_parallel = FALSE, } else { ncores <- parallel::detectCores() -1 } - require(doParallel) + PEcAn.logger::logger.info(paste0("Running in parallel with: ", ncores)) cl = parallel::makeCluster(ncores) doParallel::registerDoParallel(cl) From 9137628fa99dd82ab1d1e98b059092d879fbdbcb Mon Sep 17 00:00:00 2001 From: istfer Date: Thu, 2 Nov 2023 18:56:38 +0200 Subject: [PATCH 8/8] CI updates --- modules/data.remote/DESCRIPTION | 3 ++- modules/data.remote/tests/Rcheck_reference.log | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/data.remote/DESCRIPTION b/modules/data.remote/DESCRIPTION index c994d1cd476..617f49b1338 100644 --- a/modules/data.remote/DESCRIPTION +++ b/modules/data.remote/DESCRIPTION @@ -20,7 +20,6 @@ Imports: PEcAn.utils, purrr, XML, - raster, sp, MODISTools (>= 1.1.0), reticulate, @@ -28,6 +27,7 @@ Imports: magrittr, PEcAn.remote, stringr (>= 1.1.0), + terra, doParallel, parallel, foreach @@ -36,6 +36,7 @@ Suggests: dplyr, ggplot2, lubridate, + raster, reshape, testthat (>= 1.0.2), tibble diff --git a/modules/data.remote/tests/Rcheck_reference.log b/modules/data.remote/tests/Rcheck_reference.log index 822a9411303..182f76f6d12 100644 --- a/modules/data.remote/tests/Rcheck_reference.log +++ b/modules/data.remote/tests/Rcheck_reference.log @@ -82,8 +82,6 @@ Author field differs from that derived from Authors@R * checking loading without being on the library search path ... OK * checking use of S3 registration ... OK * checking dependencies in R code ... WARNING -'library' or 'require' calls not declared from: - ‘PEcAn.DB’ ‘doParallel’ ‘raster’ 'library' or 'require' calls in package code: ‘PEcAn.DB’ ‘doParallel’ ‘raster’ ‘rgdal’ Please use :: or requireNamespace() instead.