GDAL - CSharp环境配置

更新时间:2023-12-16 15:34:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

一、GDAL C# DLL下载

http://www.gisinternals.com/sdk/

http://www.gisinternals.com/sdk/PackageList.aspx?file=release-1400-gdal-1-10-1-mapserver-6-4-1.zip

位于压缩包中的位置:bin\\gdal\\csharp\\...目录下:

开发时把以_csharp.dll结尾的添加到项目引用,其余的拷贝到bebug目录下。

二、在调用Gdal.AllRegister() 方法时报异常:“OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发

异常。

原因分析:gdal初始化时,其依赖dll项不全导致异常,可采用Dependency Walker工具查看相关依赖项。把九个DLL拷贝到debug是不能解决问题的。

解决方法:

采用SharpMap的GDAL初始化方法,需要两个数据: 1. GdalConfiguration.cs 2. gdal_data_config.rar

第一步:将GdalConfiguration.cs添加到项目中,然后解压gdal_data_config.rar到debug目录下,文件夹名称为gdal。

第二步:在使用Gdal.AllRegister()初始化前,调用以下两句代码进行相关初始化数据的配置即可。

SharpMap.GdalConfiguration.ConfigureGdal(); SharpMap.GdalConfiguration.ConfigureOgr();

附件1:GdalConfiguration.cs

/****************************************************************************** *

* 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;

///

/// Function to determine which platform we're on ///

private static string GetPlatform() {

return IntPtr.Size == 4 ? \ : \; }

///

/// Construction of Gdal/Ogr ///

static GdalConfiguration() {

var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath; var executingDirectory = Path.GetDirectoryName(executingAssemblyFile);

if (string.IsNullOrEmpty(executingDirectory))

throw new InvalidOperationException(\);

var gdalPath = Path.Combine(executingDirectory, \); 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 = nativePath + \ + Path.Combine(nativePath, \) + \ + path; Environment.SetEnvironmentVariable(\, path);

// Set the additional GDAL environment variables. var gdalData = Path.Combine(gdalPath, \);

Environment.SetEnvironmentVariable(\, gdalData); Gdal.SetConfigOption(\, gdalData);

var driverPath = Path.Combine(nativePath, \);

Environment.SetEnvironmentVariable(\, driverPath); Gdal.SetConfigOption(\, driverPath);

Environment.SetEnvironmentVariable(\, gdalData); Gdal.SetConfigOption(\, gdalData);

var projSharePath = Path.Combine(gdalPath, \);

Environment.SetEnvironmentVariable(\, projSharePath); Gdal.SetConfigOption(\, projSharePath); }

///

/// Method to ensure the static constructor is being called. ///

/// Be sure to call this function before using Gdal/Ogr/Osr public static void ConfigureOgr() {

if (_configuredOgr) return;

// Register drivers Ogr.RegisterAll(); _configuredOgr = true;

PrintDriversOgr(); }

///

/// Method to ensure the static constructor is being called. ///

/// Be sure to call this function before using Gdal/Ogr/Osr public static void ConfigureGdal() {

if (_configuredGdal) return;

// Register drivers Gdal.AllRegister(); _configuredGdal = true;

PrintDriversGdal(); }

private static void PrintDriversOgr() { #if DEBUG

var num = Ogr.GetDriverCount(); for (var i = 0; i < num; i++) {

var driver = Ogr.GetDriver(i);

Console.WriteLine(string.Format(\, i, driver.name)); } #endif }

private static void PrintDriversGdal() { #if DEBUG

var num = Gdal.GetDriverCount(); for (var i = 0; i < num; i++) {

var driver = Gdal.GetDriver(i);

Console.WriteLine(string.Format(\, i, driver.ShortName, driver.LongName)); } #endif } } }

本文来源:https://www.bwwdw.com/article/h0d5.html

Top