GDAL/OGR安装--转载
转自:https://blog.csdn.net/mygisforum/article/details/22478491
一、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)。