Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception while using GDAL in C#

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' threw an exception.

it's my code

string fileName = @"/path to geotiff file";

OSGeo.GDAL.Dataset DS = 
    OSGeo.GDAL.Gdal.Open(fileName, OSGeo.GDAL.Access.GA_ReadOnly);

can anyone help?

Edit:

I have these dlls

enter image description here

This is the full error message:

enter image description here

It says that cannot load gdal_wrap. But when I'm going to add that dll to my application the below message is shown:

enter image description here

like image 617
Hossein Narimani Rad Avatar asked Jan 16 '13 07:01

Hossein Narimani Rad


2 Answers

As an update to this there is now GDAL maintained by the SharpMap team as a nuget package here which is updated regularly. You'll need to install both the "GDAL.Native" and "GDAL" package for your project to use the GDAL library. Once installed via nuget, they'll automatically create a "GdalConfiguration.cs" that you call into to initialize the GDAL paths before starting. The only thing to note is the packages are setup to automatically copy their appropriate GDAL libraries to your output build directory. If you need to deploy the application you'll have to do a bit of extra effort.

like image 182
DeusExMachina25 Avatar answered Oct 01 '22 04:10

DeusExMachina25


To solve this one I downloaded the prebuilt libraries as described here and grabbed FWTools from here.

The unmanaged DLLs I used came from \install_dir\FWTools2.4.7\bin and the C# wrapper from \install_dir\FWTools2.4.7\csharp.

gdal14.dll, msvcp71.dll and msvcr71.dll came from here, which is mentioned in that first link.

The error you are receiving re gdal_wrap.dll is referring to one of its dependencies. I threw that DLL into depends and it found a lengthy list of dependent libraries. Note that this list is likely longer due to my use of the FWTools distribution - if you built your version from source it may look different, though the same principles apply.

To get the above code to work on my machine I had the following files in my output directory:

gdal14.dll
gdalconst_csharp.dll
gdalconst_wrap.dll
gdal_csharp.dll
gdal_fw.dll
gdal_wrap.dll
geos_fw.dll
geotiff_fw.dll
hdf5dll.dll
hdf_fw.dll
jpeg12_osgeo.dll
jpeg_osgeo.dll
libcurl.dll
libeay32.dll
libexpat.dll
libmysql.dll
libpq.dll
libtiff_fw.dll
lti_dsdk_dll.dll
mfhdf_fw.dll
msvcp71.dll
msvcr71.dll
NCScnet_fw.dll
NCSEcw_fw.dll
NCSUtil_fw.dll
netcdf.dll
ogdi_32b1.dll
proj.dll
sqlite3.dll
ssleay32.dll
szlibdll.dll
xerces-c_2_7.dll
zlib1.dll
zlib_osgeo.dll

Now these don't necessarily all have to live in the output directory - as long as they are on your path somewhere (e.g., \Windows\System32) you should be fine.

like image 44
nick_w Avatar answered Oct 01 '22 03:10

nick_w