From b76881470f5b8057b4f7562c2ea720c4d273d5f8 Mon Sep 17 00:00:00 2001 From: ecor Date: Mon, 10 Jun 2024 12:31:18 +0200 Subject: [PATCH] Watershed Analysis - Documentation and Tests --- .gitignore | 2 ++ DESCRIPTION | 2 +- NAMESPACE | 5 ++++- R/generics.R | 5 +++-- ...est_flowacc2.R => test_flowAccumulation.R} | 2 +- .../{test_pitfinder2.R => test_pitfinder.R} | 2 +- man/NIPD.Rd | 4 ++-- ...flowAccumulaion.Rd => flowAccumulation.Rd} | 20 +++++++++---------- man/pitfinder.Rd | 8 ++++---- man/watershed.Rd | 2 +- 10 files changed, 29 insertions(+), 23 deletions(-) rename inst/tinytest/{test_flowacc2.R => test_flowAccumulation.R} (96%) rename inst/tinytest/{test_pitfinder2.R => test_pitfinder.R} (96%) rename man/{flowAccumulaion.Rd => flowAccumulation.Rd} (80%) diff --git a/.gitignore b/.gitignore index 7e97e7d2f..df8ca5ee1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,8 @@ src/Makevars.ucrt.rej proj_conf_test .Rproj +proj_conf_test* +*.Rproj *.tar.gz *Rcheck/ *.o diff --git a/DESCRIPTION b/DESCRIPTION index 7ee9a1a5d..4bd361d7f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: terra Type: Package Title: Spatial Data Analysis Version: 1.7-79 -Date: 2024-05-23 +Date: 2024-06-10 Depends: R (>= 3.5.0) Suggests: parallel, tinytest, ncdf4, sf (>= 0.9-8), deldir, XML, leaflet (>= 2.2.1), htmlwidgets LinkingTo: Rcpp diff --git a/NAMESPACE b/NAMESPACE index c5eb1693a..3b8f5580c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,7 +5,9 @@ exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats ## EC 20210702 ecor exportMethods(watershed, pitfinder, NIDP, flowAccumulation) - +## EC 20250606 ecor +###exportMethods(watershed2,pitfinder2,NIDP2,flowAccu2,flowAccu2_weight) +## END EC S3method(cbind, SpatVector) S3method(rbind, SpatVector) @@ -28,3 +30,4 @@ export(add_box, add_legend, add_grid, focalMat, extractAlong, gdal, getGDALconfi + diff --git a/R/generics.R b/R/generics.R index 2a36e6059..2f56f1381 100644 --- a/R/generics.R +++ b/R/generics.R @@ -1060,15 +1060,16 @@ setMethod("terrain", signature(x="SpatRaster"), function(x, v="slope", neighbors=8, unit="degrees", filename="", ...) { if (v=="flowacc") { - flowAccu2(x,filename=filename,...) + flowAccumulation(x,filename=filename,...) } else { unit <- match.arg(unit, c("degrees", "radians")) opt <- spatOptions(filename, ...) seed <- ifelse("flowdir" %in% v, .seed(), 0) x@ptr <- x@ptr$terrain(v, neighbors[1], unit=="degrees", seed, opt) - messages(x, "terrain") + } + messages(x, "terrain") } ) diff --git a/inst/tinytest/test_flowacc2.R b/inst/tinytest/test_flowAccumulation.R similarity index 96% rename from inst/tinytest/test_flowacc2.R rename to inst/tinytest/test_flowAccumulation.R index 436d9835c..bbefb96c9 100644 --- a/inst/tinytest/test_flowacc2.R +++ b/inst/tinytest/test_flowAccumulation.R @@ -27,7 +27,7 @@ flowacc0 <- c(001,001,001,001,001,001, flowdir1 <- terrain(elev,"flowdir") -flowacc1 <- flowAccu2(flowdir1) +flowacc1 <- flowAccumulation(flowdir1) result <- (flowacc1==flowacc0) & (flowdir1==flowdir0) diff --git a/inst/tinytest/test_pitfinder2.R b/inst/tinytest/test_pitfinder.R similarity index 96% rename from inst/tinytest/test_pitfinder2.R rename to inst/tinytest/test_pitfinder.R index b0bcb4ecd..59d89a9f3 100644 --- a/inst/tinytest/test_pitfinder2.R +++ b/inst/tinytest/test_pitfinder.R @@ -30,7 +30,7 @@ flowdir<- terrain(elev,v="flowdir") ## Pit Detect -pits1 <- pitfinder2(flowdir) +pits1 <- pitfinder(flowdir) xypit <- as.data.frame(pits1,xy=TRUE) names(xypit) <- c("x","y","pit") diff --git a/man/NIPD.Rd b/man/NIPD.Rd index 8e3d67dab..874579fea 100644 --- a/man/NIPD.Rd +++ b/man/NIPD.Rd @@ -82,8 +82,8 @@ plot(flowdir1) plot(flowdir2) ## -nidp1 <- NIDP2((flowdir1)) -nidp2 <- NIDP2((flowdir2)) +nidp1 <- NIDP((flowdir1)) +nidp2 <- NIDP((flowdir2)) t(array(nidp1[],rev(dim(nidp1)[1:2]))) t(array(nidp2[],rev(dim(nidp2)[1:2]))) diff --git a/man/flowAccumulaion.Rd b/man/flowAccumulation.Rd similarity index 80% rename from man/flowAccumulaion.Rd rename to man/flowAccumulation.Rd index 26f976551..0ffc9c1e8 100644 --- a/man/flowAccumulaion.Rd +++ b/man/flowAccumulation.Rd @@ -12,7 +12,7 @@ Computes flow accumulation or the total contributing area in terms of numbers of } \usage{ -\S4method{flowAccu2}{SpatRaster}(x, weight=NULL, filename="", ...) +\S4method{flowAccumulation}{SpatRaster}(x, weight=NULL, filename="", ...) } \arguments{ @@ -30,7 +30,7 @@ SpatRaster The algorithm is an adaptation of the one proposed by Zhou at al, 2019. } -\seealso{\code{\link{terrain}},\code{\link{watershed2}},\code{\link{NIDP2}}} +\seealso{\code{\link{terrain}},\code{\link{watershed}},\code{\link{NIDP}}} \author{ Emanuele Cordano @@ -53,7 +53,7 @@ for (r in 1:nrow(elev1)) { x <- (c-5)*dy elev1[r,c] <- 5*(x^2+y^2) - elev2[r,c] <- 10+5*(abs(x))-0.001*y ### 5*(x^2+y^2) + elev2[r,c] <- 10+5*(abs(x))-0.001*y } } @@ -80,10 +80,10 @@ plot(flowdir1) plot(flowdir2) ## -flow_acc1 <- flowAccu2((flowdir1)) -flow_acc2 <- flowAccu2((flowdir2)) +flow_acc1 <- flowAccumulation((flowdir1)) +flow_acc2 <- flowAccumulation((flowdir2)) ## Alternative option -## here the argument of "terrain"functions is not elevation but flow directions. +## here the argument of "terrain" functions is flow directions instead of elevation. flow_acc2a <- terrain(flowdir2,"flowacc") t(array(flow_acc1[],rev(dim(flow_acc1)[1:2]))) @@ -95,8 +95,8 @@ plot(flow_acc2) weight <- elev1*0+10 -flow_acc1w <- flowAccu2_weight(flowdir1,weight) -flow_acc2w <- flowAccu2_weight(flowdir2,weight) +flow_acc1w <- flowAccumulation(flowdir1,weight) +flow_acc2w <- flowAccumulation(flowdir2,weight) t(array(flow_acc1w[],rev(dim(flow_acc1w)[1:2]))) t(array(flow_acc2w[],rev(dim(flow_acc2w)[1:2]))) @@ -111,8 +111,8 @@ elev <- rast(system.file('ex/elev.tif',package="terra")) flowdir <- terrain(elev,"flowdir") weight <- cellSize(elev,unit="km") -flowacc_weight <- flowAccu2_weight(flowdir,weight) -flowacc <- flowAccu2(flowdir) +flowacc_weight <- flowAccumulation(flowdir,weight) +flowacc <- flowAccumulation(flowdir) } diff --git a/man/pitfinder.Rd b/man/pitfinder.Rd index c11e56ae7..abe04e807 100644 --- a/man/pitfinder.Rd +++ b/man/pitfinder.Rd @@ -16,7 +16,7 @@ find pits (depressions with no outlet ) } \arguments{ - \item{p}{SpatRaster wih flow-direcion. See \code{\link{terrain}}} + \item{x}{SpatRaster wih flow-direcion. See \code{\link{terrain}}} \item{filename}{character. Output filename} \item{...}{additional arguments for writing files as in \code{\link{writeRaster}}} } @@ -32,7 +32,7 @@ A \code{\link{SpatRaster-class}} (raster) map containing value 1 for the pits a Emanuele Cordano } -\seealso{\code{\link{terrain}},\code{\link{watershed2}},\code{\link{flowAccu2}},\code{\link{flowAccu2_weight}},\code{\link{NIDP2}}} +\seealso{\code{\link{terrain}},\code{\link{watershed}},\code{\link{flowAccumulation}},\code{\link{NIDP}}} \examples{ ## Creation of a Digital Elevation Model @@ -60,14 +60,14 @@ t(array(flowdir[],rev(dim(flowdir)[1:2]))) ## Pit Detect -pits <- pitfinder2(flowdir) +pits <- pitfinder(flowdir) ## Application wth example DEM elev <- rast(system.file('ex/elev.tif',package="terra")) flowdir <- terrain(elev,"flowdir") -pits <- pitfinder2(flowdir) +pits <- pitfinder(flowdir) diff --git a/man/watershed.Rd b/man/watershed.Rd index f2eefd71a..39574bbad 100644 --- a/man/watershed.Rd +++ b/man/watershed.Rd @@ -16,7 +16,7 @@ delineate the area covered by a catchment from a SpatRaster with flow direction } \arguments{ - \item{p}{SpatRaster with flow direction. See \code{\link{terrain}}. } + \item{x}{SpatRaster with flow direction. See \code{\link{terrain}}. } \item{pourpoint}{matrix or SpatVector with the pour point location} \item{filename}{character. Output filename} \item{...}{additional arguments for writing files as in \code{\link{writeRaster}}}