【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 文件的文件夹里面)


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值