[GDAL]2.读取栅格和矢量数据

MFC项目要选择多字节字符集。

GDAL读取栅格:

 1 void DlgImageInfo::OnBnClickedSearch()
 2 {
 3     // TODO: 在此添加控件通知处理程序代码
 4     CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("IMG影像文件(*.img)|*.img|TIFF影像文件(*.tiff)|*.tiff||"),AfxGetMainWnd());
 5     CString str;
 6     if (dlg.DoModal()==IDOK)
 7     {
 8         str=dlg.GetPathName();
 9         m_FileName.SetWindowText(str);
10 
11         GDALAllRegister();  
12         CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
13         const char* pszFile =(LPCTSTR)str;
14         GDALDataset* poDataset;  
15         //使用只读方式打开图像   
16         poDataset = (GDALDataset*)GDALOpen(pszFile,GA_ReadOnly); 
17         if( poDataset == NULL )  
18         {  
19             CString ex;
20             ex.AppendFormat("File: %s不能打开!\n",str);
21             listImageInfo.AddString(ex);  //            
22         }  
23         CString sDriver;
24         sDriver.AppendFormat( "Driver: %s/%s\n",
25             poDataset->GetDriver()->GetDescription(), 
26             poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
27 
28         listImageInfo.AddString(sDriver);         
29         
30         CString sSize;
31         sSize.AppendFormat("Size is %dx%dx%d\n",poDataset->GetRasterXSize(),poDataset->GetRasterYSize(),poDataset->GetRasterCount());
32         listImageInfo.AddString(sSize);
33         //输出图像的投影信息 
34         if( poDataset->GetProjectionRef()  != NULL ) 
35         {
36             CString sPrj;
37             sPrj.Format("Projection is `%s'\n", poDataset->GetProjectionRef());
38             listImageInfo.AddString(sPrj)  ; 
39         }
40         //输出图像的坐标和分辨率信息 
41         double adfGeoTransform[6]; 
42         if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) 
43         { 
44             CString sOrgin;
45             sOrgin.Format("Origin = (%.6f,%.6f)\n", 
46                 adfGeoTransform[0], adfGeoTransform[3]);
47             CString sPixel;
48             sPixel.Format("Pixel Size = (%.6f,%.6f)\n", 
49                 adfGeoTransform[1], adfGeoTransform[5]);
50             listImageInfo.AddString(sOrgin)  ; 
51             listImageInfo.AddString(sPixel)  ; 
52         }
53         GDALClose((GDALDatasetH )poDataset); 
54     }
55 }

 

OGR读取矢量数据:

 1 void VectorInfoDlg::OnBnClickedSearchVector()
 2 {
 3     CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("ESRI Shapefile(*.shp)|*.shp|MapInfo File(*.mid *.mif *.tab)|*.mid *.mif *tab|Arc/Info .E00 (*.e00)|*.e00|AutoCAD DXF(*.dxf)|*.dxf|Comma Separated Value (.csv)|*.csv|GML(*.gml)|*.gml|KML(*.kml)|*.kml|所有文件|*.*||"),AfxGetMainWnd());
 4     CString strFilePath;
 5     if (dlg.DoModal()==IDOK)
 6     {
 7         strFilePath=dlg.GetPathName();
 8         m_FileName.SetWindowText(strFilePath);
 9         const char* pszFile =(LPCTSTR)strFilePath;
10 
11         CString strFilename=dlg.GetFileName();
12         strFilename.Delete(strFilename.GetLength()-4,4);
13         const char* pFileName =(LPCTSTR)strFilename;
14         OGRRegisterAll();
15         CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO");
16         OGRDataSource  *poDS;
17 
18         poDS = OGRSFDriverRegistrar::Open(pszFile, FALSE );
19         if( poDS == NULL )
20         {
21             AfxMessageBox("Open failed.\n");
22             return;
23         }
24 
25         OGRLayer  *poLayer;
26 
27         poLayer = poDS->GetLayerByName( pFileName );
28         OGRFeature *poFeature;
29         poLayer->ResetReading();
30         while( (poFeature = poLayer->GetNextFeature()) != NULL )
31         {
32             OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
33             int iField;
34             CString strFieldDefn;
35             strFieldDefn.AppendFormat("共有%d个属性字段:",poFDefn->GetFieldCount());
36             for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
37             {
38                 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
39                 
40                 if( poFieldDefn->GetType() == OFTInteger )
41                     strFieldDefn.AppendFormat( "%d,", poFeature->GetFieldAsInteger( iField ) );
42                 else if( poFieldDefn->GetType() == OFTReal )
43                     strFieldDefn.AppendFormat( "%.3f,", poFeature->GetFieldAsDouble(iField) );
44                 else if( poFieldDefn->GetType() == OFTString )
45                     strFieldDefn.AppendFormat( "%s,", poFeature->GetFieldAsString(iField) );
46                 else
47                     strFieldDefn.AppendFormat( "%s,", poFeature->GetFieldAsString(iField) );
48             }
49             m_VectorInfo.AddString(strFieldDefn);
50             OGRGeometry *poGeometry;
51             CString strGeometry;
52             poGeometry = poFeature->GetGeometryRef();
53             if( poGeometry != NULL 
54                 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
55             {
56                 OGRPoint *poPoint = (OGRPoint *) poGeometry;
57 
58                 strGeometry.AppendFormat( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
59             }
60             else
61             {
62                 strGeometry.AppendFormat( "no point geometry\n" );
63             }  
64             m_VectorInfo.AddString(strFieldDefn);
65             OGRFeature::DestroyFeature( poFeature );
66         }
67 
68         OGRDataSource::DestroyDataSource( poDS );//释放数据源
69     }
70 }

注意:

如果VS编译的是64位程序,那么需要的是64位的GDAL的DLL,不然会报找不到函数依赖项的错误。

#include "..\\GDAL\\include\\gdal_priv.h"
#include "..\\GDAL\\include\\gdal_priv.h"
#include "..\\GDAL\\include\\ogr_spatialref.h"
#include "..\\GDAL\\include\\ogr_attrind.h"
#include "..\\GDAL\\include\\ogrsf_frmts.h"
#include "..\\GDAL\\include\\gdalwarper.h"
#include "..\\GDAL\\include\\gdal.h"
#include "..\\GDAL\\include\\cpl_string.h"
#pragma comment(lib,"..\\GDAL\\lib\\gdal_i.lib")

  

posted @ 2012-12-21 16:20  太一吾鱼水  阅读(1444)  评论(1编辑  收藏  举报