項目需求:
將一個多邊形數據(Polygon)根據指定字段生成柵格數據,和。
不消說,先查閱ArcGIS的幫助文檔。
首先進入我視線的是 IConversionOp接口的ToRasterDataset方法:
Set variable = object.ToRasterDataset (dataset, rasterFormat, pWorkspace, name )
The ToRasterDataset method syntax has the following object qualifier and arguments:
Part | Description |
---|---|
object | An object expression that evaluates to an object in the Applies To list. |
variable | A reference to an object that implements IRasterDataset. |
dataset | Required. An IGeoDataset object. |
rasterFormat | Required. A string expression that represents the rasterFormat. |
pWorkspace | Required. An IWorkspace object. |
name | Required. A string expression that represents the name. |
因為參數中不能指定字段名,不符合需求,繼續尋找另外的方法。
其實,需求的功能就是ArcToolbox的Conversion下的Polygon To Raster功能,如下圖所示:
原來有現成的工具啊,這可就容易了。工具的說明如下圖所示:
PolygonToRaster_conversion(in_features, value_field, out_raster_dataset, cell_assignment, priority_field, cellsize)
Parameter | Explanation | Datatype |
---|---|---|
Input features (Required) | The polygon input feature dataset to be converted to a raster.
|
Feature Layer |
Value field (Required) | The field used to assign values to the output raster. It can be any field of the input feature dataset's attribute table.
|
Field |
Output raster name (Required) | The output raster dataset to be created. When you're not saving to a geodatabase, specify .tif for a TIFF file format, .img for an ERDAS IMAGINE file format, or no extension for a GRID file format.
|
Raster Dataset |
Cell assignment type (Optional) | The method to determine how the cell will be assigned a value when more than one feature falls within a cell.
|
String |
Priority field (Optional) | This field is used when a feature should take preference over another feature with the same attribute.
|
Field |
Output cell size (Optional) | The cell size from which the output raster dataset will be created.
|
Analysis cell size |
我們直接用IGeoProcessor的Execute方法進行掉用該工具就OK了。
代碼如下圖所示:
public static bool ToRaster(IFeatureClass feaureClass, string fieldName, String rasterWorkspace, String rasterName, int 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("MAXIMUM_AREA");
pParameterArray.Add(null);
pParameterArray.Add(cellSize);
//Convertion Tools(PolygonToRaster_conversion)
pGPR = pGP.Execute("PolygonToRaster_conversion", pParameterArray, null);
return true;
}
catch (Exception ex)
{
throw ex;
}
}