1
votes

I'd like to using extract() for calculate a percentual of pixel with "1"s values inside a buffer given some coordinates in a raster (ras).

In my example:

#Packages
library(raster)

# Create a raster 
ras <- raster(ncol=1000, nrow=1000)
set.seed(0)
values(ras) <- runif(ncell(ras))
values(ras)[values(ras) > 0.5] = 1 
values(ras)[values(ras) < 0.5] = NA
ras
#class      : RasterLayer 
#dimensions : 1000, 1000, 1e+06  (nrow, ncol, ncell)
#resolution : 0.36, 0.18  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 
#source     : memory
#names      : layer 
#values     : 1, 1  (min, max)

# Create some 30 coordinates
pts<-sampleRandom(ras, size=30, xy=TRUE)
pts.df<-as.data.frame(pts)
pts.df$area<-rnorm(30, mean=10)
head(pts.df)
#        x      y layer      area
#1 -109.26 -43.65     1 10.349010
#2   93.42 -87.21     1  9.861920
#3   57.06  86.85     1  8.642071
#4 -109.98 -45.63     1 10.376485
#5  -92.34  37.89     1 10.375138
#6   19.62  21.51     1  8.963949

#Representation
plot(ras)
points(cbind(pts.df$x,pts.df$y))

#Function for extract percentual 1s
perc1<- function(x,...) {
  leng1<-length(values(x) ==1) # length of 1s pixels
  lengtotal<-length(x) # total length of pixels inside buffer
  perc<-(leng1/lengtotal)*100
  return(perc)
}

# Extract circular, 100000 units buffer
cent_max <- raster::extract(ras, # raster layer
    cbind(pts.df$x,pts.df$y),    # SPDF with centroids for buffer
    buffer = 100000,                 # buffer size
    fun=perc1,                  # what to value to extract
    df=TRUE) 

Here I need a help with a programing problem, because perc1 function doesn't work. I'd like to create a new column (percentual_1s) with extract()that a make a percentual of pixel with "1"s values. I need some direction for calculate de length()of pixels with "1" value divided for the total of pixels ("1"s values and NA) inside de each 100000 units buffer *100. My desirable output is:

#        x      y layer      area   percentual_1s
#1 -109.26 -43.65     1 10.349010   23.15
#2   93.42 -87.21     1  9.861920   45.18
#3   57.06  86.85     1  8.642071   74.32
#4 -109.98 -45.63     1 10.376485   11.56
#5  -92.34  37.89     1 10.375138   56.89
#6   19.62  21.51     1  8.963949   88.15

Please, any ideas?

1
Does your data really span the entire globe? A 20 meter buffer is less than the area of one raster cell the raster area. raster::extract will not return anything that is partially covered.at80
Bad Karma, but I change de buffer value. Thanks @at80Leprechault

1 Answers

0
votes

adapt your function for extraction as follows:

percentual_1s<- function(x,...) {
  leng1<-length(which(x==1))
  lengtotal<-length(x) 
  perc<-(leng1/lengtotal)*100
  return(perc)
}

And add "na.rm = FALSE" in the extract call:

cent_max <- extract(ras, pts[,1:2], buffer = 1000000, fun=percentual_1s, df=TRUE, na.rm = FALSE)

That gives me:

head(cent_max)
  ID    layer
1  1 49.81865
2  2 50.27545
3  3 50.03113
4  4 50.29819
5  5 50.39391
6  6 48.89556

The solution in: https://stat.ethz.ch/pipermail/r-sig-geo/2020-September/028437.html