Search code examples
rr-rasterrgdal

Error with CRS argument while reprojecting


I'm trying to iterate multiple rasters (+500) in a for loop but I'm facing some problems.

First I want to reproject them from CRS EPSG:4326 to CRS EPSG: 32614, then resample them by using a mask raster which has a smaller resolution as well as extension and finally writing a result raster for each raster in the working directory, but I've been obtaining the following error message regarding the CRS argument:

Error in CRS(x) : PROJ4 argument-value pairs must begin with +: E:\Proyecto PM2.5\2_PM_2.5_Processing\Test/AOD_MOD_CDTDB_April_2016.tif

I took a look at multiple posts here, but I couldn't go over this problem. Below is my code, any help will be really appreciated from this R beginner

#find all tifs in your directory
dir<-"E:\\Proyecto PM2.5\\2_PM_2.5_Processing\\Test"

#get a list of all files with .tif in the name in the directory
files<-list.files(path=dir, pattern='.tif', full.names = TRUE)

#raster with the expected characteristics: extension, cellsize, number of pixels
r_ref <- raster("E:\\Proyecto PM2.5\\3_PM_2.5_Entrega\\temporal\\Raster_C.tif")

for (file in files){
  name <- file
  projectRaster(name,crs="+init=epsg:32614")
  resample(file,r_ref,method="ngb")
  savename<-sub("ZMVM",name,basename(file))
  writeRaster(r,file=savename,)
}

Solution

  • You do

    for (file in files){
       name <- file
       projectRaster(name,crs="+init=epsg:32614")
    

    So name is the same as file (why do you make a copy?) --- a filename. You ask projectRaster to project a character string (file name). What you intended is surely something like this

    for (file in files){
       r <- raster(file)
       projectRaster(r, crs="+init=epsg:32614")