1

I have different raster layers, but it is not a mosaic. I want to extract all pixel values, mean and standard deviation for each raster. But I prepared my scrip in r, and I import each raster and extract information one by one raster information. When I want to read in other efficient for, for example use stack, but show this problem

> raster <- stack('C:/Users/Dinosca/Documents/Proyectos GRASS/Camp_500')
Error in .local(.Object, ...) : 

Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : 
Cannot create a RasterLayer object from this file.  

Sorry, I have never worked with r and raster information before.

1

Solution 1

Create a loop to iterate through files:

library(raster)

raster_files <- list.files(mypath,full.names = T) #use pattern = '.tif$' or something else if you have multiple files in this folder

r_name <- list.files(mypath,full.names = F)

rList <- list() # to save raster values
statList <- list() # to save data.frame with statistics

for(i in 1:length(raster_files)){
  temp <- raster(raster_files[i])
  rList[[i]] <- values(temp) # extract values for each raster

  # name
  Name <- r_name[i]

  mx=raster::maxValue(temp)
  mn=raster::minValue(temp)
  avg=raster::cellStats(temp,'mean',na.rm=T)
  stdev=raster::cellStats(temp,'sd',na.rm=T)

  statList[[i]] <- data.frame(Name,mx,mn,avg,stdev) # create a data.frame to save statistics
}

df <- do.call(rbind.data.frame,statList) # final data.frame with all statistics

Solution 2

You can use purrr package to handle this easily:

library(raster)
library(dplyr)
library(purrr)

mypath <- 'C:/Users/Dinosca/Documents/Proyectos GRASS/Camp_500'

# To extract all pixel values (the result is a list, each slot is a raster)
list.files(mypath,full.names = T) %>% #use pattern = '.tif$' or something else if you have multiple files in this folder
  map(raster) %>% map(raster::values)

# To extract max, min, mean and sd for each raster (the result is a data.frame)
list.files(mypath,full.names = T) %>%
  map(raster) %>%
  map_df(function(x){
    data.frame(
      mx=raster::maxValue(x),
      mn=raster::minValue(x),
      avg=mean(x[],na.rm=T),
      stdev=sd(x[],na.rm=T))})
  • Using mean(x[]) negates the memory safe aspects of the raster package. While I appreciate new methods and tricky approaches, your answer obscures the basics. It would be nice to provide a clear, simple answer and then provide a plausibly faster and more efficient solution. Simply looping or using lapply on the vector of raster pointers, resulting from list.files, and then using raster::cellStats, which is memory safe, to collect statistics would be a clear approach, which is also a duplicate answer. Using purrr to replace a for loop is clever but, may really confuse a novice user. – Jeffrey Evans Sep 11 '17 at 20:19
  • I can understand his example, but i have the problem. When I want to create a raster list. R show this error: Error in .local(.Object, ...) : Error in .rasterObjectFromFile(x, objecttype = "RasterBrick", ...) : Cannot create a RasterLayer object from this file. – Dinosca Sep 11 '17 at 20:43
  • @JeffreyEvans you're right. I edited my answer adding a for() loop and replacing x[] with cellStats(). – aldo_tapia Sep 11 '17 at 21:20
  • @Dinosca did you check if in your folder are only one kind of file extension? If you have some name.tif.aux.xml or files like this will be an error – aldo_tapia Sep 11 '17 at 21:23
  • Yes, i had a problem with format. Thanks @aldo_tapia you are so friendly. But now I have another problem. But, i hope I can fix it. – Dinosca Sep 11 '17 at 23:41
1

I would suggest looking into n-layers

s <- lapply(raster_data, stack)

This thread provided the above code Importing multiple stacked raster images in R?

Get the number of layers in a Raster* object, typically used with a (multilayer) RasterStack or RasterBrick object

This is a good tutorial Image Raster Data in R - An Intro too look into as well.

  • I wrote my new code, and I am using the tutorial, but I can not read the stack function in Raster. Always show me: Error in .local(.Object, ...) : Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", : Cannot create a RasterLayer object from this file. – Dinosca Sep 11 '17 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.