项目需求:
将一个多边形数据(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;
}
}