Skip to content

Instantly share code, notes, and snippets.

@bradbrowne
Created November 21, 2012 04:44
Show Gist options
  • Save bradbrowne/4123054 to your computer and use it in GitHub Desktop.
Save bradbrowne/4123054 to your computer and use it in GitHub Desktop.
Configure OGR for C# in LINQPad
void Main()
{
Configure();
for (int i = 0; i < OSGeo.OGR.Ogr.GetDriverCount(); i++)
{
OSGeo.OGR.Ogr.GetDriver(i).Dump();
}
}
public static void Configure()
{
string msPath = @"C:\Code\release-1600-gdal-1-9-mapserver-6-2";
string pathEnv = Environment.GetEnvironmentVariable("PATH");
if (Directory.Exists(msPath))
{
string[] paths = new string[]
{
Path.Combine(msPath, @"bin"),
Path.Combine(msPath, @"bin\gdal\plugins"),
Path.Combine(msPath, @"bin\gdal\csharp"),
Path.Combine(msPath, @"bin\ms\csharp")
};
foreach (string path in paths)
{
if (!pathEnv.Contains(path))
{
if (Directory.Exists(path))
{
pathEnv = path + ";" + pathEnv;
Environment.SetEnvironmentVariable("PATH", pathEnv);
}
else
{
throw new DirectoryNotFoundException(string.Format("'{0}' directory not found.", path));
}
}
}
// Path to directory containing various GDAL data files
// (EPSG CSV files, S-57 definition files, DXF header and footer files, ...).
// This option is read by the GDAL and OGR driver registration functions.
// It is used to expand EPSG codes into their description in the OSR model (WKT based).
string GDAL_DATA = Path.Combine(msPath, @"bin\gdal-data");
Environment.SetEnvironmentVariable("GDAL_DATA", GDAL_DATA);
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_DATA", GDAL_DATA);
// Path to directory containing driver files that start with the prefix "gdal_X.dll".
string GDAL_DRIVER_PATH = Path.Combine(msPath, @"bin\gdal\plugins");
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", GDAL_DRIVER_PATH);
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_DRIVER_PATH", GDAL_DRIVER_PATH);
// Path to directory containing EPSG files for the Proj.4 library.
string PROJ_LIB = Path.Combine(msPath, @"bin\proj\SHARE");
Environment.SetEnvironmentVariable("PROJ_LIB", PROJ_LIB);
OSGeo.GDAL.Gdal.SetConfigOption("PROJ_LIB", PROJ_LIB);
// This line throws an exception if the the wrong version of GDal was found in the path somewhere,
// or the path didn't point to GDal correctly.
OSGeo.OGR.Ogr.RegisterAll();
}
else
{
throw new DirectoryNotFoundException(string.Format("'{0}' directory not found.", msPath));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment