现在的位置: 首页 > 综合 > 正文

gdal C#编译

2018年06月07日 ⁄ 综合 ⁄ 共 5172字 ⁄ 字号 评论关闭

http://blog.csdn.net/liubx733/article/details/4935021

(以下内容是网络搜索到的,当时忘记记录网址,所以对于出处没有表明,在此对原作者表示歉意。希望对需要的朋友有所帮助。)

GDAL编译时机器中必须装有VC。

 

2007-08-26 | How to compile GDAL/OGR C# interface

标签: GDAL  OGR  Csharp  interface 

 

 What is GDAL? GDAL is abbreviation of Geospatial Data Abstraction
Library. It is a translator library for raster geospatial data formats. Most important of all, it is a open source library. As a raster translator library, it can support most of raster formats.

   This article tries to describe the primary steps of creating Csharp interface from the gdal source. Before compilation you must have tow necessary packages. One is gdal source
code package (you can find it here). The other is swigwin package(you
can download for free here
),which will be used in Csharp interface compilation.

   step1 Edit nmake.opt file

     Open G:/gdal/nmake.opt with notepad, and modify these lines:

    1  GDAL_HOME = "../gdal"      

   The actual location of gdal package. 

    2  SWIG= ../swigwin-1.3.31/swig.exe     

   The actual location of swigwin package.

    3  MSVC_VER=1400

     Assume compiler is Microsoft visual studio2005.

  step2  setting up compiling environment

     Run VS2005 command prompt, then execute  (assume VS.net is installed in E:/Microsoft visual studio 8)

     E:/Microsoft Visual studio 8/VC/bin/vcvars32.bat.

    Once the environment is setup, you can cd to the GDAL root directory and do the following:

  step3 compile Csharp interface

   execute follow commands: (assume gdal is installed in the G:/GDAL)

  G:/GDAL> nmake /f makefile.vc

  G:/GDAL> nmake /f makefile.vc install

  G:/GDAL> nmake /f makefile.vc devinstall

 Once the build has completed successfully,you can begin to build Csharp interface.For creating the interface execute the following commands.

 G:/GDAL/swig/csharp> nmake /f makefile.vc interface

 G:/GDAL/swig/csharp> nmake /f makefile.vc

Upon a successful compilation the following files are created,you will find them in ../gdal/swig/csharp/:

gdal_gdal_csharp.dll
gdal_ogr_csharp.dll
gdal_osr_csharp.dll
gdal_gdalconst_csharp.dll
gdal_gdal_wrap.dll
gdal_ogr_wrap.dll
gdal_osr_wrap.dll
gdal_gdalconst_wrap.dll
and some sample applications (*.exe)

Note: *_csharp.dll is the managed part of the interface. You should add a reference to these assemblies for using the classes of the interface. These *_csharp.dll-s will load the
corresponding *_wrap.dll which are the unmanaged part of the interface hosting the code of the gdal core(from webpage:http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpCompile).

Step4 test your compilation

For testing the successful compilation you can use:

G:/GDAL/swig/csharp> nmake /f makefile.vc test

This command will invoke some of the sample applications. For the proper execution the location of the proj.dll should be available in the PATH.

Step5 use *_csharp.dll in your program

Add these *_csharp.dll to the reference of the C# program you oriented. Enjoin it! 

 

 另一份:

Gdal是栅格地理空间数据格式的转换开源类库,它使用一种单一的抽象数据模型,满足了其所支持的所有数据格式的应用需求。目前Gdal的最新版本为 
GDAL/OGR 1.6.0 
与Gdal类似地,与Gdal库一起的OGR类库,提供了对矢量数据的相关操作。 
详细资料请参考GDAL 
流浪者N应用Gdal是为了用C#实现对HDF文件的操作。 
下面为流浪者N编译支持HDF4、HDF5的Gdal库的操作,首先声明流浪者N这么编译出的Gdal仍然不支持HDF,这里写下一为记录;二为交流, 
寻求帮助。 
流浪者N尝试编译的Gdal版本:gdal-1.5.2、gdal-1.6.0 ; 
另外还需swig,这里用的版本:swigwin-1.3.38 ;HDF4:42r4-win-vnet;HDF5:5-182-win- 
vs2005 
为了支持HDF5,还需下载SZip,这里用szip21-vs2005 ; 
OK,现在万事具备,只欠编译了,尽管编译出来不能支持HDF,但对于TIFF之类还是支持的,仍然期待… 
编译准备…… 
1.Gdal压缩包、swigwin压缩包、HDF压缩包、szip压缩包解压; 
2.修改。

修改Gdal中的bug,Source Files/leveller/levellerdataset.cpp文件171行 { “?, 
kPI / 180.0, UNITLABEL_DEGREE },将“ “? ”修改为“ ”? “ ”,保存; 
修改Gdal根目录下的nmake.opt文件:MSVC_VER=1400(即选择VS2005);GDAL_HOME=“YourDir”, 
YourDir为你自己建立的用于存放编译生成的文件的地方,自行定义;SWIG = swig.exe,将swigwin解压后其中swig.exe 
的路径填于此处; 
3.再次修改nmake.opt,安装 Hdf4、Hdf5的驱动。

找到“Uncomment the following and update to enable NCSA HDF Release 4 
support”位置,将该项下方的三个#去除。修改如下:HDF4_PLUGIN = YES    HDF4_DIR = E:/JoSn 
/sIon/Gdal0311_10/42r4-win-vnet     HDF4_LIB = $(HDF4_DIR)/lib/ 
hd424.lib 
同理修改HDF5的相关配置。修改如下:HDF5_PLUGIN = YES     HDF5_DIR = E:/JoSn/sIon 
/Gdal0311_10/5-182-win-vs2005     SZIP_DIR = E:/JoSn/sIon 
/Gdal0311_10/szip21-vs2005-enc       HDF5_LIB = $(HDF5_DIR) 
/hdf5dll.lib / 
  $(SZIP_DIR)/dll/szlibdll.lib 
修改部分结束 
修改完毕,编译真正开始… 
1.打开VS 2005 命令行,定位到Gdal所在的文件夹; 
2.nmake /f makefile.vc; 
3.nmake /f makefile.vc install; 
4.nmake /f makefile.vc devinstall,这步完成后GDAL_HOME处生成一系列的文件,其bin目录下会有 
gdal15.dll or gdal16.dll,视gdal版本而定。 
5. cd swig/csharp,定位到gdal目录下的csharp文件夹。 
6.nmake /f makefile.vc,这步完成后会在csharp目录下生成8个dll文件。将这8个文件拷贝到gdal15.dll 
or gdal16.dll 所在位置(见第4步)。 
测试程序…… 
1.添加*_csharp.dll 四个文件的引用。 
2.OSGeo.GDAL.Gdal.AllRegister(); 
3.OSGeo.GDAL.Dataset ds=OSGeo.GDAL.Gdal.Open(@”d: 
/test.tiff”,OSGeo.GDAL.Access.GA_ReadOnly); 
4.int w = ds.RasterXSize; int h = ds.RasterYSize; MessageBox.Show 
(”image width=” + w + “,height=” + h); 
5.tiff文件的测试没有问题,但改为HDF4或HDF5文件就无能为力了。提示为:暂不支持该文件。 
寻求交流与帮助 
1.望对Gdal有较深研究者传授流浪者N编译支持HDF的方法 
2.流浪者N后来用FWTools,据说FWTools编译的Gdal支持了HDF4、HDF5,但不知是流浪者N愚钝,不会用 
FWTools2.2.8,还是FWTools2.2.8存在bug,总之有异常,说是“OSGeo.GDAL.GdalPINVOKE的类型初始值设 
定项引发异常”,这倒与之前流浪者N错误编译Gdal库时的异常相同,不知是和缘故,望广大GIS同行们指点。 
3.大家帮帮忙,期待ing

  

 

 使用时的问题

 

#7 2009-09-11
15:52 | DreamSpark      

你好,请问OSGeo.OGR.Ogr.RegisterAll();为什么老执行不过去?报“OSGeo.OGR.Ogr”的类型初始值设定项引发异常这样的问题。不知该怎么解决? 
  回复
  引用  查看    

#8[楼主]
2009-10-10 21:41 | giswei      

@DreamSpark
这个问题是dll不全造成的,除了要引用的4dll外,还有5dll也要放到Debug目录下。
在编译C#下的gdal时,总共生成了9dll,在编译的本机上,程序是通过环境变量path找到另外的几个dll的。
在没有编译过gdal的电脑上,反正就把这9个编译后的dll放到debug下面就一切Ok
如果要编译后的这9dll,可以邮件向我要
fuweiping@gmail.com
  回复
  引用  查看    

上面一问一答来自http://www.cnblogs.com/webgis8/archive/2009/07/19/1526425.html

(将四个*_CSharp.dll在项目中“添加引用”添加进来,其余gdal16.dll和另外四个编译C#时生成的dll文件拷贝到项目的debug下)

抱歉!评论已关闭.