• GDAL/OGR安装--转载


    转自:https://blog.csdn.net/mygisforum/article/details/22478491

    一、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目录下,其余的均在压缩包中的【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

    注:以上两文件下载地址:gdal_csharp开发环境配置文件

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

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

    SharpMap.GdalConfiguration.ConfigureGdal();

    SharpMap.GdalConfiguration.ConfigureOgr();

     3.GdalConfiguration类的内容如下:

    [csharp] view plain copy
     
    1. /****************************************************************************** 
    2.  * 
    3.  * Name:     GdalConfiguration.cs.pp 
    4.  * Project:  GDAL CSharp Interface 
    5.  * Purpose:  A static configuration utility class to enable GDAL/OGR. 
    6.  * Author:   Felix Obermaier 
    7.  * 
    8.  *****************************************************************************/  
    9.   
    10. using System;  
    11. using System.IO;  
    12. using System.Reflection;  
    13. using Gdal = OSGeo.GDAL.Gdal;  
    14. using Ogr = OSGeo.OGR.Ogr;  
    15.   
    16. namespace SharpMap  
    17. {  
    18.     public static partial class GdalConfiguration  
    19.     {  
    20.         private static bool _configuredOgr;  
    21.         private static bool _configuredGdal;  
    22.   
    23.         /// <summary>  
    24.         /// Function to determine which platform we're on  
    25.         /// </summary>  
    26.         private static string GetPlatform()  
    27.         {  
    28.             return IntPtr.Size == 4 ? "x86" : "x64";  
    29.         }  
    30.   
    31.         /// <summary>  
    32.         /// Construction of Gdal/Ogr  
    33.         /// </summary>  
    34.         static GdalConfiguration()  
    35.         {  
    36.             var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;  
    37.             var executingDirectory = Path.GetDirectoryName(executingAssemblyFile);  
    38.   
    39.             if (string.IsNullOrEmpty(executingDirectory))  
    40.                 throw new InvalidOperationException("cannot get executing directory");  
    41.   
    42.   
    43.             var gdalPath = Path.Combine(executingDirectory, "gdal");  
    44.             var nativePath = Path.Combine(gdalPath, GetPlatform());  
    45.   
    46.             // Prepend native path to environment path, to ensure the  
    47.             // right libs are being used.  
    48.             var path = Environment.GetEnvironmentVariable("PATH");  
    49.             path = nativePath + ";" + Path.Combine(nativePath, "plugins") + ";" + path;  
    50.             Environment.SetEnvironmentVariable("PATH", path);  
    51.   
    52.             // Set the additional GDAL environment variables.  
    53.             var gdalData = Path.Combine(gdalPath, "data");  
    54.             Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);  
    55.             Gdal.SetConfigOption("GDAL_DATA", gdalData);  
    56.   
    57.             var driverPath = Path.Combine(nativePath, "plugins");  
    58.             Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath);  
    59.             Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath);  
    60.   
    61.             Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData);  
    62.             Gdal.SetConfigOption("GEOTIFF_CSV", gdalData);  
    63.   
    64.             var projSharePath = Path.Combine(gdalPath, "share");  
    65.             Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath);  
    66.             Gdal.SetConfigOption("PROJ_LIB", projSharePath);  
    67.         }  
    68.   
    69.         /// <summary>  
    70.         /// Method to ensure the static constructor is being called.  
    71.         /// </summary>  
    72.         /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>  
    73.         public static void ConfigureOgr()  
    74.         {  
    75.             if (_configuredOgr) return;  
    76.   
    77.             // Register drivers  
    78.             Ogr.RegisterAll();  
    79.             _configuredOgr = true;  
    80.         }  
    81.   
    82.         /// <summary>  
    83.         /// Method to ensure the static constructor is being called.  
    84.         /// </summary>  
    85.         /// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>  
    86.         public static void ConfigureGdal()  
    87.         {  
    88.             if (_configuredGdal) return;  
    89.   
    90.             // Register drivers  
    91.             Gdal.AllRegister();  
    92.             _configuredGdal = true;  
    93.         }  
    94.     }  
    95. }  

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

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

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

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

  • 相关阅读:
    在Excel中查找/替换时使用换行符
    用fieldset标签轻松实现Tab选项卡效果
    用JSFL将Flash中的元件导出为PNG
    PHP学习笔记之PDO
    网页中的数学公式
    解决fl.findObjectInDocByType/fl.findObjectInDocByName的毛病
    HTML+CSS 网页中鼠标滚轮失效的解决办法
    jQuery 离开页面时提示
    ASP 计算出指定年份生肖
    au3创建Access数据库表例子
  • 原文地址:https://www.cnblogs.com/niudieyi/p/8706852.html
Copyright © 2020-2023  润新知