[gdal-dev] problem getting ogr_csharp to work

Mark Cheyne cheynm at yahoo.com
Mon Feb 18 10:07:51 EST 2008


ahh...i'm all too familiar with 'unable to load xyz'
when using the c# wrap. you should find that file (and
other dependencies) in the FWTools /bin directory. the
easiest thing is to copy it to the directory that
contains your C# program's binaries. you might then
get a complaint about the next missing dependency, so
repeat. If using Visual Studio, you might consider
using a post-build event for this.

you might be able to avoid having to do the above if
you set your environment with a class like below that
Dave Baker on this list passed my way a few months
ago. However, I've had spotty results with that - It
may work great on machine X, but not Y. Overall, I've
had different deployment behavior on each of the 3 or
4 machines I've used OGR on. On one machine I even see
different behavior when running in the debugger (env
setup does not work) and not (it does). No pattern
yet, I just take it as an extra dose of
anti-alzheimers problem solving...

If you are writing Visual Studio 2005/2008 unit tests,
you are aware that each test run produces a new
directory. If your tests use the c# wrap, either your
environment needs to be set, or all the dependencies
need to get copied to the test directory. VS takes
care of copying direct references, but you may want to
look at specifying a deployment item via VS's
Test-Edit Test Run Configurations-Local Test Run menu.

I'll be happy to help more if it comes to it.

Mark

     /// <summary>
    /// A class used to set the FWtools environment
for the current
 process.
    /// </summary>
    public static class FWToolsEnvironment
    {
        private static bool isSet = false;

        public static void Set()
        {
            string fwDir =
System.Environment.GetEnvironmentVariable(@"FWTOOLS_OVERRIDE");
            if (fwDir == null)
                fwDir =
System.Environment.GetEnvironmentVariable(@"FWTOOLS_DIR");
            if (fwDir == null)
                using (RegistryKey rk =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\FWtools"))
                {
                    fwDir =
(string)rk.GetValue(@"Install_Dir");
                }
            Set(fwDir);
        }

        public static void Set(string fwtools_dir)
        {
            if (isSet) // we don't need to do this
again
                return;
            if (fwtools_dir == null) // not much to do
here so...
                return;

           
System.Environment.SetEnvironmentVariable(@"FWTOOLS_DIR",
fwtools_dir);

            string fwtbin = fwtools_dir + @"\bin;";
            string fwtpy = fwtools_dir + @"\python;";
            string path = "";
            path += fwtbin;
            path += fwtpy;
            path +=
System.Environment.GetEnvironmentVariable("PATH");
           
System.Environment.SetEnvironmentVariable(@"PATH",
path);
           
System.Environment.SetEnvironmentVariable(@"PYTHONPATH",
fwtools_dir + @"\pymod");
           
System.Environment.SetEnvironmentVariable(@"PROJ_LIB",
fwtools_dir + @"\proj_lib");
           
System.Environment.SetEnvironmentVariable(@"GEOTIFF_CSV",
fwtools_dir + @"\data");
           
System.Environment.SetEnvironmentVariable(@"GDAL_DATA",
fwtools_dir + @"\data");
          
 
System.Environment.SetEnvironmentVariable(@"GDAL_DRIVER_PATH",
fwtools_dir + @"\gdal_plugins");
            isSet = true;
        }
    }

--- niels hoffmann <nielslcr at gmail.com> wrote:

>  Hi,
> 
> I have a problem getting the csharp binding to work.
> I installed the latest FWTools 2.1.0 and I tried to
> explicitly add my
> FWTools \bin dir to my path using the code snippet
> in the Q&A:
> http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpUsage
> 
> I'm basically following the OGRGEOS.cs example:
> Geometry g1 = Geometry.CreateFromWkt("POLYGON((0 0,
> 10 10, 10 0, 0 0))");
> 
> But I keep getting the following exception:
> 
> Unhandled Exception:
> System.TypeInitializationException: The type
> initializer fo
> r 'OSGeo.OGR.OgrPINVOKE' threw an exception. --->
> System.TypeInitializationExcep
> tion: The type initializer for 'SWIGExceptionHelper'
> threw an exception.
> ---> Sy
> stem.DllNotFoundException: *Unable to load DLL
> 'ogr_wrap'*: The operating
> system c
> annot run . (Exception from HRESULT: 0x800700B6)
>    at
>
OSGeo.OGR.OgrPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_Og
> r(ExceptionDelegate applicationDelegate,
> ExceptionDelegate
> arithmeticDelegate, E
> xceptionDelegate divideByZeroDelegate,
> ExceptionDelegate
> indexOutOfRangeDelegate
> , ExceptionDelegate invalidOperationDelegate,
> ExceptionDelegate ioDelegate,
> Exce
> ptionDelegate nullReferenceDelegate,
> ExceptionDelegate outOfMemoryDelegate,
> Exce
> ptionDelegate overflowDelegate, ExceptionDelegate
> systemExceptionDelegate)
>    at
> OSGeo.OGR.OgrPINVOKE.SWIGExceptionHelper..cctor()
>    --- End of inner exception stack trace ---
>    at
> OSGeo.OGR.OgrPINVOKE.SWIGExceptionHelper..ctor()
>    at OSGeo.OGR.OgrPINVOKE..cctor()
>    --- End of inner exception stack trace ---
>    at OSGeo.OGR.OgrPINVOKE.new_Geometry(Int32 jarg1,
> String jarg2, Int32
> jarg3,
> IntPtr jarg4, String jarg5)
>    at OSGeo.OGR.Geometry..ctor(wkbGeometryType type,
> String wkt, Int32 wkb,
> IntP
> tr wkb_buf, String gml)
>    at OSGeo.OGR.Geometry.CreateFromWkt(String wkt)
>    at pgIntersectOGRGEOS.Program.Main(String[] args)
> in
> D:\SVN_Work\trunk\Code\p
> gIntersectOGRGEOS\pgIntersectOGRGEOS\Program.cs:line
> 23
> 
> Anybody got any ideas?
> 
> Cheers,
> Niels Hoffmann
> > _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev


Mark Cheyne
(608) 332-2235 (cell)


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 



More information about the gdal-dev mailing list