[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-sig-geo
Subject:    Re: [R-sig-Geo] Set single raster to NA where values of raster stack are NA
From:       Andrew Vitale <vitale232 () gmail ! com>
Date:       2013-04-29 5:07:50
Message-ID: CAFTcopdqfCk9pqSUWRcbp4PObHcXzpBdJnD_XZB5+g-WTH7VOw () mail ! gmail ! com
[Download RAW message or body]

Perfect!

I started down the rbind road using 'sp', but ran into the error stating I
needed unique IDs.  A simple fix, no doubt.  Thanks for your help.

-Andrew


On Sun, Apr 28, 2013 at 9:47 PM, Robert J. Hijmans <r.hijmans@gmail.com>wrote:

> Andrew,
>
> I think that a good approach might be, for your polygons:
> p <- rbind(p1, p2, p3, etc, makeUniqueIDs=TRUE)
> test <- mask(r4, p, inverse=TRUE)
>
> # but to follow your example:
> # solution 1
> s2 <- stack(r1, r2, r3, r4)
> test.a.cool <- calc(s2, function(x){ m <- apply(x, 1, function(z)
> any(is.na(z))); m[m] <- NA; m})
>
> # solution 2
> test <- overlay(r4, s, fun=function(x, y){x[apply(y, 1,
> function(z)any(is.na(z)))] <- NA; x})
>
> # much simpler, solution 3
> ss <- sum(s)
> test <- mask(r4, ss)
>
>
> Best, Robert
>
> On Sun, Apr 28, 2013 at 9:23 PM, Andrew Vitale <vitale232@gmail.com>
> wrote:
> > I have two 30m x 30m raster files from which I would like to sample
> random
> > points.  Prior to sampling, I would like to remove the clouded areas from
> > the images.  I turned to R and Hijman and van Etten's Raster package for
> > the task.
> >
> > Using the drawPoly(sp=TRUE) command, I drew in 18 different polygons.
>  The
> > function did not seem to allow 18 polygons as one sp object, so I drew
> them
> > all separately.  I then gave the polygons a proj4string matching the
> > rasters', and set them into a list.  I ran the list through a lapply
> > function to convert them to rasters (using rasterize()) with the polygon
> > areas set to NA, and the rest of the image set to 1.
> >
> > My end goal is one raster layer with the 18 areas set to NA, and the rest
> > of the extent set to 1.  I have tried stacking the list of rasterized
> > polygons, and subsetting it to set a new raster to NA in the same areas.
> >  My reproducible code is below.
> >
> >
> >     r1 <- raster(nrow=50, ncol = 50)
> >     r1[] <- 1
> >     r1[4:10,] <- NA
> >     r2 <- raster(nrow=50, ncol = 50)
> >     r2[] <- 1
> >     r2[9:15,] <- NA
> >     r3 <- raster(nrow=50, ncol = 50)
> >     r3[] <- 1
> >     r3[24:39,] <- NA
> >
> >     r4 <- raster(nrow=50, ncol = 50)
> >     r4[] <- 1
> >
> >     s <- stack(r1, r2, r3)
> >
> >     test.a.cool <- calc(s, function(x){r4[is.na(x)==1] <- NA})
> >
> > For whatever reason, the darn testacool is a blank plot, where I'm aiming
> > to have it as a raster with all values except for the NAs in the stack,
> s,
> > equal to 1.  In simple terms, a cloud mask.
> >
> > Any tips?
> >
> > Thanks.
> >
> >
> > --
> > *Andrew P. Vitale*
> > Masters Student
> > Department of Geography
> > University of Nevada, Reno
> >
> > vitale232@gmail.com
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
*Andrew P. Vitale*
Masters Student
Department of Geography
University of Nevada, Reno
(412) 915-3632
vitale232@gmail.com

	[[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic