Search code examples
rrasterrgdal

Error when importing raster file


I transformed a shapefile into a raster file using rasterize R function, and saved this raster with the writeRaster function (.bil and .asc). Now, I can't import the new file, returning this error:

Error in .local(.Object, ...) : 
  EHdr driver does not support 64 NBITS value.

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

My script:

library(maptools)
library(raster)
# shapefile from Natural Earth website 
a <- readShapeSpatial('ne_10m_roads.shp') 
e <- extent( -180, 180, -60, 90 ) 
r <- raster(e, nrow=3600, ncol=8640) 
s2r <- rasterize(a,r)

I have using a notebook with Ubuntu 14.10 - 64bit, and 4Gb RAM, RStudio software and R version 3.1.1:

R version 3.1.1 (2014-07-10) 
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

*After format my computer, the new file was correctly open only before install the dependences of rgdal via terminal.

** All the directories were correctly choosen, and the file is found into the directory.


Solution

  • Why do you use these formats? BIL is obsolete, ASCII inefficient. It is better to use GeoTIFF or (if you are working within the realm of R/raster) GRD (the default format).

    I suppose this error occurs with the 'BIL' file, not with the ascii file. When writing the 'BIL' file you may want to specify writeRaster argument datatype='INT2S', datatype='FLT4S' or any other datatype that is not 64 bits ('FLT8S'). You normally do not need to have that much precision (unless you want to store very large integer numbers exactly). You may be able to read the file with:

    raster('file.bil', native=TRUE)