您的位置:首页 > 其它

关于GDAL读取MODIS卫星数据

2010-12-15 00:30 351 查看
由于modis卫星数据跟我们经常遇到的geotif数据组织方式不一样,读取的时候一定要特别注意。geotif数据,一般是一个文件,包含了多个波段的数据;而modis呢,一个文件包含了多各SUBDATASETSGDAL,每个SUBDATASETS又包含多个波段数据。另外默认编译的GDAL并不包含对MODIS数据支持,需要单独下载针对HDF4,HDF5的源码,再修改下make.opt文件,这时再编译GDAL,就支持modis数据的读写了。如果嫌麻烦就下载一个别人编译好了的来用,网上有很多基于GDAL的开源项目,搜一下就可以了。
下面给一个例子,希望对大家用modis数据研究大尺度的地理现象有所帮助。modis数据毕竟免费嘛,呵呵……
////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <vector>
#include<string>
using namespace std;

#include<math.h>
#include "..//include//gdal.h"
#include "..//include//gdal_priv.h"
#include "..//include//ogr_srs_api.h"
#include "..//include//cpl_string.h"
#include "..//include//cpl_conv.h"
#pragma comment (lib,"..//lib//gdal_i.lib")

int main(int argc, char *argv[])
{

GDALAllRegister();
vector<GDALDataset *> datasets;

char * hdfFileName="D://fsbdata//V2KRNS10__20060411_NDVI__SE-Asia//0001//0001_NDV.HDF";
GDALDataset *tmpDataset = (GDALDataset *) GDALOpen( hdfFileName, GA_ReadOnly);

if(tmpDataset==NULL) return 0;
GDALDriver * driver=tmpDataset->GetDriver ();
string papszMetadata=GDALGetDriverShortName((GDALDriverH)tmpDataset);
int index=papszMetadata.find_first_of("hdf");
if(index<0)
index=papszMetadata.find_first_of("HDF");
if(index)
{
char ** SUBDATASETS = GDALGetMetadata( (GDALDatasetH)tmpDataset, "SUBDATASETS" );
if( CSLCount(SUBDATASETS) > 0 )
{
printf( "Subdatasets:/n" );
for(int i = 0; SUBDATASETS[i] != NULL; i++ )
{
if(i%2==0)
{
string tmpstr=string(SUBDATASETS[i]);
tmpstr=tmpstr.substr(tmpstr.find_first_of("=")+1);
const char *tmpfilename=tmpstr.c_str();
GDALDataset * tmpdt=(GDALDataset *) GDALOpen(tmpfilename, GA_ReadOnly);
if(tmpdt)
{
datasets.push_back(tmpdt);
}
}
}
}
else
{
int bandCount=tmpDataset->GetRasterCount();
if(bandCount>0)
{
GDALRasterBand * poband=tmpDataset->GetRasterBand(1);
}
printf( "it contains %d bands! /n",bandCount);
}
}
if(!datasets.empty())
{
int count=datasets.size();
int bandCount=0;
for(int i=0;i<count;i++)
{
bandCount+=datasets[i]->GetRasterCount();
if(datasets[i]->GetRasterCount()>0)
{
GDALRasterBand * poband=datasets[i]->GetRasterBand(1);
}
}
printf( "it contains %d bands! /n",bandCount);
for(i=0;i<count;i++)
{
delete datasets[i];
datasets[i]=NULL;
}
datasets.reserve(0);
}

delete tmpDataset;
delete driver;
printf( "it's ok! please enter any key! /n" );
return 0;
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/tangnf/archive/2007/06/03/1636214.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: