写在前面
以gosif网站上的tiff数据为例。
该数据是地学上常用的一种数据,希望读取该网站上的gosif数据:http://data.globalecology.unh.edu/data/GOSIF_v2/8day/
但数据全是压缩包格式的,解压后内存占用又会变大。所以需要直接从压缩包中读取数据。
基于R语言
library(R.utils)
library(stringr)
library(raster)
library(rasterVis)
# download and read the data
filename <- 'GOSIF_2000057.tif.gz'
website_path <- 'http://data.globalecology.unh.edu/data/GOSIF_v2/8day/'
save_path <- 'D:/user/Downloads/'
gzfile <- download.file(str_c(website_path,filename), destfile = str_c(save_path,filename))
# Read the data that has been downloaded
filename <- 'GOSIF_2001.tif.gz'
data_path <- 'D:/user/Downloads/gosif/'
gzfile <- gunzip(str_c(data_path,filename), remove = F)
# read
gosif = raster(gzfile)
# plot
levelplot(gosif)
# read the data from net directly
website_path <- 'http://data.globalecology.unh.edu/data/GOSIF_v2/8day/'
filename <- 'GOSIF_2000057.tif.gz'
z = gzcon(url(str_c(website_path,filename)))
raw = textConnection(readLines(z))
close(z)
# read
gosif = raster(raw)
close(raw)
基于Matlab
% 从压缩包中读取数据
filenames = gunzip('D:/user/Downloads/gosif/GOSIF_2001.tif.gz');
% 读取为栅格数据
[A,R] = geotiffread(filenames{1,1}) ;
% 删除已读取的数据,节省存储空间
delete('D:/user/Downloads/gosif/GOSIF_2001.tif')
函数详解:gunzip
gunzip(files)
fiels是压缩文件路径,输出压缩后的文件具有相同的名称(不包括扩展名),并被写入与输入文件相同的目录。
gunzip(files,outputdir)
将压缩后的文件写入目录outputdir。
gunzip(url, …)
从Internet通用资源定位器(URL)中提取gzip内容。URL被下载到临时目录并删除。‘http://’
filenames = gunzip(…)
返回字符串单元格数组中gunzips文件的相对路径名,cell格式
gunzip(’*.gz’)
对当前目录中的所有gz文件进行解压缩
参考:
http://matlab.izmiran.ru/help/techdoc/ref/gunzip.html
https://zhidao.baidu.com/question/1817713776587277068.html
将文件压缩为.gz格式
gzip(files)
gzip(files,outputdir)
filenames = gzip(...)
http://matlab.izmiran.ru/help/techdoc/ref/gzip.html
其他压缩解压缩相关函数
http://matlab.izmiran.ru/help/techdoc/ref/tar.html
http://matlab.izmiran.ru/help/techdoc/ref/untar.html
http://matlab.izmiran.ru/help/techdoc/ref/unzip.html
http://matlab.izmiran.ru/help/techdoc/ref/zip.html