GDAL C# 开发环境配置 您所在的位置:网站首页 csharp开发环境 GDAL C# 开发环境配置

GDAL C# 开发环境配置

2023-10-09 18:18| 来源: 网络整理| 查看: 265

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

1.GDAL/OGR In CSharp官网主页 

2.GDAL CSharp 编译后的dll 下载地址

3.一个不错的帮助文档gdal api document 

4.官网提供的csharp实例代码片段

5.GDAL Raster Formats

二、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; /// /// 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)。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有