基于R语言或Matlab直接读取压缩包中的数据

9 篇文章 7 订阅
3 篇文章 0 订阅

写在前面

以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

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撼沧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值