Search code examples
c#gdalogr

The type initializer for 'OSGeo.OSR.OsrPINVOKE' threw an exception


I'm trying to create a CoordinateTransformation to transform the SRS of a shapefile. However, on attempting to create one with the following method call:

var sourceSrs = new SpatialReference(Osr.SRS_WKT_WGS84);

I get the exception:

The type initializer for 'OSGeo.OSR.OsrPINVOKE' threw an exception.

The detailed exception looks like:

Exception

Can anyone help?


Solution

  • Forcing the project to build for target platform of x64 helps. You can do that by going to Project Properties (Shortcut: Alt + Enter) > Build > Platform Target.

    Further, if you are running the code from some unit test, do the same for the Unit Testing project and change the "Default Process Architecture" to x64. The "Default Process Architecture" can be changed from the Test (in VS menu items) > Test Settings > Default Processor Architecture > x64.