0
votes

the problem i am trying to solve is that i want to extract the value of pixels of multiple rasters using a shapefile containing multiple point locations , for each point i would like to extract the value of multiple rasters "time series" , and also loop through a list of buffer radiuses to be input in the extract function , the end result i want is a csv for each station for each buffer

as far i did manage to write a program under R with the raster library , my code is composed in two loops , the first loop goes into each raster and extract the pixel value corresponding to the locations of multiple locations in a shapefile , the second one contains a list of radiuses to be looped to extract the means , max, and min of each pixel radiuses

but the code is too loose at this point without effective integration of the radius loop

library(raster)
library(rgdal)
library(stringr)
library(ggplot2)
library(lubridate)

raster_files <- list.files('J:/nvc',pattern="*.tif",full.names = T)
r_name<- list.files(path='J:/nvc',pattern="*.tif",full.names = F)
shape<-readOGR('J:/nvc/locations/locations.shp')
radius <- read.csv('J:/nvc/radius.csv')
rList <- list() 
statList <- list() 


for (j in 1:nrow(radius)){
  for(i in 1:length(raster_files)){
    ras <- raster(raster_files[i])
    stack <- stack(ras)
    crs(stack) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84"
    mean <- raster::extract(stack,shape,buffer=j,fun=mean,df=TRUE)
    min <- raster::extract(stack,shape,buffer=j,fun=max,df=TRUE)
    max <- raster::extract(stack,shape,buffer=j,fun=min,df=TRUE)
    Name <- r_name[i]
    
    statList[[i]] <- data.frame(max)
    df <- do.call(rbind.data.frame,statList)
  }
}

N.B : i am just naming 3 locations to try , alpha bravo and charlie

and for the radiuses it's 1,2,4,6,8,10,20 km

the rasters are named 1h-2020042601 , 1h-2020042602,1h-2020042603 ..... e.g : 1 raster for each hour

1

1 Answers

0
votes

When asking a R question, always include a minimal, reproducible, self-contained example, as I do below. You should not need to loop over the rasters. Make a RasterStack

library(raster)
#raster_files <- list.files('J:/nvc',pattern="*.tif",full.names = T)
raster_files <- system.file("external/rlogo.grd", package="raster")
stack <- stack(raster_files)
points <- cbind(x=c(48, 53, 7), y=c(63, 43, 28))
bufs <- c(5, 10)     

One way to loop is with lapply

x <- lapply(bufs, function(b) extract(stack, points, buffer=b))

That returns a list of lists. In this case for

# 2 buffers
length(x)
#[1] 2
# 3 points
sapply(x, length)
#[1] 3 3

Compute the desired statistics

r <- rapply(x, function(i) c(min(i, na.rm=TRUE), mean(i, na.rm=TRUE), max(i, na.rm=TRUE)))

And put it together nicely

r <- matrix(r, ncol=3, byrow=TRUE)
colnames(r) <- c("min", "mean", "max")
r <- cbind(buf=rep(bufs, each=nrow(points)),
    point=rep(1:nrow(points), length(bufs)), r)
r
#     buf point min     mean max
#[1,]   5     1   0 119.4375 255
#[2,]   5     2   0 132.6292 255
#[3,]   5     3  81 182.5958 255
#[4,]  10     1   0 156.1361 255
#[5,]  10     2   0 154.1814 255
#[6,]  10     3  74 189.7354 255