一、使用工具及對應命名空間
這些是ArcMap的工具箱及對應的引用命名空間,這些引用都是以Tools為后綴的。
二、執行GP工具的兩種方式
(1)通過對應的工具執行
public bool Corridor(string in_distance_raster1, string in_distance_raster2, string out_raster)
{
try
{
if (System.IO.File.Exists(out_raster))
{
//刪除已經存在的文件
System.IO.File.Delete(out_raster);
}
ESRI.ArcGIS.SpatialAnalystTools.Corridor corridor = new ESRI.ArcGIS.SpatialAnalystTools.Corridor();
corridor.in_distance_raster1 = in_distance_raster1;
corridor.in_distance_raster2 = in_distance_raster2;
corridor.out_raster = out_raster;
Geoprocessor gp = new Geoprocessor();
IGeoProcessorResult result = (IGeoProcessorResult)gp.Execute(corridor, null);
if (result == null || result.Status != esriJobStatus.esriJobSucceeded)
{
MessageBox.Show("廊道分析失敗");
}
else
{
//獲取分析的結果
MessageBox.Show("廊道分析成功!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
這里通過執行對應的工具“ESRI.ArcGIS.SpatialAnalystTools.Corridor”來實現GP工具操作。Geoprocessor 是一個托管程序集是。
(2)通過對應的工具名執行
//矢量轉柵格
public static bool ShpToRaster(IFeatureClass feaureClass, string fieldName, String rasterWorkspace, String rasterName, string cell_assignment,double cellSize)
{
string fullPath;
IGeoProcessor pGP;
IGeoProcessorResult pGPR;
IVariantArray pParameterArray;
try
{
fullPath = System.IO.Path.Combine(rasterWorkspace, rasterName);
if (System.IO.File.Exists(fullPath))
{
//刪除已經存在的文件
System.IO.File.Delete(fullPath);
}
pGP = new GeoProcessorClass();
pParameterArray = new VarArrayClass();
pParameterArray.Add(feaureClass);
pParameterArray.Add(fieldName);
pParameterArray.Add(rasterWorkspace + @"\" + rasterName);
pParameterArray.Add(cell_assignment);
pParameterArray.Add(null);
pParameterArray.Add(cellSize);
if (feaureClass.ShapeType == esriGeometryType.esriGeometryPoint)
{
pGPR = pGP.Execute("PointToRaster_conversion", pParameterArray, null);
}
if (feaureClass.ShapeType == esriGeometryType.esriGeometryPolyline)
{
pGPR = pGP.Execute("PolylineToRaster_conversion", pParameterArray, null);
}
if (feaureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
pGPR = pGP.Execute("PolygonToRaster_conversion", pParameterArray, null);
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
這里通過執行對應的工具名“pGPR = pGP.Execute("PointToRaster_conversion", pParameterArray, null);”來實現GP工具操作。IGeoProcessor 一個COM Interop程序集
注意事項:
1、確保填入參數有正確的設置順序
2、如果你想跳過可選參數,你只需要將它們填入一個空字符串,如""
3、如果你填入空字符串,處理工具將使用默認值
三、Gp操作有的需要環境設置
默認情況下是輸入的坐標系統作為輸出坐標,如果要設置新的,需要改變以下值。
public void setCoordinateSystem(IGeoProcessor2 gp)
{
// Set overwrite option to true.
gp.OverwriteOutput = true;
// Set workspace environment.
gp.SetEnvironmentValue("workspace", @"C:\data\saltlake.gdb");
// Set the output coordinate system environment.
gp.SetEnvironmentValue("outputCoordinateSystem", @
"C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\Nad 1983\NAD 1983 UTM Zone 12N.prj");
IVariantArray parameters = new VarArrayClass();
parameters.Add("roads");
parameters.Add("roads_copy");
gp.Execute("CopyFeatures_management", parameters, null);
}
以下是重新改變環境變量值
// Get the cell size environment value.
object env = gp.GetEnvironmentValue("cellsize");
// Reset the environment values to their defaults.
gp.ResetEnvironments();
詳見Esri官網:
四、Gp工具參數設置
這是Gp工具最麻煩的一步,也最容易出錯。在ArcMap幫助文檔可以搜到對應的工具的幫助,里面有一個介紹參數的。
一般對於FeatureLayer就是IFeatureLayer對象,RasterLayer就是IRasterLayer對象,或者是文件的路徑。Field就是IField對象或者字段名。Table就算是ITable對象。大多數參數就是string或者object類型。具體的可以實際中測試選擇。
五、參考資料或網址
1.http://edndoc.esri.com/arcobjects/9.2/NET/c4ff8b68-0410-435f-b8e5-682d5cea47cf.htm
2.http://blog.csdn.net/liuguobo/article/details/16965987
3.http://www.cnblogs.com/zhangjun1130/archive/2010/05/26/1744472.html
4.http://blog.csdn.net/mytudousi/article/details/31388607