栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

gdal依赖dll

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

gdal依赖dll

一、GDAL C# 部分资源及参考

二、GDAL C# DLL 下载

1.编译后的DLL下载地址:http://www.gisinternals.com/sdk/,在“GDAL and MapServer lasted release version”中下载最新版本。

2.点击后进入下载页面:

3.编译后的DLL除了gdal110.dll及其依赖项位于bin目录下,其余的均在压缩包中的【bingdalcsharp...】目录下:

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

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

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

SharpMap.GdalConfiguration.ConfigureGdal();

SharpMap.GdalConfiguration.ConfigureOgr();

3.GdalConfiguration类的内容如下:

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 ? "x86" : "x64";

}

/// 

/// 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("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);

}

/// 

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

}

/// 

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

}

}

}

四、GDAL读写Shape数据时的中文乱码问题

// 设置Shp字段、属性等的编码为空Gdal.SetConfigOption("SHAPE_ENCODING", "");

// 解决Shp文件中文路径乱码,无法读取的问题(有时去掉却可以识别中文,待解决)Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");//不能完全解决中文路径的问题,路径包含奇数个中文时通常都无法正确识别,偶数则正常

注:如果使用上面第三部分描述的方法,仍存在类型初始值异常问题,请把程序debug目录下以非_csharp结尾的dll删掉即可(代码已自动匹配gdal文件夹下的相关dll)。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911537.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号