5

I'm getting a weird error when trying to read a raster into R.

>r <- raster('myraster.tif')
Error in .local(.Object, ...) : 
  TIFFReadDirectory:Failed to read directory at offset 2606110

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

It is definitely reading from the correct location, as when I try to read in something that does not exist, I get a completely different error:

> r <- raster('thisrasterdoesnotexist.tif')
Error in .local(.Object, ...) : 
  `C:\Users\Documents\thisrasterdoesnotexist.tif' does not exist in the file system,
and is not recognised as a supported dataset name.


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

I was reading and writing rasters from this directory just a few minutes ago. I've tried restarting R and my computer without success.

So the rasters that are doing this are ones that I recently downloaded using the function download.file(). Other rasters are reading in fine. When I used download.file() I did it with just arguments url and output filename. Am I missing something about how do download tiffs from the internet?

Also, my session info:

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rgdal_0.9-1   raster_2.3-24 sp_1.1-0     

loaded via a namespace (and not attached):
[1] grid_3.1.1      lattice_0.20-29 ncdf_1.6.8      tools_3.1.1    
  • Is this a single or multi-band raster? – Aaron May 19 '15 at 14:58
  • Just a single band tiff – Amadou Kone May 19 '15 at 15:16
  • Try using the flag method="auto" in the download.file function. – Jeffrey Evans Jun 9 '15 at 17:06
  • Out of curiosity, are you changing the name of the file extension when you download the file? Can you please show the syntax of download.file() that you are using? The problem may very well be there. Have you tried a manual download of the tif? Perhaps it is simply corrupted in storage or during download. – Jeffrey Evans Jun 9 '15 at 20:56
2

Clearly the file is corrupted. Try downloading it 'by hand' and see if it is OK. If so, try download.file(, mode="wb")

0

Try the following approach:

require(raster)

# Create raster layer
r = raster("C:/path/to/your/image.tif")

# Inspect raster layer
class(r)

> class(r)
[1] "RasterLayer"
attr(,"package")
[1] "raster"
  • If you set your working directory, using "setwd", it is not necessary to explicitly define the path in the raster function. – Jeffrey Evans May 19 '15 at 16:17
  • I should have mentioned that I already used setwd(), so including the full url doesn't do anything different and gives the same results. – Amadou Kone May 19 '15 at 16:36
0

It would help if you provided details on OS, R and package versions. You can use sessionInfo() to collect this information.

I recall a similar issue associated with 16bit JPEG's coming up years ago, but I believe that it has been fixed. Check the validity of your raster with rgdal directly. Using the "GDALinfo" function you can do this without reading the raster into R.

library(rgdal)
GDALinfo(system.file("external/test.ag", package="sp")[1])

If you end up using rgdal's "readGDAL" function to read your raster, the resulting object will be a sp SpatialPixelsDataFrame and not a raster object. This also means that the entire raster is read into memory so, it is not ideal. If this route does work, you can coerce the resulting object to a raster class using the raster function.

Update: Some of your packages are dated. Current versions as of 05/19/2015: raster_2.3-40, rgdal_0.9-2, sp_1.1-0. To update these packages you can run:

update.packages(repos="http://cran.us.r-project.org", ask=FALSE) 

Alternately, you can use the R GUI under the Packages menu. In either case, under windows, make sure that you are running R as administrator.

Whereas this may not fix your issues per se, sometimes updating packages will resolve certain issues.

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.