随笔 - 272  文章 - 7  评论 - 27  阅读 - 83万

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\nmake.opt

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

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

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

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

 

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

 

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

 

打开 D:\1.10.1\swig\csharp\gdal\GdalPINVOKE.cs

         D:\1.10.1\swig\csharp\ogr\OgrPINVOKE.cs

         D:\1.10.1\swig\csharp\osr\OsrPINVOKE.cs 

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

 

打开D:\1.10.1\swig\csharp\gdal\Band.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 swig\csharp

                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:标识生成调试信息

posted on   NLazyo  阅读(771)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示