Exception while using GDAL in C#

后端 未结 8 529
囚心锁ツ
囚心锁ツ 2020-12-11 02:35

I started to use gdal_csharp dll in my application and read a geotiff file. but it says:

The type initializer for \'OSGeo.GDAL.GdalPINVOKE\'         


        
8条回答
  •  醉话见心
    2020-12-11 02:48

    I know this is a reasonably old question now, but I found this in google after researching the same problem myself, so this means that for searches on this error this is still a very relevant page to update given it's still in the top 5 from the big G when the same problem is searched.

    In my case it was the answers from "DeusExMachina25" and "Grzegorz Sławecki" that struck a chord.

    I'm writing some software that makes use of the current builds of "sharp map" on NUGet (as of 24th of June 2016) and my software kept throwing the same gdal_wrap message as the OP originally reported, even though I'm using the GDAL package provided by the Sharpmap team.

    I didn't realize that the NUGet installer for the package had installed a configuration class for me, but after reading through this thread and finding out that it does I went looking for it.

    Sure enough I found the file 'GdalConfiguration.cs' in my project and added a call to it in an appropriate place in my project, expecting GDAL to be initialized correctly.

    However, after I did this, I still had the same problem.

    So, I set a break point on the beginning of the GDAL routine that had been added, and waited until the break point was hit.

    I then traced through the method, and eventually found the following line:

    var gdalPath = Path.Combine(executingDirectory, "gdal");
    

    at around line 64 in the file.

    Tracing through this, I noticed that the path being built was:

    d:\geodata\maptest\maptest\bin\debug\gdal
    

    but the NUGet installer had installed all the dependent assemblies in

    d:\geodata\maptest\maptest\bin\debug
    

    Exactly where I expected them to be.

    I changed line 64 so that it now read:

    var gdalPath = Path.Combine(executingDirectory, "");
    

    and voila, the error went away and everything started to work.

    I could have done things the other way too, and created a folder called gdal, then copied everything into that, but that then would have gotten deleted when I did a "clean" on the project.

    Since the config class, set's up various environment variables based on this path, quickly changing that one line also fixes up the path for the GDAL data files , plugins and a few other things too.

提交回复
热议问题