Hello,

Our research group is currently trying to decide on a standard data format
for our modeled climate data (average monthly floating point values for temp
and precip from now until 2099).  Our group utilizes R as well as ArcGIS
processing quite a bit, so our goal is to have one format that can be
directly utilized in both those programs, without having to convert into
another format, which duplicates data.

Currently our data is in ASCII txt format.  Another downfall of this format
is the large storage space it takes up on disk as opposed to binary formats.

My thought was to use a GeoTiff format.  I know ArcGIS can directly
geoprocess that format, and it seems that it may be possible within R as
well.  Looking through some of your documentation, our local R guru seems to
understand how to read and write from and to geotiffs, but he's not sure how
to actually process in that format.  From the ASCII format, we have read
that data into a matrix within R, then R handles all processing from within
the matrix.

Do you know of a way to read a GeoTiff into a matrix format with R?

Thanks,
Tom


Dear Tom,

There are many ways, and here are some ideas:

If "file.tif" is a geotiff file, you can do

library(raster)
f <- "file.tif"
r <- raster(f)
plot(r)

To get the values of 'r' you can get them as a vector:

v = getValues(r)

or as a matrix:

v = getValues(r, format='matrix')


A vector is more practical than a matrix in most cases (except for
neighborhood operations).
Also, I wonder if you actually need to extract the values. For raster
data processing, it could be advantageous to directly operate on r,
like:

s <- sqrt(r) * 2 - 10
s <- s / r
plot( s )
s <- writeRaster(s, filename='new.tif')

I agree that you would want to stay away from ascii (too big, too slow).

For climate data you could also use netCDF

x = raster("file.nc")  # etc, as above.

The 'raster' package has a vignette (and help pages) with more  examples

Best, Robert




Since you were so helpful, I figured I'd try once more...

Just wondering if you know how to write out compressed LZW TIFFs from R?

We've been using the rgdal and raster packages to read and write out
GeoTIFFs using the writeRaster command, but we like to use compressed TIFFs
to save on disk space.  Each time we send them through R they balloon in
size again and we need to re-compress them outside of R.

The LZW compression is about 10:1 in size compared to the uncompressed TIFF.

Thanks!
Tom



Dear Tom,

That was a useful question because there is a way to do that, namely
adding the argument ' options="COMPRESS=LZW" ' to writeRaster. But
this was not working. I fixed it in raster version 1.4-7, which should
be available from CRAN by next Monday.

However, In that version you won't need to use the option as I have
made it the default. To keep files small it is also important to
specify that your values are integer (or byte) type, if they are. See
example below.

> library(raster)
Loading required package: sp
raster version 1.4-7 (24-August-2010)
> r = raster()
> r[] = 1:ncell(r)
> rr = writeRaster(r, 'test1.tif', overwrite=TRUE, options="COMPRESS=NONE")
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.7.1, released 2010/02/08
Path to GDAL shared files: C:/soft/R/R-2.11.1-x64/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.11.1-x64/library/rgdal/proj
> rr = writeRaster(r, 'test2.tif', overwrite=TRUE, options="COMPRESS=LZW")
> # save as small integers (in this case that leads to truncation of values
> 32767, watch out for that )
> rr = writeRaster(r, 'test3.tif', datatype='INT2S', overwrite=TRUE,
options="COMPRESS=NONE")
> rr = writeRaster(r, 'test4.tif', datatype='INT2S', overwrite=TRUE,
options="COMPRESS=LZW")
>
> file.info('test1.tif')$size / 1024
[1] 253.7852
> file.info('test2.tif')$size / 1024
[1] 178.6006
> file.info('test3.tif')$size / 1024
[1] 127.0723
> file.info('test4.tif')$size / 1024
[1] 91.13672
>

Best, Robert




Just wondering if you know how to write out compressed LZW TIFFs from R?

We've been using the rgdal and raster packages to read and write out
GeoTIFFs, but we like to use compressed TIFFs to save on disk space.  Each
time we send them through R they balloon in size again and we need to
re-compress them outside of R.

Thanks!
Tom
Tom


,

Please see:

http://www.gdal.org/frmt_gtiff.html

and use the options= argument to pass a string such as "COMPRESS=LZW".
Please summarise to the R-sig-geo list if this helps.

Best wishes,

Roger

	[[alternative HTML version deleted]]

