[R-sig-Geo] Replacing values in large rasters using conditional statements
Gregovich, Dave P (DFG)
dave.gregovich at alaska.gov
Thu Feb 26 19:16:31 CET 2015
Well, This may be somewhat remedial for some, but it is a voyage of discovery for me:
It appears that using the 'overlay' function is the analogous method I was looking for. I am not absolutely positive it works for *very* large rasters.
But it does seem to work with the 5 X 10^8-cell rasters I am currently working with:
>Hello,
>I often replace values in a raster based on values of another raster, like this:
>n.row <- n.col <- 100
>a <-raster(nrows=n.row,ncols=n.col)
>b <- a
>a[]<-rnorm(ncell(a))
>b[]<-rnorm(ncell(b))
>a[b > 0] <- 1
>What would analogous code be that works for large rasters without running into vector size allocation problems?
#First method as illustrated above.
n.row <- n.col <- 1000
a <-raster(nrows=n.row,ncols=n.col)
b <- a
a[]<-rnorm(ncell(a))
b[]<-rnorm(ncell(b))
out.1 <- a
out.1[b > 0] <- 1
#second method using 'overlay'...
out.2 <- overlay(a,b,fun=function(x,y){
z <- x
z[y > 0] <- 1
return(z)
})
#check the two methods are identical
all(getValues(out.1) == getValues(out.2))
My initial indications are that 'overlay' is working really well with large, stored-on-disk rasters, and doesn't seem *too* slow
I hope this is helpful to some of you.
Thanks kindly.
__________________________________
Dave Gregovich
Research Analyst
Alaska Department of Fish and Game
Wildlife Conservation Division
Douglas, AK 99821
(907) 465-4291
dave.gregovich at alaska.gov
__________________________________
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list