[R-sig-Geo] continuous raster to binary
Tim Howard
tghoward at gw.dec.state.ny.us
Thu Jun 14 18:10:45 CEST 2012
What a fabulous collection of answers!
For my bigger grids that can't reside in memory, I think I need to use calc in regards to Thierry's and Matt's approach, like this:
func <- function(x){x > 3.5} #still need to add "as
r <- calc(r, fun=func, filename = ...)
So, following Rafael's note that reclass was faster than subs alone, here are timings on real data comparing reclass to calc.
> func <- function(x){x > ctpt}
> system.time(rasCtc <- calc(ras, fun=func, filename=out1, format = "GTiff", datatype = "INT1U"))
user system elapsed
119.16 28.00 147.37
> system.time(rasCt <- reclass(ras, rccutpt, filename=out2, format = "GTiff", datatype = "INT1U"))
user system elapsed
119.85 28.38 148.44
They are very close, and perhaps not even significantly different, but calc may be edging out reclass by a small amount. Brilliant!
Thanks everyone, for the help.
Best,
Tim
>>> Matthew Landis <landis at isciences.com> 6/14/2012 11:37 AM >>>
In terms of Tim's example:
plot(r)
r[] <- as.numeric(r[] > 3.5)
plot(r)
works for me. Made only a small change from Thierry's elegant example
to return numeric instead of boolean.
M
On 6/14/2012 9:56 AM, ONKELINX, Thierry wrote:
> How about
> rs<- r> 0.35
> or
> rs<-(r> 0.35) * 1
> untested!
>
> Best regards,
> Thierry
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
> team Biometrie& Kwaliteitszorg / team Biometrics& Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
> + 32 2 525 02 51
> + 32 54 43 61 85
> Thierry.Onkelinx at inbo.be<mailto:Thierry.Onkelinx at inbo.be>
> www.inbo.be<http://www.inbo.be/>
>
> To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> Van: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] Namens Tim Howard
> Verzonden: donderdag 14 juni 2012 15:02
> Aan: r-sig-geo at r-project.org
> Onderwerp: [R-sig-Geo] continuous raster to binary
>
> All,
> I'd like to convert a continuous raster to binary (0, 1). Here's what I've come up with
>
> library(raster)
>
> #build the sample raster
> r<- raster(ncol=100, nrow=100)
> r[]<- round(runif(ncell(r)) * 10)
> #set up a cut point, cut it
> ctpts<- c(0,3.5,10)
> rc<- cut(r,breaks=ctpts)
>
> #since the result from cut was a raster with values c(1,2), use subs to change to c(0,1)
> subdf<- data.frame(from=c(1,2), to=c(0,1))
> rs<- subs(rc, subdf, subsWithNA=TRUE)
> rs
>
>
> My questions:
> 1. Is there a more efficient way? (I'll be doing this to many large rasters)
> 2. Using 32-bit R, subs bails with a memory error, even if a file is specified (cut runs fine). I can move to 64-bit R on a different machine, but it would be nice to be able to run it on this 32-bit machine. Any suggestions?
>
> Thanks in advance,
> Tim
> ~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20120614/6c2413e5/attachment.html>
More information about the R-sig-Geo
mailing list