[R-sig-Geo] Raster Package Crash raster:::terrain

Robert J. Hijmans r.hijmans at gmail.com
Wed Jul 17 00:37:21 CEST 2013


Andrew,


A simpler script that works for me (indeed with the version of raster)

library(raster)
r <- raster(xmn =  688256.5, xmx = 779259.5, ymn = 4276005, ymx =
4396000, nrows = 11650, ncols = 11347, crs = "+proj=utm +zone=11
+datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
set.seed(232)
r <- init(r, function(x) runif(x, min = 1352, max = 3981),
filename='r_from_File.grd', overwrite = TRUE)
rough <- terrain(r, opt = 'roughness', filename =
'roughness_test.grd', overwrite = TRUE)


If this uses too much RAM for your computer, then use a more
conservative chunksize. Set it to e.g.

rasterOptions(chunksize=1e+06)
# and do
r <- raster('r_from_File.grd')
rough <- terrain(r, opt = 'roughness', filename =
'roughness_test.grd', overwrite = TRUE)

Robert

On Tue, Jul 16, 2013 at 2:31 PM, Michael Sumner <mdsumner at gmail.com> wrote:
> That's a pretty old version of raster (released over a month ago :).
>
> FWIW, I can run your code successfully with the latest CRAN version so
> perhaps see if that fixes the issue first.
>
> (sessionInfo below)
>
> Cheers, Mike.
>
> R version 3.0.1 Patched (2013-07-07 r63210)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
>
> locale:
> [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
> [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
> [5] LC_TIME=English_Australia.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  compiler  methods
> [8] base
>
> other attached packages:
> [1] raster_2.1-49 sp_1.0-11
>
> loaded via a namespace (and not attached):
> [1] grid_3.0.1      lattice_0.20-15
>
>
>
> On Wed, Jul 17, 2013 at 6:22 AM, Andrew Vitale <vitale232 at gmail.com> wrote:
>> Hello,
>>
>> I'm getting some strange behavior from the raster package terrain(opt =
>> 'roughness') function.
>>
>> I have a large (~1GB) digital elevation model in .grd format that was
>> created from a past R session.  When I load the file in and attempt to
>> calculate the roughness index, I consistently crash R.
>>
>> In an attempt to debug, I created a raster file with the same extent,
>> nrows, ncols, and crs as my actual data, and set random values with the
>> same min and max.  My system is storing the file in memory rather than
>> writing to the temporary library.  The terrain(opt = 'roughness') function
>> sometimes runs with the raster in RAM, but usually doesn't.
>>
>> Since running from memory doesn't help me with my problem, I've created the
>> sample script to save the simulated DEM to disk then exit R without saving
>> the session.  You'll then have to restart R.  The second part of the code
>> attempts to reload the simulated DEM and calculate roughness, and this
>> piece of code crashes every time I run it (as it does with my actual data).
>>
>> Here is my sessionInfo(), and a sample code follows:
>>
>>
>> R version 3.0.1 (2013-05-16)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>>
>> locale:
>> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
>> States.1252
>> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
>>
>> [5] LC_TIME=English_United States.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> other attached packages:
>> [1] raster_2.1-37 sp_1.0-9
>>
>> loaded via a namespace (and not attached):
>> [1] grid_3.0.1      lattice_0.20-15
>>
>>
>> #===============================================================================
>> #Example code
>> #Set the working directory to your system
>> #===============================================================================
>> setwd('C:/Users/avitale/Documents')
>> library(raster)
>>
>> #set the extent, resolution, and crs to match my data
>> r <- raster(xmn =  688256.5, xmx = 779259.5, ymn = 4276005, ymx = 4396000,
>> nrows = 11650, ncols = 11347,
>> crs = "+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs +ellps=WGS84
>> +towgs84=0,0,0")
>>
>> set.seed(232)
>> r[] <- runif(ncell(r), min = 1352, max = 3981)
>>
>> #Write r to disk and exit the r session without saving
>> r <- writeRaster(r, 'r_from_File.grd', overwrite = TRUE)
>> q(save = 'no')
>>
>> #===============================================================================
>> #Restart R!!!
>> #Set the working directory to your system
>> #===============================================================================
>> setwd('C:/Users/avitale/Documents')
>> library(raster)
>>
>> r <- raster(file.path(getwd(), 'r_from_File.grd'))
>> rough <- terrain(r, opt = 'roughness', filename = 'roughness_test.grd',
>> overwrite = TRUE)
>>
>>
>>
>> --
>> *Andrew P. Vitale*
>> Masters Student
>> Department of Geography
>> University of Nevada, Reno
>> (412) 915-3632
>> vitale232 at gmail.com
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
> --
> Michael Sumner
> Hobart, Australia
> e-mail: mdsumner at gmail.com
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list