[R-sig-Geo] bug writeRaster to ENVI
Roger Bivand
Roger.Bivand at nhh.no
Tue Feb 15 08:56:28 CET 2011
On Mon, 14 Feb 2011, Robert Hijmans wrote:
>
>
>> 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.
It is not present in GDAL 1.8.0, but is present in 1.7.2. I think it is:
http://trac.osgeo.org/gdal/ticket/3751
so should not be present in GDAL 1.7.3 - could anyone running GDAL 1.7.3
or 1.8.0 please check for completeness, and report to me offlist? The GDAL
ENVI driver in 1.7.2 and earlier was defective.
I'll try to find time to move the MSYS-built Windows GDAL binaries to
1.8.0.
Roger
>
>> 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
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list