年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

186 lines
6.4 KiB

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 Kingo.Plugin.MakeTaskPackage.Entity
{
public static partial class GdalConfiguration
{
private static volatile bool _configuredOgr;
private static volatile bool _configuredGdal;
private static volatile bool _usable;
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetDefaultDllDirectories(uint directoryFlags);
// LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_LIBRARY_SEARCH_SYSTEM32
private const uint DllSearchFlags = 0x00000400 | 0x00000800;
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AddDllDirectory(string lpPathName);
/// <summary>
/// Construction of Gdal/Ogr
/// </summary>
static GdalConfiguration()
{
string executingDirectory = null, gdalPath = null, nativePath = null;
try
{
if (!IsWindows)
{
const string notSet = "_Not_set_";
string tmp = Gdal.GetConfigOption("GDAL_DATA", notSet);
_usable = tmp != notSet;
return;
}
string executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
executingDirectory = Path.GetDirectoryName(executingAssemblyFile);
if (string.IsNullOrEmpty(executingDirectory))
throw new InvalidOperationException("cannot get executing directory");
// modify search place and order
SetDefaultDllDirectories(DllSearchFlags);
gdalPath = Path.Combine(executingDirectory, "gdal");
nativePath = Path.Combine(gdalPath, GetPlatform());
if (!Directory.Exists(nativePath))
throw new DirectoryNotFoundException($"GDAL native directory not found at '{nativePath}'");
if (!File.Exists(Path.Combine(nativePath, "gdal_wrap.dll")))
throw new FileNotFoundException(
$"GDAL native wrapper file not found at '{Path.Combine(nativePath, "gdal_wrap.dll")}'");
// Add directories
AddDllDirectory(nativePath);
AddDllDirectory(Path.Combine(nativePath, "plugins"));
// Set the additional GDAL environment variables.
string gdalData = Path.Combine(gdalPath, "data");
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
Gdal.SetConfigOption("GDAL_DATA", gdalData);
string 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);
string projSharePath = Path.Combine(gdalPath, "share");
Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath);
Gdal.SetConfigOption("PROJ_LIB", projSharePath);
_usable = true;
}
catch (Exception e)
{
_usable = false;
Trace.WriteLine(e, "error");
Trace.WriteLine($"Executing directory: {executingDirectory}", "error");
Trace.WriteLine($"gdal directory: {gdalPath}", "error");
Trace.WriteLine($"native directory: {nativePath}", "error");
//throw;
}
}
/// <summary>
/// Gets a value indicating if the GDAL package is set up properly.
/// </summary>
public static bool Usable
{
get { return _usable; }
}
/// <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 (!_usable) return;
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 (!_usable) return;
if (_configuredGdal) return;
// Register drivers
Gdal.AllRegister();
_configuredGdal = true;
PrintDriversGdal();
}
/// <summary>
/// Function to determine which platform we're on
/// </summary>
private static string GetPlatform()
{
return Environment.Is64BitProcess ? "x64" : "x86";
}
/// <summary>
/// Gets a value indicating if we are on a windows platform
/// </summary>
private static bool IsWindows
{
get
{
var res = !(Environment.OSVersion.Platform == PlatformID.Unix ||
Environment.OSVersion.Platform == PlatformID.MacOSX);
return res;
}
}
private static void PrintDriversOgr()
{
#if DEBUG
if (_usable)
{
var num = Ogr.GetDriverCount();
for (var i = 0; i < num; i++)
{
var driver = Ogr.GetDriver(i);
Trace.WriteLine($"OGR {i}: {driver.GetName()}", "Debug");
}
}
#endif
}
private static void PrintDriversGdal()
{
#if DEBUG
if (_usable)
{
var num = Gdal.GetDriverCount();
for (var i = 0; i < num; i++)
{
var driver = Gdal.GetDriver(i);
Trace.WriteLine($"GDAL {i}: {driver.ShortName}-{driver.LongName}");
}
}
#endif
}
}
}