关于GDAL读取MODIS卫星数据
2009-03-20 15:22
489 查看
由于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;
}
http://blog.csdn.net/tangnf/archive/2007/06/03/1636214.aspx
下面给一个例子,希望对大家用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;
}
http://blog.csdn.net/tangnf/archive/2007/06/03/1636214.aspx
相关文章推荐
- 关于GDAL读取MODIS卫星数据
- 关于GDAL读取MODIS卫星数据
- 使用GDAL/OGR读取多边形数据
- 关于c++文件流读取最后一行数据时重复或者出错问题
- 关于使用PyTorch设置多线程(threads)进行数据读取而导致GPU显存始终不释放的问题
- 关于oracle数据库读取数据的三种方式
- 关于OGR读取多边形-Polygon(GDAL 2.0版本之前)
- 关于access数据库读取数据问题
- 关于mybatis读取数据库字段text类型时,读出数据为地址,并不是字符串的问题
- SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题
- 使用GDAL工具对FY3系列卫星数据进行校正
- GDAL\OGR读取数据示例 C#版本
- 关于 OLEDB 读取 XLS 关系型数据文件的指定 Worksheet 时末尾必须加 $ 的原因。
- 关于 C# select 读取 Excel 数据 为空,或成科学计数法解决方案
- SharePoint2010沙盒解决方案基础开发——关于TreeView树形控件读取列表数据(树形导航)的webpart开发及问题
- 关于C++语言从磁盘里面读取浮点数据的问题
- 关于Tensorflow 的数据读取环节
- 关于MNIST数据格式和matlab读取问题
- GDAL读取S-57海图数据中文属性值乱码问题解决
- 关于json解析、读取数据