求GDAL的c++源码
小弟刚开始学习这个库,急求GDAL的源码学习一下,只要能实现读取,显示,和保存就可以,谢谢大家帮忙! --------------------编程问答-------------------- GDALAllRegister();GDALDataset *poDataset;
poDataset = (GDALDataset *)GDALOpen(tif.path,GA_ReadOnly); //读取
int nbandcount=poDataset->GetRasterCount();
int imgWidth = poDataset->GetRasterXSize();//文件X方向像素宽度
int imgHeight = poDataset->GetRasterYSize();//Y方向宽度
//下面是创建对象并保存
GDALDataset *poDstDS;
GDALDriver *poDriver ;
const char *pszFormat = "GTiff";
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
char *option;
option = "INTERLEAVE=PIXEL";
poDstDS = poDriver->Create( strTifPath, nPicWidth,nPicWidth,1, GDT_UInt16,
&option );
poDstDS->GetRasterBand(1)->RasterIO(GF_Write,xTileOffset,yTileOffset,nWidth,nHeight,pafScanblock2,nWidth,nHeight,GDT_UInt16,0,0);
GDALClose( (GDALDatasetH) poDstDS );
//至于显示,那不是GDAL的事,转成JPEG或其他位图格式,自己显示去吧
--------------------编程问答-------------------- 这是显示读取函数
GDALDataset *poDataset;
GDALAllRegister();
CString origCString(m_filepath);
wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
size_t origsize = wcslen(wCharString) + 1;
size_t convertedChars = 0;
char *CharString;
CharString=new char(origsize);
wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
poDataset = (GDALDataset *) GDALOpen( CharString, GA_ReadOnly );
if( poDataset != NULL)
{
nImgSizeX=poDataset->GetRasterXSize();
nImgSizeY=poDataset->GetRasterYSize();
m_bandcount=poDataset->GetRasterCount();
/*m_view->m_bandcount=m_bandcount;*/
// char str[50];
// wtoa(m_bandcount,str,10);
// CString str;
// str.Format(_T("%d"),m_bandcount);
// GetDlgItem(IDC_BANDCOUNT)->SetWindowText(str);
/*m_view.Invalidate();*/
TRACE("%d,%d,%d\n",nImgSizeX,nImgSizeY,m_bandcount);
CString ProjectionRef;
ProjectionRef=poDataset->GetProjectionRef();
GDALRasterBand *poBand1,*poBand2,*poBand3;
m_view=new CIMGView();
if (m_view->m_page==0)
{
poBand1 = poDataset->GetRasterBand(m_page1.m_blue);
poBand2 = poDataset->GetRasterBand( m_page1.m_green );
poBand3 = poDataset->GetRasterBand( m_page1.m_red);
}
else
{
poBand1 = poDataset->GetRasterBand(m_page2.m_gray);
poBand2 = poDataset->GetRasterBand( m_page2.m_gray );
poBand3 = poDataset->GetRasterBand( m_page2.m_gray);
}
CString RasterDataType,ColorInterpretation;
RasterDataType=GDALGetDataTypeName(poBand1->GetRasterDataType());
ColorInterpretation=GDALGetColorInterpretationName(poBand1->GetColorInterpretation());
poBand1->GetMaximum();
poBand1->GetMinimum();
pafScanblock1=new BYTE[nImgSizeX*nImgSizeY];
pafScanblock2=new BYTE[nImgSizeX*nImgSizeY];
pafScanblock3=new BYTE[nImgSizeX*nImgSizeY];
BYTE* tempafScanblock1=new BYTE[nImgSizeX*nImgSizeY];
BYTE* tempafScanblock2=new BYTE[nImgSizeX*nImgSizeY];
BYTE* tempafScanblock3=new BYTE[nImgSizeX*nImgSizeY];
m_buffer=new BYTE[nImgSizeX*nImgSizeY*4];
poBand1->RasterIO( GF_Read, 0, 0,nImgSizeX,nImgSizeY,
pafScanblock1,nImgSizeX,nImgSizeY, poBand1->GetRasterDataType(),0, 0 );
poBand2->RasterIO( GF_Read, 0, 0,nImgSizeX,nImgSizeY,
pafScanblock2,nImgSizeX,nImgSizeY, poBand2->GetRasterDataType(),0, 0 );
poBand3->RasterIO( GF_Read, 0, 0,nImgSizeX,nImgSizeY,
pafScanblock3,nImgSizeX,nImgSizeY, poBand3->GetRasterDataType(),0, 0 );
strentch(pafScanblock1,tempafScanblock1);
strentch(pafScanblock2,tempafScanblock2);//灰度拉伸函数就是很简单的拉伸
strentch(pafScanblock3,tempafScanblock3);
for (int i=0;i<nImgSizeX*nImgSizeY;i++)
{
m_buffer[i*4]=tempafScanblock1[i];
m_buffer[i*4+1]=tempafScanblock2[i];
m_buffer[i*4+2]=tempafScanblock3[i];
m_buffer[i*4+3]=255;
}
delete [] tempafScanblock1;
delete [] tempafScanblock2;
delete [] tempafScanblock3;
}
Invalidate();
delete poDataset;
delete [] pafScanblock1;
delete [] pafScanblock2;
delete [] pafScanblock3;
显示函数我用了GDI+:
CDC *pDC=GetDC();
Graphics g(pDC->m_hDC);
Bitmap bitmap(nImgSizeX, nImgSizeY, nImgSizeX * 4, PixelFormat32bppRGB, m_buffer);
g.DrawImage(&bitmap,0,0);
出的问题是1:读取上百波段的IMG格式图像时数据集为空,几个波段的图像可以。
2:图像显示不真实,图像发绿! --------------------编程问答-------------------- 确实不懂,
上百波段的数据我没见过,也没处理过
另外,GDal不是用来做显示的.是否是显示的问题我也不清楚。 --------------------编程问答-------------------- Band的RasterIO函数的最后一个参数设为1看看,如果设为0的话,就是以RRRGGGBBB这种形式存储,如果设为1的话,就是以RGBRGBRGB这种形式存储。 --------------------编程问答-------------------- 这个你可以到google上去搜一把,很多地方都有的
http://www.codase.com/search/smart?join=VSIFOpen&scope=join%2Fjoin&lang=*&project=&search=Search
这个网站就有很多示例代码,自己找找看吧 --------------------编程问答-------------------- 学习加标记 --------------------编程问答-------------------- 学习加标记
补充:企业软件 , 地理信息系统