GDAL needs to find its support files: the GDAL_DATA folder with coordinate system and driver metadata, the plugin folder with optional drivers, and the PROJ database used for reprojection. When it cannot find them you get errors such as “Unable to open EPSG support file gcs.csv” or “PROJ: proj_create_from_database: Cannot find proj.db”, or drivers that silently do not appear.
Setting these paths in your own code, relative to the app’s folder, means the program works without the user having to set system-wide environment variables.
Use Gdal.SetConfigOption
The GDAL C# bindings (OSGeo.GDAL) expose GDAL’s configuration options directly. GDAL checks these before it looks at environment variables, so this is the most reliable way:
using OSGeo.GDAL;
using OSGeo.OSR;
string gdalRoot = Path.Combine(AppContext.BaseDirectory, "gdal");
SetIfMissing("GDAL_DATA", Path.Combine(gdalRoot, "data"));
SetIfMissing("GDAL_DRIVER_PATH", Path.Combine(gdalRoot, "plugins"));
// PROJ 6 and later (GDAL 3 and later) looks for proj.db here.
Osr.SetPROJSearchPaths([Path.Combine(gdalRoot, "proj")]);
// Register drivers only after the paths are set.
Gdal.AllRegister();
static void SetIfMissing(string name, string value)
{
// Respect a value the user or the deployment already set.
if (string.IsNullOrEmpty(Gdal.GetConfigOption(name, null)))
{
Gdal.SetConfigOption(name, value);
}
}
Adjust the folder names to your GDAL distribution; builds differ in where they put the data and plugin directories. Gdal.GetConfigOption returns the configured value or, if there is none, the environment variable of the same name, so the check above also respects values set outside the program.
Order matters
GDAL_DRIVER_PATH is read when Gdal.AllRegister() loads the plugin drivers. Set it afterwards and the plugins never load. GDAL_DATA is read lazily, the first time GDAL needs a support file, but setting everything before AllRegister() keeps you from having to remember which option is which.
Why not Environment.SetEnvironmentVariable
The original version of this post used Environment.SetEnvironmentVariable:
if (Environment.GetEnvironmentVariable("GDAL_DATA") is null)
{
Environment.SetEnvironmentVariable("GDAL_DATA", Path.Combine(gdalRoot, "data"));
}
It sets the variable for the current process only, which is what you want, and it often works on Windows if it runs before GDAL is loaded. It has two weaknesses. On Linux and macOS, .NET keeps its own copy of the environment and SetEnvironmentVariable does not update the native one, so GDAL’s C code never sees the value. And even on Windows, native code that has already cached its environment will not notice the change. Gdal.SetConfigOption talks to GDAL directly and avoids both problems.
The old post also set GEOTIFF_CSV. That was needed by GDAL 2.x builds; GDAL 3 and later get the GeoTIFF coordinate definitions from PROJ instead, so you can drop it. The PROJ equivalent of GDAL_DATA is the PROJ_DATA variable (PROJ_LIB before PROJ 9.1), which Osr.SetPROJSearchPaths replaces.
Using a NuGet package
If you get GDAL from the MaxRev.Gdal.Core package together with its runtime packages for Windows, Linux or macOS, you do not need any of this. Call its helper once at startup and it configures the data, plugin and PROJ paths for you:
using MaxRev.Gdal.Core;
GdalBase.ConfigureAll();
Related examples
- Get the file list from a directory
- GDAL configuration options in the GDAL documentation
- Environment.SetEnvironmentVariable on Microsoft Learn