1

I am trying to load raster files stored in AWS s3 bucket and I've managed to read them as data frame. How can I change them to rasterlayer objects?

library(aws.3)
library(raster)
library(rgdal)

#set aws s3 credentials 
Sys.setenv("AWS_ACCESS_KEY_ID" ="XXXXXXXXXXXXXXXX",
       "AWS_SECRET_ACCESS_KEY" ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
       "AWS_DEFAULT_REGION"="us-east-1")

#read objects
r_files<-get_object_df(bucket="my-bucket")

#check dataset
str(r_files)  

 > 'data.frame':    8 obs. of  8 variables:
   $ Key              : chr  "niger_LST_day_2004.tif" 
  "niger_LST_night_2004.tif" "niger_black_albedo_2004.tif" 
   "niger_builtup_2000.tif" ...
   $ LastModified     : chr  "2018-06-05T21:07:37.000Z" "2018-06- 
     05T21:07:37.000Z" "2018-06-05T21:07:37.000Z" "2018-06-05T21:07:37.000Z" ...
   $ ETag             : chr  "\"f0659cc4d1f606acb571a3c510cff3ce\"" 
 "\"cee34ed55e0293957455e1576d0cba3f\"" "\"bdb26dfaac36b394c29aee55dde1834e\"" "\"5b7df07a486678b76daf5e1f08aeea0a\"" ...
   $ Size             : chr  "4615511" "4504193" "5581624" "490477" ...

str(r_files)

I followed the suggestion posted on Creating raster layer from data frame in R? changed the data frame to matrix and I get this.

# change to matrix 
r_files_2 <-as.matrix(r_files)

str(r_files_2)
> chr [1:8, 1:8] "niger_LST_day_2004.tif" "niger_LST_night_2004.tif" ...
- attr(*, "dimnames")=List of 2
  ..$ : chr [1:8] "1" "2" "3" "4" ...
  ..$ : chr [1:8] "Key" "LastModified" "ETag" "Size" ...

and to create raster objects I will go with:

for (i in r_files_2){assign(unlist(strsplit(i, "[.]"))[1],raster(i))}

But, the following error shows up

> Error in .local(.Object, ...) : 
  `/home/rstudio/aws_s3_testing/niger_LST_day_2004.tif' does not exist in the file system,
 and is not recognised as a supported dataset name.

What would be the best way to save each file to my home directory("aw_s3_testing") as tiff file?

Your Answer

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

Browse other questions tagged or ask your own question.