GDAL C# 開發環境配置
一、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目錄下。
三、常見異常解決
異常描述:在完成了以上步驟後,通常仍然不能正常進行開發,經常在呼叫 Gdal.AllRegister()方法時會丟擲如下異常: “OSGeo.GDAL.GdalPINVOKE”的型別初始值設定項引發異常。原因分析: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)。