c# 使用GDAL处理大图

注意问题:

1.GDAL 使用官网生成好的dll,必须把Bin目录下的dll一并加到执行目录下去,否则会出错。

 2. 用环境变量设置引用路径可以避免一大堆dll放一起。代码如下:

 /// <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>
        public static void Gdal_Configuration()
        {
            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 + ";" ;
            Environment.SetEnvironmentVariable("PATH", path);
            Gdal.AllRegister();
           
        }

3.最好使用自己手动编译的dll,会少很多没使用到的dll,只用9个dll。

4.用GDAL的用户控件,第二次拖动控件进窗体后会造成“未能加载工具箱项,将从列表中移除”的问题,建议代码手动添加吧,是非托管dll的问题。

编译步骤如下:

首先,下载GDAL源码,官网下即可。

打开D:gdal make.opt

修改54行: GDAL_HOME = "D:GDAL"。(编译生成文件的保存路径)

       83行: SWIG = D:swigwin-2.0.4swig.exe(必须是swigwin.exe的完整路径)。

      153行: "#WIN64=YES",去掉#。注意保存。

      675行:"SYM_PREFIX=_",去掉最后面的下划线。

打开D:gdalmakefile.vc,修改23~26行,将“_”改为$(SYM_PREFIX)。如图:

打开D:1.10.1swigcsharpAssemblyInfo.cs文件,将94行代码注释掉,解决安全透明代码无法调用的问题。

打开 D:1.10.1swigcsharpgdalGdalPINVOKE.cs

         D:1.10.1swigcsharpogrOgrPINVOKE.cs

         D:1.10.1swigcsharposrOsrPINVOKE.cs 

             修改188~193行:将重复的代码注释掉,解决接口重定义的问题。

打开D:1.10.1swigcsharpgdalBand.cs|Dataset.cs|Driver.cs,修改第17行,解决接口成员名错误问题。

 public Band(IntPtr cPtr, bool cMemoryOwn, object parent) : base(GdalPINVOKE.Band_SWIGUpcast(cPtr), cMemoryOwn, parent)

 public Dataset(IntPtr cPtr, bool cMemoryOwn, object parent) : base(GdalPINVOKE.Dataset_SWIGUpcast(cPtr), cMemoryOwn, parent)

 public Driver(IntPtr cPtr, bool cMemoryOwn, object parent) : base(GdalPINVOKE.Driver_SWIGUpcast(cPtr), cMemoryOwn, parent) {

3、编译

开始—所有程序—Microsoft Visual Studio 2010—Visual Studio Tools—Visual Studio x64兼容工具命令提示(2010)

打开命令行工具,cd d:gdal-1.10.1

然后执行 nmake /f makefile.vc

                nmake /f makefile.vc install

                nmake /f makefile.vc devinstall

注:编译可能要费一些时间,不要着急。

以上是完成了C++的编译,要再进入csharp编译。

执行         cd swigcsharp

                nmake /f makefile.vc

            (运行这一步有问题的话,加以下两句:namke /f makefile.vc clear 、nmake /f makefile.vc interface)

                nmake /f makefile.vc install

正常情况下可以编译成功。


命令参数说明:

使用命令:nmake -f makefile.vc MSVC_VER=1600 DEBUG=1 ANALYZE=1 WITH_PDB=1 可以设置使用的c++版本
MSVC_VER:VC++的版本,下面是对应关系
1900 = 14.0(2015)
1800 = 12.0(2013)
1700 = 11.0(2012)
1600 = 10.0(2010)
1500 = 9.0 (2008)
1400 = 8.0 (2005) - specific compilation flags, different from older VC++
1310 = 7.1 (2003)
1300 = 7.0 (2002)
1200 = 6.0
DEBUG:bebug版本标识,不使用此参数,默认为Release
ANALYZE=1:对GDAL代码进行分析,这个一般不用
WITH_PDB=1:标识生成调试信息

原文地址:https://www.cnblogs.com/bile/p/7325952.html