This is a process that takes just a few seconds in GIS software. My attempt to do it in R uses a large amount of memory then fails. Is there something wrong in my code, or is this just something R cannot do? I have read R can work inside Grass, can I use a Grass function from inside R?


# I have many environmental rasters in this format
new_r <- raster(ncol=615, nrow=626, xmn=-156.2, xmx=-154.8, ymn=18.89, ymx=20.30)
res(new_r) <- 0.00225
projection(new_r) <- "+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs +towgs84=0,0,0"

R> new_r ### not too big with a few hundred cells per side
class       : RasterLayer 
dimensions  : 627, 622, 1  (nrow, ncol, nlayers)
ncell       : 389994 
resolution  : 0.00225, 0.00225  (x, y)
projection  : +proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs +towgs84=0,0,0 
extent      : -156.2, -154.8, 18.89, 20.3  (xmin, xmax, ymin, ymax)
values      : none

# I get the DEM at much higher resolution (zipfile is 182Mb)
zipurl <- "ftp://soest.hawaii.edu/coastal/webftp/Hawaii/dem/Hawaii_DEM.zip"
DEMzip <- download.file(zipurl, destfile = "DEMzip")
unzip("DEMzip", exdir = "HIDEM")
HIDEM <- raster("HIDEM/hawaii_dem")

R> HIDEM ### 10m resolution, file is way too big
class       : RasterLayer 
dimensions  : 15067, 13136, 1  (nrow, ncol, nlayers)
ncell       : 197920112 
resolution  : 10, 10  (x, y)
projection  : +proj=utm +zone=5 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0 
extent      : 179066, 310426, 2093087, 2243757  (xmin, xmax, ymin, ymax)
values      : HIDEM/hawaii_dem 
min value   : 0 
max value   : 4200 

# the following line fails (after a long time)
new_HIDEM <- projectRaster(HIDEM, new_r)
  • Just curious, what is the package you are using?
    – djq
    Commented Feb 23, 2011 at 1:23
  • @celenius: this package is called raster
    – J. Win.
    Commented Feb 23, 2011 at 2:19

3 Answers 3


From my look at the source, raster looks to guess if the dataset fits into memory, and if so, perform the operation in memory, otherwise on disk. You can force it to perform the calculation by explicitly setting chunksize (cells to process at a time) and maxmemory (maximum number of cells to read into memory):

setOptions(chunksize = 1e+04, maxmemory = 1e+06)

Alternatively, you could perform the transformation with GDAL directly:

gdalwarp -t_srs '+proj=utm +zone=5 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0' HIDEM/hawaii_dem hawaii_dem_utm.tif

This will likely be the fastest option, and doesn't require setting up a GIS environment explicitly.

  • That didn't do it, but this did: setOptions(chunksize = 1e+04, maxmemory = 1e+06) Time eight minutes, much less than it would take to install and use a real GIS.
    – J. Win.
    Commented Feb 23, 2011 at 0:05
  • @J. Winchester: I've updated my response to include your settings as that's the better approach. The package author, would likely be interested to hear when and why it crashes, and hopefully update the defaults to reflect this.
    – scw
    Commented Feb 23, 2011 at 0:19
  • 1
    it's a good idea to add (lossless) compression and tiling (defaults to 256x256) to the GeoTIFF from gdalwarp if your target can handle it: -co COMPRESS=LZW -co TILED=YES
    – mdsumner
    Commented Feb 23, 2011 at 13:10
  • I let Robert Hijmans know about the case. On a smaller DEM, the default settings are near-optimal, so this is a mystery so far.
    – J. Win.
    Commented Feb 23, 2011 at 19:36
  • 1
    Note that as of at least raster version 2.6-7 setOptions has been replaced by rasterOptions
    – see24
    Commented Apr 26, 2019 at 13:33

You can also use the spgrass6 package for the integration between R and grass. The author is Roger Bivand (the author of sp)

This package have many function to completly run grass inside R (or the reverse) and exchange data between R and grass

for more information : http://cran.r-project.org/web/packages/spgrass6/index.html


his is a process that takes just a few seconds in GIS software. My attempt to do it in R uses a >large amount of memory then fails.

You answered your questions, do that in GRASS or GDAL and leave R for other tasks.

  • 1
    For completeness: you should look to the spgrass package to run grass in R.
    – johanvdw
    Commented Feb 22, 2011 at 21:56
  • 1
    And a third option is using saga gis. There is a module (RSAGA) which connects saga and R.
    – johanvdw
    Commented Feb 22, 2011 at 22:02
  • This R function is designed to use GDAL, but seems not to be using it well in this case. My question is "How can I best accomplish this task with R", not "What GIS software is available that can do this task."
    – J. Win.
    Commented Feb 23, 2011 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.