[R-sig-Geo] bug writeRaster to ENVI

Robert Hijmans r.hijmans at gmail.com
Tue Feb 15 08:00:21 CET 2011



> I discovered a strange behaviour of the function writeRaster in ther 
> raster package. Having a *.img or *.tiff file with a projected extend 
> object like this, 
> ...
> alway causes a wrong projection and associated coordinates when writing 
> to a envi file with the function call: 

Carsten, 

See the below for a self contained illustration of the problem, and for a
work-around. 

There appears to be a problem in gdal, or perhaps rgdal. This needs to be
looked at in more detail.

> library(raster)
Loading required package: sp
raster version 1.7-37 (7-February-2011)
> 
> r <- raster(nrow=10, ncol=10)
> r[] <- 1:ncell(r)
> 
> extent(r)
class       : Extent 
xmin        : -180 
xmax        : 180 
ymin        : -90 
ymax        : 90 
> r1 <- writeRaster(r, filename='test1.envi', format='ENVI', overwrite=TRUE)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23
Path to GDAL shared files: C:/soft/R/R-2.12.1/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009
Path to PROJ.4 shared files: C:/soft/R/R-2.12.1/library/rgdal/proj
> # not good
> extent(r1)
class       : Extent 
xmin        : 0 
xmax        : 10 
ymin        : -10 
ymax        : 0 
> 
> 
> # the problem seems to be with gdal
> # as this also occurs with writeGDAL
> sp <- as(r, 'SpatialGridDataFrame')
> bbox(sp)
    min max
s1 -180 180
s2  -90  90
> writeGDAL(sp, 'test2.envi', driver='ENVI')
> y <- readGDAL('test2.envi')
test2.envi has GDAL driver ENVI 
and has 10 rows and 10 columns
> # not good:
> bbox(y)
  min max
x   0  10
y -10   0
> 
> 
> #work around
> extent(r1) <- extent(r)
> hdr(r1, 'ENVI')
> x <- raster('test1.envi')
> # OK now:
> extent(x)
class       : Extent 
xmin        : -180 
xmax        : 180 
ymin        : -90 
ymax        : 90 
> 
> 

Hope this helps, Robert
-- 
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/bug-writeRaster-to-ENVI-tp6023256p6026499.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list