GDAL在VS2008 VC++中的配置

GDAL在VS2008 VC++中的配置

 

A prebuilt GDAL 1.4.1 library is available at:

 http://download.osgeo.org/gdal/win32/1.4.1/gdalwin32dev141.zip

This should be unpacked on top of the executable package which includes the gdal14.dll:

 http://download.osgeo.org/gdal/win32/1.4.1/gdalwin32exe141.zip

The dev package includes: * Include files in the gdalwin32-1.4.1\include directory. * An release (/MD /Ox) DLL (bin\gdal14.dll) and stub library (lib\gdal_i.lib).

The DLL and stub libraries are built with Visual Studio.NET 2003 (VC 7.1), which is likely required for any use of the C++ API. The libraries should be compatible with any win32 C/C++ compiler if only the GDAL/OGR C API is used. No debug version is offered.

1、下载GDAL库,新建VC++工程文件,在项目的属性对话框中找到{配置属性}→{C/C++}→{常规},在右侧的{附加包含目录}中,将下载到的GDAL库的include文件夹路径添加到这里:

 

2、在{配置属性}→{链接器}→{常规}的{附加库目录}中,将下载到的GDAL库的lib文件夹填写入:

 

3、在{配置属性}→{链接器}→{输入}中的{附加依赖项}中,填入lib文件夹下的gdal_i.lib文件名。

 

4、现在可以进行编译,但是在运行的过程中,会出现“gdal14.dll”缺失的问题。将下载到的gdalwin32exe141.zip中的bin文件夹下的gdal14.dll解压到编译成的debug文件夹下,运行成功。

 

测试代码:

View Code
  1 #include <stdio.h> //官方提供的代码中缺少这项,编译错误
  2 
  3 #include "gdal_priv.h"
  4 
  5 #include "cpl_conv.h" //for CPLMalloc()
  6 
  7  
  8 
  9 int main()
 10 
 11 {
 12 
 13     //注册文件格式
 14 
 15     GDALAllRegister();
 16 
 17   
 18 
 19     const char* pszFile = "C:\\Test.img";
 20 
 21     GDALDataset *poDataset;
 22 
 23     //使用只读方式打开图像
 24 
 25     poDataset = (GDALDataset*) GDALOpen( pszFile,GA_ReadOnly );
 26 
 27     if( poDataset == NULL )
 28 
 29     {
 30 
 31         printf( "File: %s不能打开!\n",pszFile);
 32 
 33         return 0;
 34 
 35     }
 36 
 37  
 38 
 39     //输出图像的格式信息
 40 
 41     printf( "Driver:%s/%s\n",
 42 
 43         poDataset->GetDriver()->GetDescription(),
 44 
 45         poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME) );
 46 
 47  
 48 
 49     //输出图像的大小和波段个数
 50 
 51     printf( "Size is%dx%dx%d\n",
 52 
 53         poDataset->GetRasterXSize(),poDataset->GetRasterYSize(),
 54 
 55         poDataset->GetRasterCount());
 56 
 57  
 58 
 59     //输出图像的投影信息
 60 
 61     if( poDataset->GetProjectionRef() != NULL )
 62 
 63         printf( "Projectionis `%s'\n", poDataset->GetProjectionRef() );
 64 
 65  
 66 
 67     //输出图像的坐标和分辨率信息
 68 
 69     double adfGeoTransform[6];
 70 
 71     if( poDataset->GetGeoTransform( adfGeoTransform) == CE_None )
 72 
 73     {
 74 
 75         printf( "Origin =(%.6f,%.6f)\n",
 76 
 77             adfGeoTransform[0], adfGeoTransform[3]);
 78 
 79  
 80 
 81         printf( "PixelSize = (%.6f,%.6f)\n",
 82 
 83             adfGeoTransform[1], adfGeoTransform[5]);
 84 
 85     }
 86 
 87  
 88 
 89     GDALRasterBand *poBand;
 90 
 91     int            nBlockXSize, nBlockYSize;
 92 
 93     int            bGotMin, bGotMax;
 94 
 95     double         adfMinMax[2];
 96 
 97  
 98 
 99     //读取第一个波段
100 
101     poBand = poDataset->GetRasterBand( 1 );
102 
103  
104 
105     //获取图像的块大小并输出
106 
107     poBand->GetBlockSize(&nBlockXSize, &nBlockYSize );
108 
109     printf( "Block=%dx%dType=%s, ColorInterp=%s\n",
110 
111         nBlockXSize, nBlockYSize,
112 
113         GDALGetDataTypeName(poBand->GetRasterDataType()),
114 
115         GDALGetColorInterpretationName(
116 
117         poBand->GetColorInterpretation()));
118 
119  
120 
121     //获取该波段的最大值最小值,如果获取失败,则进行统计
122 
123     adfMinMax[0] = poBand->GetMinimum( &bGotMin);
124 
125     adfMinMax[1] = poBand->GetMaximum( &bGotMax);
126 
127  
128 
129     if( ! (bGotMin&& bGotMax) )
130 
131         GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
132 
133  
134 
135     printf( "Min=%.3fd,Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
136 
137  
138 
139     //输出图像的金字塔信息
140 
141     if( poBand->GetOverviewCount() > 0 )
142 
143         printf( "Band has%d overviews.\n", poBand->GetOverviewCount() );
144 
145  
146 
147     //输出图像的颜色表信息
148 
149     if( poBand->GetColorTable() != NULL)
150 
151         printf( "Band hasa color table with %d entries.\n",
152 
153         poBand->GetColorTable()->GetColorEntryCount() );
154 
155  
156 
157     float *pafScanline;
158 
159     int   nXSize = poBand->GetXSize();
160 
161   
162 
163     //读取图像的第一行数据
164 
165     pafScanline = (float*) CPLMalloc(sizeof(float)*nXSize);
166 
167     poBand->RasterIO(GF_Read, 0, 0, nXSize,1,
168 
169         pafScanline, nXSize,1, GDT_Float32, 0, 0 );
170 
171  
172 
173     CPLFree(pafScanline);
174 
175  
176 
177     //关闭文件
178 
179     GDALClose((GDALDatasetH)poDataset);
180 
181 }

 运行结果:

 参考链接:http://blog.csdn.net/liminlu0314/article/details/7072007#t2

原文地址:https://www.cnblogs.com/huanzi/p/2731197.html