[R-sig-Geo] Antw: Re: use Buffer for calculation on raster

Alexander Arpaci alexander.arpaci at boku.ac.at
Sat Jul 23 00:43:55 CEST 2011


Thank you very much for that quick reply and indeed I was too complicated...
your approach works great!But things are more complicated... I have different buffer width depending on the data accuracy

thats why I used primarily a loop. Now I run it again in a loop for creating the buffers and extracting the pixel values; its slow but it works. but the next problem is some of the buffer are extending over the raster extent so they yield NA values  
when I run than a sapply function to calculate the mean I get warnings and all values are NA. I seem not to be able to set a na.rm into the sapply function..


my code after your recommendation with result all bufferwidth equal even when I use the variable where different accuracy values are stored
bufs=gBuffer(wb_new,byid=TRUE,id=NULL,width=wb_new$DS_KO,capStyle="ROUND")
try<-extract(sc,bufs)
m<-sapply(try,FUN=mean)
plot(sc)
plot(bufs,add=TRUE)



my new loop code
for(i in 1:98){bufers[[i]]=gBuffer(wb_new[i,],byid=TRUE,id=NULL,width=wb_new$DS_KO[i],capStyle="ROUND")}

ext<-list()
for (i in 1:98){
ext[[i]]<- extract(sc,bufers[[i]])

}
mean_bufers<-sapply(ext,FUN=mean,na.rm=TRUE)
Es gab 50 oder mehr Warnungen (Anzeige der ersten 50 mit warnings())
mean_bufers
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[76] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 
any suggestions

thanks
alex



MSc. Alexander Arpaci
Assistant researcher

Department of Forest - and Soil Sciences
Institute of Silviculture
Tel. +43-1-47654-4081
e-mail: alexander.arpaci at boku.ac.at
www.wabo.boku.ac.at 
>>> Robert Hijmans  22.07.11 19.03 Uhr >>>
> I have created a list of buffer "spatialpolygons" which are stored in a
list (thx to Barry) but what I want
> to do, is now use the area of each buffer to calculate an mean from  a set
> of pixel values falling into
> that buffer. I thought the easiest way is to create shape files and use
> them with the raster package but
> I am struggeling with creating  shape files from that list
> Maybe I am thinking too complicated ?

How about this:

wb_new <- readShapeSpatial("G:/Students/Nelson/wb_studyarea.shp")#spatial
points used for the buffers 

library(rgeos)
# no loops!
bufs = gBuffer(wb_new, byid=TRUE, id=NULL, width=500, capStyle="ROUND") 

library(raster)
x <- extract(sc, bufs)

x should be a list with a length equal to the number of points in wb_new

Robert


--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/use-Buffer-for-calculation-on-raster-tp6610936p6611183.html
Sent from the R-sig-geo mailing list archive at Nabble.com.

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list