C#+arcengine获得栅格数据的属性表
此文为或的栅格数据的属性表的功能,在此,我的属性表中有count和value字段,其中value是栅格数据的高程值,count是value在影像中出现的次数,此程序的功能为得到count最大的数,以此来获得对应的value即此栅格影像的高程值的众数,如下:
/// <summary>
/// 获得属性表
/// </summary>
/// <param name=”maskTifPath”>raster的路径</param>
private int GetAttributeTable(string maskPath)
{
int elevation=0;//高程
//得到raster(见另外的博文获得栅格数据)
IRaster raster=GetRaster(maskPath);
if (raster != null)
{
try
{
//得到一段光栅带
IRasterBandCollection rasterBandCollection = (IRasterBandCollection)raster;
int bandCount = rasterBandCollection.Count;//bandCount=1
for (int j = 0; j < bandCount; j++)
{
IRasterBand rasterBand = rasterBandCollection.Item(j);
//得到此光栅带的属性表,并保存到table
ITable table = rasterBand.AttributeTable;
//创建一个查询(SQL语句)
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = “”;
//用queryFilter查询表,并把结果保存到游标指针中去
ICursor cursorCount = table.Search(queryFilter, false);
IRow rowCount = cursorCount.NextRow();
// count字段集合
List<int> countCol = new List<int>();
int MaxValue = 0;
for (int i = 0; i < table.Fields.FieldCount; i++)//field.count=4
{
//判断字段名称是否为count
if (table.Fields.get_Field(i).Name == “Count”)
{
while (rowCount != null)
{
//以下显示Count字段的值,并得到count值的集合
countCol.Add(Convert.ToInt32(rowCount.get_Value(table.FindField(“Count”))));
rowCount = cursorCount.NextRow();
}
int count = countCol.Count;
//得到字段Count的最大值
MaxValue = countCol[0];
for (int w = 1; w < count; w++)
{
if (countCol[w] > MaxValue)
{
MaxValue = countCol[w];
}
}
//得到Count最大时value的值,即为高程的众数
ICursor cursorBinvalues = table.Search(queryFilter, false);
IRow rowBinvalues = cursorBinvalues.NextRow();
while (rowBinvalues != null)
{
int value = Convert.ToInt32(rowBinvalues.get_Value(table.FindField(“Count”)));
if (value == MaxValue)
{
elevation = Convert.ToInt32(rowBinvalues.get_Value(table.FindField(“Value”)));
}
rowBinvalues = cursorBinvalues.NextRow();
}
}
else
{
//字段名称不是count,无用,往下判断
}
}
}
return elevation;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
else
{
MessageBox.Show(“提取栅格数据失败!”);
return 0;
}
}
本文参照了http://blog.csdn.net/gisoracle/article/details/4297650博文,博主很强大,在次拜谢
转载自:https://blog.csdn.net/chhdxzq/article/details/43453569