【GDAL】C# 调用 gdal 库

c# 项目使用GDAL:

VS2019从NuGet中导入的 “GDAL” 和 “GDAL.Native” 应用到项目中出错,将解决方法记录一下

解决步骤:

Step1:导入“GDAL” 和 “GDAL.Native”
    点击  工具 > NuGet 包管理器 > 管理解决方案的NuGet程序包
    在 “浏览” 中搜索 “GDAL”,安装 “GDAL” 和 “GDAL.Native” 到项目中
安装GDAL依赖
Step2:安装完成后,项目的解决方案中会出现一个 “GdalConfiguration.cs”,点击后将命名空间中的函数全部删除,替换成如下代码

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Gdal = OSGeo.GDAL.Gdal;
using Ogr = OSGeo.OGR.Ogr;

namespace Manager   //自己项目的命名空间
{
    /* 修改为下列内容 */
    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;
        }
    }
}

Step3:在自己的项目代码中的"Gdal.AllRegister();"代码前加上 GdalConfiguration.ConfigureGdal();和GdalConfiguration.ConfigureOgr();

	GdalConfiguration.ConfigureGdal();
	GdalConfiguration.ConfigureOgr();
	Gdal.AllRegister();
	Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
	//读取
	string filePath = "tif图的路径";
	Dataset ds = Gdal.Open(filePath, Access.GA_ReadOnly);
	if (ds != null)
	{
		Console.WriteLine("数据读取成功");
	}
	else
	{
		Console.WriteLine("数据读取失败");
	}

自此,调用GDAL的项目就可以运行了


其他方式使用GDAL

如果不使用NuGet中的包,还可以使用 GDAL 官网提供的源码,将其编译为dll文件使用。如果自己不会编译,可以 在此 下载已编译好的dll文件,具体过程如下

将 release-1911-x64-gdal-2-4-4-mapserver-7-4-3\bin\gdal\csharp 文件下的8个dll文件复制到自己项目的 debug 文件夹下(release-1911-x64-gdal-2-4-4-mapserver-7-4-3是从 网站 下载的,可以自己选择版本),release-1911-x64-gdal-2-4-4-mapserver-7-4-3\bin文件下的所有dll文件也复制过去此网站下载的只有编译好的 dll 文件,缺少 pdf 文件,可以在网上找同版本的 pdf ,也可以自行编译源码,生成 pdf 文件,将pdf文件也复制过去,但要和 dll 文件的版本一致,比如我下的是2.4.4版本的 dll 文件,那么下载 GDAL 源码的版本也得是 2.4.4 的,最后,在项目中引用 “gdal_csharp”、“gdalconst_csharp”、“ogr_csharp”、“osr_csharp”四个 dll 文件即可(这4个 dll 文件就在刚刚的8个 dll 文件的文件夹里面)


  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
语言的好处及其适用范围 C语言是一种非常流行的编程语言,同时又是一种非常灵活和高效的语言,其应用范围广泛。下面我们就一起来看看使用C语言的好处及其适用范围。 C语言的好处: 1.高效性:C语言是一种非常高效的编程语言,其执行速度比大多数其他编程语言快很多,因此对于需要进行处理大量数据和进行复杂计算的场景非常适合。 2.灵活性:C语言提供了非常多的工具和函数,同时又支持用户自定义函数和数据类型,因此可以让程序员根据自己的需要编写出非常灵活的程序。 3.可移植性:C语言可以在不同的操作系统和硬件平台上运行,因此可以让程序更加通用和适用于不同的计算环境。 4.易于学习:C语言的语法相对简单,模块化和结构化编程风格易于理解,因此入门门槛较低,易于学习。 C语言的适用范围: 1.系统软件开发:C语言由于其高效性和可移植性的特点,其广泛用于系统软件开发中,如操作系统、编译器等。 2.嵌入式系统开发:C语言在嵌入式系统开发中也非常流行,其可以用于编写驱动程序、操作系统、网络协议栈等等。 3.科学计算:C语言的执行速度非常快,其广泛用于科学计算领域,如数值分析、机器学习等等。 4.游戏开发:C语言由于其高效性,非常适合用于游戏开发中,其可以用于编写游戏引擎、游戏物理引擎等。 总之,使用C语言可以带来高效、灵活、可移植等多种好处,其可以应用于不同领域的开发工作中。当然,C语言也有其缺点,如容易出错、内存管理较为复杂等,因此使用C语言的开发人员需要具备一定的编程和操作系统相关知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值