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

Topics: SharpMap v2.0
Jan 3, 2014 at 11:03 AM
Edited Jan 5, 2014 at 12: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"))
    {
        try
        {
            SharpMap.Layers.GdalRasterLayer layer = new GdalRasterLayer(
                fi.Name, fi.FullName);
            group.Layers.Add(layer);
        }
        catch (Exception ex)
        {
        }
    }

    m_Map.Layers.Add(group);
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.

Chris

Update

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.
Coordinator
Jan 5, 2014 at 1:03 PM
Edited Jan 5, 2014 at 1: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 2: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.
Coordinator
Jan 5, 2014 at 5: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 5:47 PM
Hi

Yeh you put me on the right path, I found the answer here http://stackoverflow.com/questions/14353502/exception-while-using-gdl-in-c-sharp made sure all of these files were there and hey presto it works.

FObermaier thanks for the help.

Chris
Marked as answer by cmwb2000 on 1/5/2014 at 10:47 AM
Coordinator
Jan 6, 2014 at 3:01 PM
For my information, could you tell me where the native libs were and where you put them?
Thanks
Jan 6, 2014 at 7: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.

Chris
Coordinator
Jan 6, 2014 at 7: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 7: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.
Coordinator
Jan 6, 2014 at 9:26 PM
Edited Jan 6, 2014 at 9: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;

    //...