This project has moved and is read-only. For the latest updates, please go here.

GDAL Extension Throwing error, installed the nugets etc still at a loss

Topics: SharpMap v2.0
Jan 3, 2014 at 12:03 PM
Edited Jan 5, 2014 at 1:07 PM
I am trying to create a rasterlayer from the example geotiffs. I have downloaded the GDAL native and plugins NuGets. However whenever I run the project it always returns a not very helpful error: "The type initializer for 'SharpMap.Layers.GdalRasterLayer' threw an exception."

I know the sharp map elements are working as I can load in a vector layer and display it no problem its just the GDAL side.

This is the code I am using. I have checked and the GDAL directories are included in the bin folder when running.
    LayerGroup group = new LayerGroup("OS");
    //group.SRID = 27700;
    group.LayerName = "Raster250";

    var di = new DirectoryInfo(@"C:\OS Maps Current\GeoTiff");
    foreach (var fi in di.GetFiles("*.tif"))
            SharpMap.Layers.GdalRasterLayer layer = new GdalRasterLayer(
                fi.Name, fi.FullName);
        catch (Exception ex)

I have tried it with both the OS 1:250k Raster images and the images supplied in the example section of the trunk. Could it be something to do with the projections, I have set it on the LayerGroup and Layer and still no luck.

Any ideas gratefully received.



I have managed to create a winforms application and got it working. I have recreated it in a new MVC4 & 5 Web Application and I am still getting the same error. So it must be something to do with it being in a MVC project and not winforms.
Jan 5, 2014 at 2:03 PM
Edited Jan 5, 2014 at 2:04 PM
I'm not an expert on web projects at all, but maybe you need some setting to allow native libraries to be loaded?
IIRC the web example project does display gdal raster layers and ogr provider based libraries.
Jan 5, 2014 at 3:53 PM
Thanks, done some searching and I managed to get a working class library which when accessed from a winforms works perfectly when I access it from MVC it throws the error. I turned on Debug unmanged code in the config and it seem it cannot find gdal_warp.dll. I am guessing that is causing the issues.
Jan 5, 2014 at 6:41 PM
SharpMap.Extensions is looking for gdal_wrap.dll in a bin/x86 or bin/x64 folder.
The Gdal.Native package should take care of copying the files to that location.
Jan 5, 2014 at 6:47 PM

Yeh you put me on the right path, I found the answer here made sure all of these files were there and hey presto it works.

FObermaier thanks for the help.

Marked as answer by cmwb2000 on 1/5/2014 at 10:47 AM
Jan 6, 2014 at 4:01 PM
For my information, could you tell me where the native libs were and where you put them?
Jan 6, 2014 at 8:20 PM
90% of them I found in the SharpMap.Extension there are a couple that are in the FWTools download. What I think really sorted it was putting the path to the FWTools/Bin directory in the System Environment Path Variable. I tried without it today at work and it wouldn't have it,

Hope that helps.

Jan 6, 2014 at 8:25 PM
Using FWTools is deprecated. Don't do that.
Have you added GDAL.Native nuget package to your main MVC Website project, I mean the one you are actually executing. That is what you should have done.
Jan 6, 2014 at 8:31 PM
Yeh I added in the Gdal.Native nuget package but it wouldnt see the dependencies even in the Bin Director when compiled. I will keep on working on it.
Jan 6, 2014 at 10:26 PM
Edited Jan 6, 2014 at 10:26 PM
I found out what it is. The SharpMap.Extensions are looking in the bin\gdal[x86|x64] folders for the native libraries. The installation script copies them to the bin[x86|x64] folder.

You can either
  • fix the code in GdalConfiguration class *) or
  • move the files around in your _bin_deployableAssemblies folder
/// <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");
    if (!Directory.Exists(gdalPath)) gdalPath = executingDirectory;