envi使用的是通用栅格数据格式,包含一个简单的二进制文件( a simple flat binary )和一个相关的ascii(文本)的头文件。
利用其他语言如c/c++等直接读取envi的数据,则可以先对hdr文件进行解析,获取数据类型。
hdr的文件结构如下
envi
description = {
canon city, colorado, landsat tm, calibrated to reflectance }
samples = 640
lines = 400
bands = 6
header offset = 0
file type = envi standard
data type = 1
interleave = bsq
sensor type = landsat tm
wavelength units = micrometers
z plot range = {0.00, 100.00}
z plot titles = {wavelength, reflectance}
band names = {
tm band 1, tm band 2, tm band 3, tm band 4, tm band 5, tm band 7}
wavelength = {
0.48500, 0.56000, 0.66000, 0.83000, 1.65000, 2.21500}解析的关键信息有samples:640(列),lines:400(行),header offset:0(头信息偏移量-单位为字节),data type=1(数据类型代码,见下表)。
| 数据类型 | 代码 |
| 字节型 | 1 |
| 16位有符号整型 | 2 |
| 32位有符号长整型 | 3 |
| 32位无符号长整型 | 13 |
| 浮点型 | 4 |
| 双精度浮点型 | 5 |
对常用数据类型文件进行了读写的测试,值完全一致。
利用idl进行文件写出:
/* c++读取envi格式技术测试代码 输出不同数据类型的二进制文件 author: dyq 2011年6月2日 bbs: http://bbs.esrichina-bj.cn/esri/forum-28-1.html e-mail: dongyq@esrichina-bj.cn blog: http://hi.baidu.com/dyqwrp */ pro test_out_bin outdir = 'c:\temp\' if file_test(outdir,/directory) ne 1 then file_mkdir,outdir //字节byte openw,lun,outdir+'a.dat',/get_lun writeu,lun,bindgen(10) free_lun,lun //整型int openw,lun,outdir+'b.dat',/get_lun writeu,lun,indgen(10) free_lun,lun //浮点float openw,lun,outdir+'c.dat',/get_lun writeu,lun,findgen(10) free_lun,lun //长整型long openw,lun,outdir+'d.dat',/get_lun writeu,lun,lindgen(10) free_lun,lun //双精度double openw,lun,outdir+'e.dat',/get_lun writeu,lun,dindgen(10) free_lun,lun end
c++下读取文件:
//c++读取envi格式技术测试代码
#include "stdafx.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
printf("hello ! successful using c++! ^_^ \n");
int i,n;
file*fp;
//二进制字节型
char *bdata=new char[10];
fp=fopen("c:\\temp\\a.dat","rb");
n=fread(bdata,1,10,fp);
fclose(fp);
for(i=0;i<10;i++)
{
cout<<"二进制";
cout<<i<<":"<<short(bdata[i])<<endl;
}
//二进制整型文件
short int *idata=new short int[10];
fp=fopen("c:\\temp\\b.dat","rb");
n=fread(idata,2,10,fp);
fclose(fp);
for(i=0;i<10;i++)
{
cout<<"整型";
cout<<i<<":"<<idata[i]<<endl;
}
//二进制浮点文件
float *fdata=new float[10];
fp=fopen("c:\\temp\\c.dat","rb");
n=fread(fdata,4,10,fp);
fclose(fp);
for(i=0;i<10;i++)
{
cout<<"浮点";
cout<<i<<":"<<fdata[i]<<endl;
}
//二进制长整型文件
long *ldata=new long[10];
fp=fopen("c:\\temp\\d.dat","rb");
n=fread(ldata,4,10,fp);
fclose(fp);
for(i=0;i<10;i++)
{
cout<<"长整型";
cout<<i<<":"<<ldata[i]<<endl;
}
//双精度double
double *ddata=new double[10];
fp=fopen("c:\\temp\\e.dat","rb");
n=fread(ddata,8,10,fp);
fclose(fp);
for(i=0;i<10;i++)
{
cout<<"双精度型";
cout<<i<<":"<<ddata[i]<<endl;
}
return 0;
}最后输出:

到此这篇关于c/c++下读取envi栅格文件格式的示例代码的文章就介绍到这了,更多相关c++读取envi栅格文件格式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论