SharpMap.Layers.GdalRasterLayer threw an exception

Topics: General Topics
Mar 10 at 1:00 PM
Hi,
I am trying to load a raster layer from post gis database..I used this code
 var rlay = new SharpMap.Layers.GdalRasterLayer("demelevation", "PG\":dbname=Bahram host=localhost user=postgres password=123456 port=5432 mode=2 schema=public table=demelevation\"");
            SharpMap.Map myMap = new SharpMap.Map(new Size(400, 300));
            myMap.Size = new System.Drawing.Size(300, 200); //Set output size

           myMap.MinimumZoom = 100; //Minimum zoom allowed
           // /myMap.BackgroundColor = Color.White; //Set background
           // myMap.Center = new SharpMap.Geometry.Point(725000, 6180000); //Set center of map
          //  myMap.Layers.Add(rlay);
            myMap.ZoomToExtents();
            _map.Map = myMap;
I get this error
An unhandled exception of type 'System.TypeInitializationException' occurred in WindowsFormsApplication1.exe
Additional information: The type initializer for 'SharpMap.Layers.GdalRasterLayer' threw an exception.

the error is in line
var rlay = new SharpMap.Layers.GdalRasterLayer("demelevation", "PG\":dbname=Bahram host=localhost user=postgres password=123456 port=5432 mode=2 schema=public table=demelevation\"");
I can not figure out were is the problem..Could some one please help me?
I have added GDAL.Native as reference too.
Thank you
Mar 10 at 8:35 PM
I have also installed FWtools and tested the examples winformsample app in sharpmap source and I get the same error when I want to add a geotif image file too
Coordinator
Mar 11 at 6:39 AM
FWTools is obsolete, do yourself a favor and revert those changes.
The current NuGet packages rely on GDAL 1.9.2 which does not work with postgis raster.
The version in Branches/1.0 (GITHUB!) relies on GDAL 1.11, this can handle postgis raster.

Nonetheless, it seems to me that your setup is not correct. In the build output folder of your project you must have a gdal sub folder in which you have some x86 and x64 folder along with some others. If that is not the case there is sth wrong.

You need to add gdal.native to the final executable you are building, and it must match the version of the gdal package the SharpMap.Extensions relies upon..
Mar 11 at 8:08 AM
FObermaier wrote:
FWTools is obsolete, do yourself a favor and revert those changes.
The current NuGet packages rely on GDAL 1.9.2 which does not work with postgis raster.
The version in Branches/1.0 (GITHUB!) relies on GDAL 1.11, this can handle postgis raster.

Nonetheless, it seems to me that your setup is not correct. In the build output folder of your project you must have a gdal sub folder in which you have some x86 and x64 folder along with some others. If that is not the case there is sth wrong.

You need to add gdal.native to the final executable you are building, and it must match the version of the gdal package the SharpMap.Extensions relies upon..
Dear FObermaier,
In WinFormSample I had the same problem and I uncommented some parts of codes in FWtoolhealper.cs
// Copyright 2009 John Diss www.newgrove.com
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 


using System;
using System.Configuration;
using System.IO;

namespace SharpMap.Extensions.Data
{
    [Obsolete("Call GdalConfiguration.ConfigureGdal() or GdalConfiguration.ConfigureOgr()")]
    public static class FwToolsHelper
    {
        static FwToolsHelper()
        {
            
            string fwtoolsPath = ConfigurationManager.AppSettings["FWToolsBinPath"];

            if (String.IsNullOrEmpty(fwtoolsPath) || !Directory.Exists(fwtoolsPath))
                throw new FwToolsPathException(fwtoolsPath);


            string path = Environment.GetEnvironmentVariable("PATH");

            string[] paths = path.Split(new[] {';', ','});

            bool pathFound = false;
            foreach (string pth in paths)
            {
                if (String.Compare(pth, fwtoolsPath, StringComparison.CurrentCultureIgnoreCase) == 0)
                {
                    pathFound = true;
                    break;
                }
            }
            if (!pathFound)
                //Environment.SetEnvironmentVariable("PATH", path + (!path.EndsWith(";") ? ";" : "") + fwtoolsPath);
                Environment.SetEnvironmentVariable("PATH", fwtoolsPath + ";" + path);

            SetFWToolsEnvironmentVariable("FWToolsProjLib", "PROJ_LIB");
            SetFWToolsEnvironmentVariable("FWToolsGeoTiffCsv", "GEOTIFF_CSV");
            SetFWToolsEnvironmentVariable("FWToolsGdalData", "GDAL_DATA");
            SetFWToolsEnvironmentVariable("FWToolsGdalDriver", "GDAL_DRIVER");
             

        }

        
        private static void SetFWToolsEnvironmentVariable(String setting, String envVariable)
        {
            string set = ConfigurationManager.AppSettings[setting];
            if (String.IsNullOrEmpty(set))
                System.Diagnostics.Debug.WriteLine(string.Format(
                                                       "\nValue for environment variable '{0}' not set!\nPlease add\n\t<add key=\"{1}\" value=\"...\"/>\n to your app.config file",
                                                       envVariable, setting));

            Environment.SetEnvironmentVariable(envVariable, set);
        }
         

        public static string FwToolsVersion
        {
            get { return OSGeo.GDAL.Gdal.VersionInfo(""); }
        }

        [Obsolete("Call GdalConfiguration.ConfigureGdal() or GdalConfiguration.ConfigureOgr()")]
        public static void Configure()
        {
            GdalConfiguration.ConfigureGdal();
            GdalConfiguration.ConfigureOgr();
            //does nothing but ensure that the Static initializer has been called.
        }

        #region Nested type: OsGeo4WPathException

        public class FwToolsPathException : Exception
        {
            public FwToolsPathException(string path)
                : base(
                    string.Format("'{0}' is an Invalid Path to FWTools{1}. Create an application setting in [app|web].config key='FWToolsBinPath' pointing to the bin directory of FWTools{1} (absolute file path) . FWTools is downloaded from http://home.gdal.org/fwtools/",
                                  path, FwToolsVersion))
            {
            }
        }

        #endregion
    }
}
Then the error fixed and the sample worked..The only problem is how to load raster from databse.Could you give me some hints?
Thank you
Coordinator
Mar 14 at 8:26 AM
In order to work with PostGis Raster you currently have to compile SharpMap.Extensions yourself.
Mar 14 at 1:53 PM
FObermaier wrote:
In order to work with PostGis Raster you currently have to compile SharpMap.Extensions yourself.
Hi,
Yes I have done it and works great,thank you