C/C++下讀取ENVI柵格文件格式的示例代碼
ENVI使用的是通用柵格數(shù)據(jù)格式,包含一個簡單的二進制文件( a simple flat binary )和一個相關的ASCII(文本)的頭文件。
利用其他語言如C/C++等直接讀取ENVI的數(shù)據(jù),則可以先對hdr文件進行解析,獲取數(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}解析的關鍵信息有samples:640(列),lines:400(行),header offset:0(頭信息偏移量-單位為字節(jié)),data type=1(數(shù)據(jù)類型代碼,見下表)。
| 數(shù)據(jù)類型 | 代碼 |
| 字節(jié)型 | 1 |
| 16位有符號整型 | 2 |
| 32位有符號長整型 | 3 |
| 32位無符號長整型 | 13 |
| 浮點型 | 4 |
| 雙精度浮點型 | 5 |
對常用數(shù)據(jù)類型文件進行了讀寫的測試,值完全一致。
利用IDL進行文件寫出:
/* C++讀取ENVI格式技術(shù)測試代碼 輸出不同數(shù)據(jù)類型的二進制文件 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 //浮點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格式技術(shù)測試代碼
#include "stdafx.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
printf("Hello ! Successful Using C++! ^_^ \n");
int i,n;
FILE*fp;
//二進制字節(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<<"二進制";
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柵格文件格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++中BitSet和Bloom_Filter的實現(xiàn)
本文主要介紹了C++中BitSet和Bloom_Filter的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02
簡單對比C語言中的fputs()函數(shù)和fputc()函數(shù)
這篇文章主要介紹了簡單對比C語言中的fputs()函數(shù)和fputc()函數(shù),注意其之間的區(qū)別,需要的朋友可以參考下2015-08-08

