ArcGIS Engine中調用GP的兩種方法

第一種,分別設置參數:

//添加命名空間
 using ESRI.ArcGIS.esriSystem;
 using ESRI.ArcGIS.Geoprocessor;
 
//實現button click方法
 private void button1_Click(object sender, EventArgs e)
 {
 //構造Geoprocessor
 Geoprocessor gp = new Geoprocessor();
 //設置參數
 ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect();
 intersect.in_features = @"F:\foshan\Data\wuqutu_b.shp;F:\foshan\Data\world30.shp";
 intersect.out_feature_class = @"E:\intersect.shp";
 intersect.join_attributes = "ONLY_FID";
 //執行Intersect工具
 RunTool(gp, intersect, null);
 }
 
private void RunTool(Geoprocessor geoprocessor, IGPProcess process, ITrackCancel TC)
 {
 // Set the overwrite output option to true
 geoprocessor.OverwriteOutput = true;
 
try
 {
 geoprocessor.Execute(process, null);
 ReturnMessages(geoprocessor);
 
}
 catch (Exception err)
 {
 Console.WriteLine(err.Message);
 ReturnMessages(geoprocessor);
 }
 }
 
// Function for returning the tool messages.
 private void ReturnMessages(Geoprocessor gp)
 {
 string ms = "";
 if (gp.MessageCount > 0)
 {
 for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
 {
 ms += gp.GetMessage(Count);
 }
 }
 
另一種添加參數:

//1-定義GeoProcessor對象
 Geoprocessor gp = new Geoprocessor();
 object sev = null;
 //2-設置參數
 gp.OverwriteOutput = true;
 //3-設置工具箱所在的路徑
 gp.AddToolbox(@"F:\lib_test\AirportsAndGolf.tbx");
 //4-設置輸入參數
 IVariantArray parameters = new VarArrayClass();
 parameters.Add(@"F:\lib_test\地下水重金屬數據.xls\Sheet1$");
 parameters.Add("`YEAR` = 2009");
 parameters.Add("W20111");
 parameters.Add(@"F:\lib_test\temp.gdb\tempwww");
 
//5-執行工具
 gp.Execute("ModelAnalysis", parameters, null);

ESRI官方幫助示例:

1.

using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.AnalysisTools;

public void SampleBufferTool()
{

  // Initialize the geoprocessor. 
  Geoprocessor GP = new Geoprocessor();

  ESRI.ArcGIS.AnalysisTools.Buffer bufferTool = new
    ESRI.ArcGIS.AnalysisTools.Buffer();

  bufferTool.in_features = @"D:\St_Johns\data.mdb\roads_Buffer";
  bufferTool.out_feature_class = @"D:\St_Johns\data.mdb\roads";
  bufferTool.buffer_distance_or_field = "distance";

  GP.Execute(bufferTool, null);

}
2.

using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;

public void SampleCalculateBestPathTool()
{

  // Initialize the geoprocessor.
  Geoprocessor GP = new Geoprocessor();

  // Add the BestPath toolbox.
  GP.AddToolbox(@"C:\SanDiego\BestPath.tbx");

  // Generate the array of parameters.
  IVariantArray parameters = new VarArrayClass();
  parameters.Add(@"C:\SanDiego\source.shp");
  parameters.Add(@"C:\SanDiego\destination.shp");
  parameters.Add(@"C:\SanDiego\bestpath.shp");

  // Execute the model tool by name.
  GP.Execute("CalculateBestPath", parameters, null);






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章