方法一 (注意:主要sde要素类进行gp操作时,需设置gp操作环境,即gp.SetEnvironmentValue("workspace", sWorkPath);所以首先创建并获取本地临时sde工作空间,
其中Temp.sde可随意命名,但是只能创建一次Temp.sde,所以建议根据需要在合适位置创建)
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", "localhost");
propertySet.SetProperty("INSTANCE", "5151");
propertySet.SetProperty("DATABASE", "GIS");
propertySet.SetProperty("USER", "lup_qx");
propertySet.SetProperty("PASSWORD", "lup_qx");
propertySet.SetProperty("VERSION", "SDE.DEFAULT");
string sWorkPath=string.Empty;
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspaceName pWorkspaceName = workspaceFactory.Create(Application.StartupPath, "Temp.sde", propertySet, 0);
sWorkPath=pWorkspaceName.PathName; //以上代码只需创建一次即可
Geoprocessor gp = new Geoprocessor();
gp.SetEnvironmentValue("workspace", sWorkPath);
gp.OverwriteOutput = true;
#region 交集操作设置
ESRI.ArcGIS.AnalysisTools.Intersect intersert = new ESRI.ArcGIS.AnalysisTools.Intersect();
intersert.in_features = this.txtshp1.Text.ToString() + ";" + sWorkPath + "\\JQDLTB"; //其中JQDLTB为sde中的要素类名称
intersert.out_feature_class = this.txtoutput.Text.ToString();
intersert.output_type = "INPUT";
intersert.join_attributes = "ALL";
if (gp.Execute(intersert, null) == null) return;
#endregion
以下方法2:未整理
如果在使用相关工具传入参数是对象或相对数据库内路径时,需要给“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)