Fiz o exercício abaixo,
me preocupando mais com uso de memória. Nesse quesito o pacote
{raster} traz grandes vantagens. Além disso, formatos binários
otimizam a leitura e ocupam menor espaço em disco.
###
<code r>
setwd("D:/Temp")
sapply(c("raster",
"sp", "rgdal"), require, char=T)
fn
<- basename(url0)
#
download.file(url0, fn, mode="wb")
###
Leitura com pacotes {raster} e {readGDAL}...
###
O pacote {raster} acessa informações do arquivo em disco,
###
enquanto {readGDAL} carrega na memória.
img1
<- raster(fn)
img2
<- readGDAL(fn)
par(mfrow=c(1,2))
image(img1,
asp=T, axes=F, ann=F); image(img2, asp=T)
par(mfrow=c(1,1))
print(object.size(img1),
units="Kb") # 11.4 Kb
print(object.size(img2),
units="Kb") # 2540.9 Kb
###
Você pode converter seu objeto para {raster}...
###
Mas em princípio ele vai ficar residente na memória
img2a
<- raster(img2)
print(object.size(img2a),
units="Kb") # 2548 Kb
###
Para economizar memória, você pode salvar no disco e
reabrir com {raster}
###
Pode usar qualquer formato aceito por raster, inclusive
ascii
###
Mas formatos binários otimizam o acesso e leitura
writeFormats()
writeRaster(img2a,
"novo.tif", "GTiff", overwrite=F)
writeRaster(img2a,
"novo.asc", "ascii", overwrite=F)
rm(img2,
img2a) ### apagar intermediários pra liberar memória
system.time(img3a
<- raster("novo.tif")) # 0.03s
system.time(img3b
<- raster("novo.asc")) # 0.06s
print(object.size(img3a),
units="Kb") # 11.4 Kb
print(object.size(img3b),
units="Kb") # 11.3 Kb
file.info(dir(patt="novo\\."))
### tamanho no disco
#
size isdir mode mtime
ctime atime exe
#
novo.asc 2289309 FALSE 666 2016-02-18 05:50:06 2016-02-18
05:50:02 2016-02-18 05:50:02 no
#
novo.tif 1249547 FALSE 666 2016-02-18 05:49:48 2016-02-18
05:49:48 2016-02-18 05:49:48 no
###
tamanho no disco em "Mb"
file.info(dir(patt="novo\\."))$size/1024^2
# [1] 2.183255 1.191661
par(mfrow=c(1,2))
image(img3a,
asp=T); image(img3b, asp=T)
par(mfrow=c(1,1))
###
Geotiff preserva mais atributos...
img3a
#
class : RasterLayer
#
dimensions : 929, 699, 649371 (nrow, ncol, ncell)
#
resolution : 10, 10 (x, y)
#
extent : 444650, 451640, 4631220, 4640510 (xmin,
xmax, ymin, ymax)
#
coord. ref. : +proj=utm +zone=16 +datum=NAD27 +units=m
+no_defs +ellps=clrk66
+nadgrids=@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat
#
data source : D:\Temp\novo.tif
#
names : novo
#
values : 6, 255 (min, max)
img3b
#
class : RasterLayer
#
dimensions : 929, 699, 649371 (nrow, ncol, ncell)
#
resolution : 10, 10 (x, y)
#
extent : 444650, 451640, 4631220, 4640510 (xmin,
xmax, ymin, ymax)
#
coord. ref. : NA
#
data source : D:\Temp\novo.asc
#
names : novo
###
</code>