2016-08-23 32 views
0

我想加起來geotiffs,但遇到內存問題。 R爲使用所有32GB的下列R-錯誤...空間數據和內存

In writeValues(y, x, start = 1) : 
    Reached total allocation of 32710Mb: see help(memory.size) 

我還檢查R的性能,這是64位和目標是...

「C:\ Program Files文件\ r \ R-3.3.0 \ BIN \ 64 \ Rgui.exe」

的版本是

R.Version() 
$platform 
[1] "x86_64-w64-mingw32" 

$arch 
[1] "x86_64" 

$os 
[1] "mingw32" 

$system 
[1] "x86_64, mingw32" 

$status 
[1] "" 

$major 
[1] "3" 

$minor 
[1] "3.0" 

$year 
[1] "2016" 

$month 
[1] "05" 

$day 
[1] "03" 

$`svn rev` 
[1] "70573" 

$language 
[1] "R" 

$version.string 
[1] "R version 3.3.0 (2016-05-03)" 

$nickname 
[1] "Supposedly Educational" 

所以看起來正在使用R.我最大內存我試過用bigmemory打包在R中。所以在下面的代碼中,我嘗試了改變th e矩陣轉換爲big.matrix,但失敗並且在嘗試寫入輸出文件時發生錯誤。任何關於嘗試修改代碼的建議,以便使用更少的內存或嘗試在ff或bigmemory軟件包中工作?

############ LOOP THROUGH AGE MAPS TO COMPILE THE NUMBER OF TIMES A CELL BURNS DURING A GIVEN SPAN OF TIME #################### 
## Empirical Fires 
print("1 of 3: 2010-2015") 

burn.mat<- matrix(0,nrow,ncol) #create matrix of all zero's, the dimension of your landscape (row, col) 

# Read in Historical Fire maps 
for (j in 2010:2015){ #Year Loop 

    age.tmp<- as.matrix(raster(paste('fr',j,'.tif',sep=''))) #read in Age Map 

    burn.mat<- burn.mat+(age.tmp==1) #when something has burned in ALFRESCO empirical fire history files, AGE=1. (age.tmp==0) is a 'logic' cmd, returning a 0,1 map for True/False 

    #Write the data to a geotiff 
    out <- raster(burn.mat,xmn=-1692148,xmx= 1321752, ymn = 490809.9, ymx = 2245610, crs = '+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs') 
    writeRaster(out,filename=paste(outdir,'/burn.mat.hist.1950-2007.tif',sep=''),format = 'GTiff',options='COMPRESS=LZW',datatype='FLT4S',overwrite=T) 
} 
+0

爲什麼代碼每次通過循環寫入相同的文件名? – mdsumner

回答

0

的問題可能會自動消失,如果你使用的光柵*的對象,而不是矩陣。喜歡的東西

library(raster) 
r <- raster('fr2010.tif') 

burn.mat <- setValues(r, 0) 

for (j in 2010:2015) { 
    age.tmp <- raster(paste0('fr', j, '.tif')) 
    burn.mat <- burn.mat + (age.tmp==1) 
# if age.tmp only has values of 0 and 1 use this instead: 
# burn.mat <- burn.mat + age.tmp 
} 

# write the results outside of the loop 
writeRaster(burn.mat, filename=file.path(outdir, 'burn.mat.hist.1950-2007.tif'), options='COMPRESS=LZW',datatype='FLT4S',overwrite=TRUE) 

更直接的方式沒有環

files <- paste0('fr', 2010:2015, '.tif')) 
s <- stack(files) 
burn <- sum(s) 

或者

burn <- sum(s == 1) 

或者寫入到一個文件中的一個步驟

b <- calc(s, sum, filename=file.path(outdir, 'burn.mat.hist.1950-2007.tif'), options='COMPRESS=LZW', datatype='FLT4S', overwrite=TRUE)