ArcGIS Enigne使用Geoprocessor

 如果在使用相关工具传入参数是对象或相对数据库内路径时,需要给“workspace”环境变量设置值,可以是IWorkspace或数据库路径。

  如果需要为此环境变量设置值(后面也会说如何避免设置此环境变量的值),最好是数据库路径,而不要直接设置IWorkspace对象。经过实际使用经验,感觉如果将IWorkspace对象传入到Geoprocessor环境后,后继的处理偶尔会出现一些无法解释的异常。代码如下:


Geoprocessor GP = new Geoprocessor(); 


string sWorkPath;  
IWorkspace pWork;  
if(pWork.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)

{  
    sWorkPath = System.IO.Path.Combine(Application.StartupPath,"gpenvwk.sde");  
    pWork.WorkspaceFactory.Create(Application.StartupPath,"gpenvwk.sde",pWork.ConnectionProperties,0);  
}

else

{  
    sWorkPath = pWork.PathName;  
}  
GP.SetEnvironmentValue("workspace",sWorkPath);  
GP.OverwriteOutput = true;   


ESRI.ArcGIS.AnalysisTools.Intersect pIntersect = new ESRI.ArcGIS.AnalysisTools.Intersect();  
pIntersect.cluster_tolerance = 0.01;  
pIntersect.in_features = "TestPline;TestPgon";  
pIntersect.out_feature_class = "Test_intRslt";  
pIntersect.join_attributes = "ONLY_FID";  
pIntersect.output_type = "INPUT";   


GP.Execute(pIntersect,null); 

 


 上面提到,如果不设置“workspace”环境变量时,那么输入输出参数就不能是表名或要素类名,而必须包含数据库路径名(要素类在要素集中时。提示:对于输入参数,路径串中的要素集名是可选的)。同样功能代码如下:


Geoprocessor GP = new Geoprocessor();   


string sWorkPath;  
IWorkspace pWork;  
if(pWork.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)

{  
    sWorkPath = System.IO.Path.Combine(Application.StartupPath,"gpenvwk.sde");  
    pWork.WorkspaceFactory.Create(Application.StartupPath,"gpenvwk.sde",pWork.ConnectionProperties,0);  
}

else

{  
    sWorkPath = pWork.PathName;  
}   


GP.OverwriteOutput = true;   


ESRI.ArcGIS.AnalysisTools.Intersect pIntersect = new ESRI.ArcGIS.AnalysisTools.Intersect();  
pIntersect.cluster_tolerance = 0.01;  
pIntersect.in_features = sWorkPath+"\\TestPline;"+sWorkPath+"\\TestPgon";  
pIntersect.out_feature_class = sWorkPath+"\\Result\\Test_intRslt";  
pIntersect.join_attributes = "ONLY_FID";  
pIntersect.output_type = "INPUT";  
GP.Execute(pIntersect,null);  



两种方式各有优缺点:

用环境变量设置Workspace路径时,只能设置一个。如果需要对多个数据库进行处理时,其它数据库只能通过参数传入;

用参数传入Workspace路径时,对一些特殊符需要处理:碰到路径中包含有空格、分号时,需要把路径用双引号(或单引号)包含起来,如:pIntersect.in_features = "\""+sWorkPath+"\\TestPline\";\""+sWorkPath+"\\TestPgon\"";

 但如果路径中含有单引号时,不管是否用双引号包含,都会出错。(这个错误我感觉是ArcGIS的Bug)

posted @ 2010-10-14 11:15  St.Dlng  阅读(3036)  评论(0编辑  收藏  举报