C/C++下讀取ENVI柵格文件格式的示例代碼
ENVI使用的是通用柵格數(shù)據(jù)格式,包含一個(gè)簡(jiǎn)單的二進(jìn)制文件( a simple flat binary )和一個(gè)相關(guān)的ASCII(文本)的頭文件。
利用其他語(yǔ)言如C/C++等直接讀取ENVI的數(shù)據(jù),則可以先對(duì)hdr文件進(jìn)行解析,獲取數(shù)據(jù)類型。
hdr的文件結(jié)構(gòu)如下
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}解析的關(guān)鍵信息有samples:640(列),lines:400(行),header offset:0(頭信息偏移量-單位為字節(jié)),data type=1(數(shù)據(jù)類型代碼,見下表)。
| 數(shù)據(jù)類型 | 代碼 |
| 字節(jié)型 | 1 |
| 16位有符號(hào)整型 | 2 |
| 32位有符號(hào)長(zhǎng)整型 | 3 |
| 32位無(wú)符號(hào)長(zhǎng)整型 | 13 |
| 浮點(diǎn)型 | 4 |
| 雙精度浮點(diǎn)型 | 5 |
對(duì)常用數(shù)據(jù)類型文件進(jìn)行了讀寫的測(cè)試,值完全一致。
利用IDL進(jìn)行文件寫出:
/* C++讀取ENVI格式技術(shù)測(cè)試代碼 輸出不同數(shù)據(jù)類型的二進(jìn)制文件 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 //字節(jié)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 //浮點(diǎn)float OPENW,lun,outdir+'c.dat',/get_lun WRITEU,lun,FINDGEN(10) FREE_LUN,lun //長(zhǎng)整型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格式技術(shù)測(cè)試代碼
#include "stdafx.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
printf("Hello ! Successful Using C++! ^_^ \n");
int i,n;
FILE*fp;
//二進(jìn)制字節(jié)型
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<<"二進(jìn)制";
cout<<i<<":"<<short(bdata[i])<<endl;
}
//二進(jìn)制整型文件
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;
}
//二進(jìn)制浮點(diǎn)文件
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<<"浮點(diǎn)";
cout<<i<<":"<<fdata[i]<<endl;
}
//二進(jìn)制長(zhǎng)整型文件
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<<"長(zhǎng)整型";
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;
}最后輸出:

到此這篇關(guān)于C/C++下讀取ENVI柵格文件格式的示例代碼的文章就介紹到這了,更多相關(guān)C++讀取ENVI柵格文件格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
帶頭結(jié)點(diǎn)單鏈表與不帶頭結(jié)點(diǎn)單鏈表的區(qū)別
這篇文章主要介紹了帶頭結(jié)點(diǎn)單鏈表與不帶頭結(jié)點(diǎn)單鏈表的區(qū)別,需要的朋友可以參考下2023-07-07
在C++中實(shí)現(xiàn)aligned_malloc的方法
這篇文章主要介紹了在C++中實(shí)現(xiàn)aligned_malloc的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之vector底層實(shí)現(xiàn)機(jī)制解析
向量(Vector)是一個(gè)封裝了動(dòng)態(tài)大小數(shù)組的順序容器(Sequence?Container)。跟任意其它類型容器一樣,它能夠存放各種類型的對(duì)象??梢院?jiǎn)單的認(rèn)為,向量是一個(gè)能夠存放任意類型的動(dòng)態(tài)數(shù)組2021-11-11
C++實(shí)現(xiàn)分?jǐn)?shù)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)分?jǐn)?shù)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06

