1.编译后的DLL下载地址:http://www.gisinternals.com/sdk/,在“GDAL and MapServer lasted release version”中下载最新版本。
2.点击后进入下载页面:
http://www.gisinternals.com/sdk/PackageList.aspx?file=release-1400-gdal-1-10-1-mapserver-6-4-1.zip
3.编译后的DLL除了gdal110.dll及其依赖项位于bin目录下,其余的均在压缩包中的【bin\gdal\csharp\...】目录下:
4.开发时把以“_csharp.dll”结尾的dll库添加到项目引用中,其余的拷贝到debug目录下。
原因分析:gdal初始化时,因其依赖dll项不全导致注册失败抛出异常,可采用Dependency Walker工具查看相关依赖项。简单的把九个DLL和找到的所有依赖项拷贝到debug目录下,通常也是不能解决问题的。
解决方法:采用SharpMap的GDAL初始化方法解决,需要两个数据:
1. GdalConfiguration.cs
2. gdal_data_config.rar
注:以上两文件下载地址:gdal_csharp开发环境配置文件
第一步:将GdalConfiguration.cs添加到项目中,然后解压gdal_data_config.rar到debug目录下,文件夹名称为gdal。
第二步:在使用Gdal.AllRegister()初始化前,调用以下两句代码进行相关初始化数据的配置即可。
SharpMap.GdalConfiguration.ConfigureGdal();
SharpMap.GdalConfiguration.ConfigureOgr();
3.GdalConfiguration类的内容如下:/****************************************************************************** * * Name: GdalConfiguration.cs.pp * Project: GDAL CSharp Interface * Purpose: A static configuration utility class to enable GDAL/OGR. * Author: Felix Obermaier * *****************************************************************************/ using System; using System.IO; using System.Reflection; using Gdal = OSGeo.GDAL.Gdal; using Ogr = OSGeo.OGR.Ogr; namespace SharpMap { public static partial class GdalConfiguration { private static bool _configuredOgr; private static bool _configuredGdal; /// <summary> /// Function to determine which platform we‘re on /// </summary> private static string GetPlatform() { return IntPtr.Size == 4 ? "x86" : "x64"; } /// <summary> /// Construction of Gdal/Ogr /// </summary> static GdalConfiguration() { var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath; var executingDirectory = Path.GetDirectoryName(executingAssemblyFile); if (string.IsNullOrEmpty(executingDirectory)) throw new InvalidOperationException("cannot get executing directory"); var gdalPath = Path.Combine(executingDirectory, "gdal"); var nativePath = Path.Combine(gdalPath, GetPlatform()); // Prepend native path to environment path, to ensure the // right libs are being used. var path = Environment.GetEnvironmentVariable("PATH"); path = nativePath + ";" + Path.Combine(nativePath, "plugins") + ";" + path; Environment.SetEnvironmentVariable("PATH", path); // Set the additional GDAL environment variables. var gdalData = Path.Combine(gdalPath, "data"); Environment.SetEnvironmentVariable("GDAL_DATA", gdalData); Gdal.SetConfigOption("GDAL_DATA", gdalData); var driverPath = Path.Combine(nativePath, "plugins"); Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath); Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath); Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData); Gdal.SetConfigOption("GEOTIFF_CSV", gdalData); var projSharePath = Path.Combine(gdalPath, "share"); Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath); Gdal.SetConfigOption("PROJ_LIB", projSharePath); } /// <summary> /// Method to ensure the static constructor is being called. /// </summary> /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks> public static void ConfigureOgr() { if (_configuredOgr) return; // Register drivers Ogr.RegisterAll(); _configuredOgr = true; } /// <summary> /// Method to ensure the static constructor is being called. /// </summary> /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks> public static void ConfigureGdal() { if (_configuredGdal) return; // Register drivers Gdal.AllRegister(); _configuredGdal = true; } } }
原文:http://blog.csdn.net/mygisforum/article/details/22478491