La primera forma: agregarlo a través del paquete nuget.La última versión es 3.6.3, y la dependencia de .net framework es 461.
Primero descargue el paquete gdal.native en nuget
Una vez completada la descarga, se generará un archivo GdalConfiguration.cs en el proyecto, con la ruta del archivo de configuración. Reemplace el código en este archivo con lo siguiente:
public static partial class GdalConfiguration
{
private static volatile bool _configuredOgr;
private static volatile 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;
PrintDriversOgr();
}
/// <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;
PrintDriversGdal();
}
private static void PrintDriversOgr()
{
#if DEBUG
var num = Ogr.GetDriverCount();
for (var i = 0; i < num; i++)
{
var driver = Ogr.GetDriver(i);
Console.WriteLine(string.Format("OGR {0}: {1}", i, driver.name));
}
#endif
}
private static void PrintDriversGdal()
{
#if DEBUG
var num = Gdal.GetDriverCount();
for (var i = 0; i < num; i++)
{
var driver = Gdal.GetDriver(i);
Console.WriteLine(string.Format("GDAL {0}: {1}-{2}", i, driver.ShortName, driver.LongName));
}
#endif
}
}
Pruébelo: método de llamada
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
// Ogr.RegisterAll();
Geometry geom = Geometry.CreateFromWkt("POINT(47.0 19.2)");
La cadena anterior de códigos de llamada se ejecuta con éxito incluso si la configuración del entorno es correcta.
Agregue el caso de gdalC#: https://github.com/OSGeo/gdal/blob/master/swig/csharp
Sitio web oficial: https://gdal.org/api/csharp/index.html