一、GDAL C# 部分資源及參考
二、GDAL C# DLL 下載
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目錄下。
三、常見異常解決
原因分析:1.gdal初始化時,因其依賴dll項不全導致註冊失敗拋出異常,可採用Dependency Walker工具查看相關依賴項。簡單的把九個DLL和找到的所有依賴項拷貝到debug目錄下,通常也是不能解決問題的。
2.如果同一個應用程序的主程序與其所引用的類庫使用不同版本的gdal csharp dll 文件,會出現同樣的異常。
3.綜合分析此異常是因其依賴的dll 和 配置信息引起的,且dll 與配置相關的文件必須對應。
解決方法:採用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;
}
}
}
四、GDAL讀寫Shape數據時的中文亂碼問題
// 設置Shp字段、屬性等的編碼爲空
Gdal.SetConfigOption("SHAPE_ENCODING", "");
// 解決Shp文件中文路徑亂碼,無法讀取的問題(有時去掉卻可以識別中文,待解決)
Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");//不能完全解決中文路徑的問題,路徑包含奇數箇中文時通常都無法正確識別,偶數則正常
注:如果使用上面第三部分描述的方法,仍存在類型初始值異常問題,請把程序debug目錄下以非_csharp結尾的dll刪掉即可(代碼已自動匹配gdal文件夾下的相關dll)。