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ù)類型代碼,見(jiàn)下表)。
數(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)文章
記錄一個(gè)C++在條件查詢時(shí)遇到的問(wèn)題(推薦)
這篇文章主要介紹了記錄一個(gè)C++在條件查詢時(shí)遇到的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01C++中BitSet和Bloom_Filter的實(shí)現(xiàn)
本文主要介紹了C++中BitSet和Bloom_Filter的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02簡(jiǎn)單對(duì)比C語(yǔ)言中的fputs()函數(shù)和fputc()函數(shù)
這篇文章主要介紹了簡(jiǎn)單對(duì)比C語(yǔ)言中的fputs()函數(shù)和fputc()函數(shù),注意其之間的區(qū)別,需要的朋友可以參考下2015-08-08C++ boost::asio編程-域名解析詳細(xì)介紹
這篇文章主要介紹了C++ boost::asio編程-域名解析詳細(xì)介紹的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下2016-11-11