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

Watershed Analysis Methods #1477

Merged
merged 48 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
36f9333
Merge pull request #1 from rspatial/master
ecor Jul 2, 2021
87ccbc1
Merge pull request #2 from rspatial/master
ecor Dec 3, 2021
644cbf9
watershed2 method
ecor Dec 3, 2021
7c9c2db
watershed2 method - merged after confict
ecor Dec 3, 2021
cdca9d2
Merge pull request #3 from rspatial/master
ecor Jul 26, 2022
4699dde
aligne with Rspatial
ecor Jul 26, 2022
58b652f
Merge pull request #4 from rspatial/master
ecor Oct 26, 2022
3a4c0c9
Merge pull request #5 from rspatial/master
ecor Oct 26, 2023
9bbd0aa
20231026
ecor Oct 26, 2023
62a5471
20231026
ecor Oct 26, 2023
ba1f73e
Merge pull request #6 from rspatial/master
ecor Oct 30, 2023
af2a2ca
Agenerics.R
ecor Oct 30, 2023
3c6c68d
Merge branch 'master' of https://github.com/ecor/terra
ecor Oct 30, 2023
7819af0
updates
ecor Oct 30, 2023
c6e944e
Merge pull request #7 from rspatial/master
ecor Nov 9, 2023
c4eb1b5
Merge branch 'master' of https://github.com/ecor/terra
ecor Nov 9, 2023
d5f45a8
Merge pull request #8 from rspatial/master
ecor Nov 20, 2023
19519b2
flowaccu2 implementation
ecor Nov 20, 2023
eba2f1d
Merge branch 'master' of https://github.com/ecor/terra
ecor Nov 20, 2023
85f1117
Merge pull request #9 from rspatial/master
ecor Nov 29, 2023
864ee6c
Merge branch 'master' of https://github.com/ecor/terra
ecor Nov 29, 2023
8042cce
flow accumulation added
ecor Nov 29, 2023
048f5bf
Merge pull request #10 from rspatial/master
ecor Nov 30, 2023
ebb474e
Merge pull request #11 from rspatial/master
ecor Dec 6, 2023
ddf3379
Merge pull request #12 from rspatial/master
ecor Dec 11, 2023
088a9c1
updates - remove large file .RData
ecor Dec 12, 2023
632c8d5
updates - remove large file .RData
ecor Dec 12, 2023
b2aa007
updates - remove large file .RData
ecor Dec 12, 2023
297c8c7
Merge pull request #13 from rspatial/master
ecor Dec 12, 2023
182b611
Merge pull request #14 from rspatial/master
ecor Dec 22, 2023
14b9afe
Merge pull request #15 from rspatial/master
ecor Jan 11, 2024
d495aef
Merge pull request #16 from rspatial/master
ecor Feb 6, 2024
27c1ecd
replacing cpp with ptr in watershjed method
ecor Feb 7, 2024
8e9497f
Merge pull request #17 from rspatial/master
ecor Feb 7, 2024
055d9b2
Merge pull request #18 from rspatial/master
ecor Feb 14, 2024
8d0d695
documentation update (watershed functions)
ecor Feb 15, 2024
bf21da5
Merge pull request #19 from rspatial/master
ecor Mar 20, 2024
fc12f55
Doc update (watershed2)
ecor Mar 20, 2024
035b9a5
Merge pull request #20 from rspatial/master
ecor Apr 3, 2024
3d02c13
modified help page at watershed2
ecor Apr 10, 2024
289dc30
adding tests for watershed terrain analysis
ecor Apr 11, 2024
2fdef4d
Watershed analysis - watershed2 example update
ecor Apr 11, 2024
98d8bf0
fixing documentation in NIPD2
ecor Apr 12, 2024
943c832
fixing documentation in NIPD2 and flowAccu2*
ecor Apr 12, 2024
d87f7f5
flowaccu terrain option added
ecor May 2, 2024
d2e52fc
flowaccu terrain option added
ecor May 2, 2024
929d165
Merge pull request #21 from rspatial/master
ecor May 6, 2024
870d957
Merge pull request #22 from rspatial/master
ecor May 22, 2024
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
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ README.md
^\.github$
^.*\.Rproj$
^\.Rproj\.user$

^_pkgdown\.yml$
^docs$
^pkgdown$

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ src/Makevars.ucrt.rej
*.dll
*.zip
.Rproj.user

af_acc*
.Rbuildignore
.RData
revdep/
python/
cmdapp/terra
Expand Down
21 changes: 21 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,24 @@ S3method(str, SpatGraticule)

export(add_box, add_legend, add_grid, focalMat, extractAlong, gdal, getGDALconfig, graticule, halo, setGDALconfig, map.pal, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers, vrt_tiles, names, round)

## EC 20210702 ecor
exportMethods(watershed2)
## END EC 20210702 ecor
## EC 20220809
exportMethods(pitfinder2)
## END EC 20220809 ecor

## EC 20231031 ecor
exportMethods(NIDP2)
## END EC 20231031

## EC 20231031 ecor
exportMethods(flowAccu2)
## END EC 20231031

## EC 20231114 ecor
exportMethods(flowAccu2_weight)
## END EC 2023114



3 changes: 3 additions & 0 deletions R/Agenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,6 @@ if (!isGeneric("xmax<-")) { setGeneric("xmax<-", function(x, ..., value) standar
if (!isGeneric("ymin<-")) { setGeneric("ymin<-", function(x, ..., value) standardGeneric("ymin<-"))}
if (!isGeneric("ymax<-")) { setGeneric("ymax<-", function(x, ..., value) standardGeneric("ymax<-"))}
if (!isGeneric("zoom")) {setGeneric("zoom", function(x, ...)standardGeneric("zoom"))}



13 changes: 13 additions & 0 deletions R/Agenerics_watershed2_extesion.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

## ADDED BY ecor

if (!isGeneric("watershed2")) {setGeneric("watershed2", function(p,pp_offset,...) standardGeneric("watershed2"))}

if (!isGeneric("pitfinder2")) {setGeneric("pitfinder2", function(p, ...) standardGeneric("pitfinder2"))}


if (!isGeneric("NIDP2")) {setGeneric("NIDP2", function(p, ...) standardGeneric("NIDP2"))}

if (!isGeneric("flowAccu2")) {setGeneric("flowAccu2", function(p, ...) standardGeneric("flowAccu2"))}

if (!isGeneric("flowAccu2_weight")) {setGeneric("flowAccu2_weight", function(p,weight,...) standardGeneric("flowAccu2_weight"))}
38 changes: 33 additions & 5 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,17 @@ setMethod("t", signature(x="SpatVector"),

setMethod("terrain", signature(x="SpatRaster"),
function(x, v="slope", neighbors=8, unit="degrees", filename="", ...) {
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")

if (v=="flowacc") {
flowAccu2(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")
}
}
)

Expand Down Expand Up @@ -1239,3 +1245,25 @@ setMethod("sort", signature(x="data.frame"),
x[i, ]
}
)






















89 changes: 89 additions & 0 deletions R/generics_watershed2_extension.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Author: Emanuele Cordano
# Date : October 2023
# Version 1.0
# License GPL v3

setMethod("watershed2", signature(p="SpatRaster",pp_offset="integer"),
function(p,pp_offset,filename="", ...) {


opt <- spatOptions(filename, ...)
## p@ptr <- p@ptr$watershed2(as.integer(pp_offset-1),opt)
p@ptr <- p@ptr$watershed2(as.integer(pp_offset-1),opt)
messages(p, "watershed2") ## EC 20210318
return(p)
## p@ptr <- uu
##messages(p, "watershed2")
}

)

#### END EC 20210702

#### EC 20220809
setMethod("pitfinder2", signature(p="SpatRaster"),
function(p,filename="", ...) {

opt <- spatOptions(filename, ...)
# p@ptr <- p@ptr$pitfinder2(opt)# EC 20231026
p@ptr <- p@ptr$pitfinder2(opt)
messages(p, "pitfinder2") ## EC 20210318
return(p)
## p@ptr <- uu
##messages(p, "watershed2")
}

)
#### END EC 20220809


#### EC 20231031
setMethod("NIDP2", signature(p="SpatRaster"),
function(p,filename="", ...) {

opt <- spatOptions(filename, ...)

p@ptr <- p@ptr$NIDP2(opt)
messages(p, "NIDP2") ## EC 20231031
return(p)

}

)
#### END EC 20231031

#### EC 20231104
setMethod("flowAccu2", signature(p="SpatRaster"),
function(p,filename="", ...) {

opt <- spatOptions(filename, ...)

p@ptr <- p@ptr$flowAccu2(opt)
messages(p, "flowAccu2") ## EC 20231104
return(p)

}

)
#### END EC 20231104


#### EC 2023114
setMethod("flowAccu2_weight", signature(p="SpatRaster",weight="SpatRaster"),
function(p,weight,filename="", ...) {

opt <- terra:::spatOptions(filename,...)
p@ptr <- p@ptr$flowAccu2_weight(weight@ptr,opt)
messages(p, "flowAccu2_weight") ## EC 20231104
return(p)

}

)
#### END EC 20231104






Empty file modified cmdapp/compile.sh
100755 → 100644
Empty file.
Empty file modified cmdapp/test.sh
100755 → 100644
Empty file.
Binary file added inst/ex/elev_vinschgau.tif
Binary file not shown.
34 changes: 34 additions & 0 deletions inst/tinytest/test_flowacc2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
###
library(terra)
#> terra 1.7.29

elev <- c(78, 72, 69, 71, 58, 49,
74, 67, 56, 49, 46, 50,
69, 53, 44, 37, 38, 48,
64, 58, 55, 22, 31, 24,
68, 61, 47, 21, 16, 19,
74, 53, 34, 12, 11, 12) |> matrix(ncol = 6, byrow = TRUE) |> rast()


flowdir0 <- c(002,002,002,004,004,008,
002,002,002,004,004,008,
001,001,002,004,008,004,
128,128,001,002,004,008,
002,002,001,004,004,004,
001,001,001,001,000,016) |> matrix(ncol = 6, byrow = TRUE) |> rast()

flowacc0 <- c(001,001,001,001,001,001,
001,002,002,003,003,001,
001,004,008,006,005,001,
001,001,001,021,001,002,
001,001,001,002,025,001,
001,003,005,008,036,002) |> matrix(ncol = 6, byrow = TRUE) |> rast()



flowdir1 <- terrain(elev,"flowdir")
flowacc1 <- flowAccu2(flowdir1)

result <- (flowacc1==flowacc0) & (flowdir1==flowdir0)

expect_equal(all(result[]),TRUE)
53 changes: 53 additions & 0 deletions inst/tinytest/test_pitfinder2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

## Creation of a Digital Elevation Model

elev <- array(NA,c(9,9))
dx <- 1
dy <- 1
for (r in 1:nrow(elev)) {
x <- (r-5)*dx
for (c in 1:ncol(elev)) {

y <- (c-5)*dy
elev[r,c] <- 10+5*(x^2+y^2)
}
}

elev <- cbind(elev,elev)
elev <- rbind(elev,elev)
elev <- rast(elev)





## Flow Directions

flowdir<- terrain(elev,v="flowdir")


#t(array(flowdir[],rev(dim(flowdir)[1:2])))

## Pit Detect

pits1 <- pitfinder2(flowdir)

xypit <- as.data.frame(pits1,xy=TRUE)
names(xypit) <- c("x","y","pit")
xypit$icell <- 1:nrow(xypit)
xypit[which(xypit$pit!=0),]


xypit2 <- xypit[which(xypit$pit!=0),]
# > xypit2
# x y pit icell
# 77 4.5 13.5 1 77
# 86 13.5 13.5 2 86
# 239 4.5 4.5 3 239
# 248 13.5 4.5 4 248
pits0 <- elev*0
pits0[c(77,86,239,248)] <- c(1,2,3,4)

result <- (pits1==pits0)

expect_equal(all(result[]),TRUE)
101 changes: 101 additions & 0 deletions man/NIPD2.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
\docType{methods}
\name{NIDP2}

\alias{NIDP2}
\alias{NIDP2,SpatRaster-method}


\title{Number of immediate adjacent cells flowing into each cell (Watershed Analysis)}

\description{
It computes the number of immediate adjacent cells flowing into each cell (Watershed Analysis)
}

\usage{
\S4method{NIDP2}{SpatRaster}(p,filename="",...)
}

\arguments{
\item{p}{Flow Direction raster map, a \code{\link{SpatRaster-class}} object, see also \code{\link{terrain}}. }
\item{filename,...}{list. Options for writing files as in \code{\link{writeRaster}}}
}

\value{

A \code{\link{SpatRaster-class}} (raster) map containing the value for each cell.
}

\details{
NDIP is computed to preliminarly to compute Flow Accumulation area in the algorithm proposed by Zhouo at al,2019.


}

\seealso{\code{\link{flowAccu2}},\code{\link{flowAccu2_weight}}}
\author{

Emanuele Cordano (implementation of the algorithm within the package),Guiyun Zhou, Wenyan Dong and Hongqiang Wei
}

\examples{


elev1 <- array(NA,c(9,9))
elev2 <- elev1
dx <- 1
dy <- 1
for (r in 1:nrow(elev1)) {
y <- (r-5)*dx
for (c in 1:ncol(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)
}
}


## Elevation Raster Maps
elev1 <- rast(elev1)
elev2 <- rast(elev2)

t(array(elev1[],rev(dim(elev1)[1:2])))
t(array(elev2[],rev(dim(elev2)[1:2])))

plot(elev1)
plot(elev2)

## Flow Direction Raster Maps
flowdir1<- terrain(elev1,v="flowdir")
flowdir2<- terrain(elev2,v="flowdir")


t(array(flowdir1[],rev(dim(flowdir1)[1:2])))
t(array(flowdir2[],rev(dim(flowdir2)[1:2])))

plot(flowdir1)
plot(flowdir2)

##
nidp1 <- NIDP2((flowdir1))
nidp2 <- NIDP2((flowdir2))

t(array(nidp1[],rev(dim(nidp1)[1:2])))
t(array(nidp2[],rev(dim(nidp2)[1:2])))

plot(nidp1)
plot(nidp2)

}




\references{
Zhou, G., Wei, H. & Fu, S. A fast and simple algorithm for calculating flow accumulation matrices from raster digital elevation. Front. Earth Sci. 13, 317–326 (2019). https://doi.org/10.1007/s11707-018-0725-9
\url{https://link.springer.com/article/10.1007/s11707-018-0725-9}
}



\keyword{spatial}
Loading
Loading