您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > GDAL-CSharp环境配置
一、GDALC#DLL下载://=release-1400-gdal-1-10-1-mapserver-6-4-1.zip位于压缩包中的位置:bin\gdal\csharp\...目录下:开发时把以_csharp.dll结尾的添加到项目引用,其余的拷贝到bebug目录下。二、在调用Gdal.AllRegister()方法时报异常:“OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发异常。原因分析:gdal初始化时,其依赖dll项不全导致异常,可采用DependencyWalker工具查看相关依赖项。把九个DLL拷贝到debug是不能解决问题的。解决方法:采用SharpMap的GDAL初始化方法,需要两个数据:1.GdalConfiguration.cs2.gdal_data_config.rar第一步:将GdalConfiguration.cs添加到项目中,然后解压gdal_data_config.rar到debug目录下,文件夹名称为gdal。第二步:在使用Gdal.AllRegister()初始化前,调用以下两句代码进行相关初始化数据的配置即可。SharpMap.GdalConfiguration.ConfigureGdal();SharpMap.GdalConfiguration.ConfigureOgr();附件1:GdalConfiguration.cs/********************************************************************************Name:GdalConfiguration.cs.pp*Project:GDALCSharpInterface*Purpose:AstaticconfigurationutilityclasstoenableGDAL/OGR.*Author:FelixObermaier******************************************************************************/usingSystem;usingSystem.IO;usingSystem.Reflection;usingGdal=OSGeo.GDAL.Gdal;usingOgr=OSGeo.OGR.Ogr;namespaceSharpMap{publicstaticpartialclassGdalConfiguration{privatestaticbool_configuredOgr;privatestaticbool_configuredGdal;///summary///Functiontodeterminewhichplatformwe'reon////summaryprivatestaticstringGetPlatform(){returnIntPtr.Size==4?x86:x64;}///summary///ConstructionofGdal/Ogr////summarystaticGdalConfiguration(){varexecutingAssemblyFile=newUri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;varexecutingDirectory=Path.GetDirectoryName(executingAssemblyFile);if(string.IsNullOrEmpty(executingDirectory))thrownewInvalidOperationException(cannotgetexecutingdirectory);vargdalPath=Path.Combine(executingDirectory,gdal);varnativePath=Path.Combine(gdalPath,GetPlatform());//Prependnativepathtoenvironmentpath,toensurethe//rightlibsarebeingused.varpath=Environment.GetEnvironmentVariable(PATH);path=nativePath+;+Path.Combine(nativePath,plugins)+;+path;Environment.SetEnvironmentVariable(PATH,path);//SettheadditionalGDALenvironmentvariables.vargdalData=Path.Combine(gdalPath,data);Environment.SetEnvironmentVariable(GDAL_DATA,gdalData);Gdal.SetConfigOption(GDAL_DATA,gdalData);vardriverPath=Path.Combine(nativePath,plugins);Environment.SetEnvironmentVariable(GDAL_DRIVER_PATH,driverPath);Gdal.SetConfigOption(GDAL_DRIVER_PATH,driverPath);Environment.SetEnvironmentVariable(GEOTIFF_CSV,gdalData);Gdal.SetConfigOption(GEOTIFF_CSV,gdalData);varprojSharePath=Path.Combine(gdalPath,share);Environment.SetEnvironmentVariable(PROJ_LIB,projSharePath);Gdal.SetConfigOption(PROJ_LIB,projSharePath);}///summary///Methodtoensurethestaticconstructorisbeingcalled.////summary///remarksBesuretocallthisfunctionbeforeusingGdal/Ogr/Osr/remarkspublicstaticvoidConfigureOgr(){if(_configuredOgr)return;//RegisterdriversOgr.RegisterAll();_configuredOgr=true;PrintDriversOgr();}///summary///Methodtoensurethestaticconstructorisbeingcalled.////summary///remarksBesuretocallthisfunctionbeforeusingGdal/Ogr/Osr/remarkspublicstaticvoidConfigureGdal(){if(_configuredGdal)return;//RegisterdriversGdal.AllRegister();_configuredGdal=true;PrintDriversGdal();}privatestaticvoidPrintDriversOgr(){#ifDEBUGvarnum=Ogr.GetDriverCount();for(vari=0;inum;i++){vardriver=Ogr.GetDriver(i);Console.WriteLine(string.Format(OGR{0}:{1},i,driver.name));}#endif}privatestaticvoidPrintDriversGdal(){#ifDEBUGvarnum=Gdal.GetDriverCount();for(vari=0;inum;i++){vardriver=Gdal.GetDriver(i);Console.WriteLine(string.Format(GDAL{0}:{1}-{2},i,driver.ShortName,driver.LongName));}#endif}}}
本文标题:GDAL-CSharp环境配置
链接地址:https://www.777doc.com/doc-6886574 .html