/// <summary>
/// 获取栅格图层的属性表
/// </summary>
/// <param name="layer">栅格图层</param>
/// <returns></returns>
public static ITable GetRasterITableByLayer(ILayer layer)
{
ITable iTable = BuildRasterTable(layer);
return iTable;
}
/// <summary>
/// 获取栅格图层的属性表
/// </summary>
/// <param name="layer">栅格图层</param>
/// <returns></returns>
public static DataTable GetRasterDataTableByLayer(ILayer layer)
{
ITable iTable = BuildRasterTable(layer);
if (iTable == null)
{
return null;
}
DataTable dataTable = new DataTable();
IFields fields = iTable.Fields;
for (int i = 0; i < fields.FieldCount; i++)
{
dataTable.Columns.Add(fields.Field[i].Name);
}
ICursor pCursor = iTable.Search(null, false);
IRow pRrow = pCursor.NextRow();
while (pRrow != null)
{
DataRow pRow = dataTable.NewRow();
for (int i = 0; i < pRrow.Fields.FieldCount; i++)
{
pRow[i] = pRrow.Value[i].ToString();
}
dataTable.Rows.Add(pRow);
pRrow = pCursor.NextRow();
}
return dataTable;
}
/// <summary>
/// 构建栅格图层属性表,有时候生成的栅格图层没有属性表,需要自己构建属性表。
/// 其中,不能为 32 位浮点像素类型的栅格数据集或多波段构建栅格属性表
/// </summary>
/// <param name="pLayer">图层</param>
/// <returns>属性表ITable</returns>
public static ITable BuildRasterTable(ILayer pLayer)
{
if (!(pLayer is IRasterLayer)) return null;
IRasterLayer rasterLayer = pLayer as IRasterLayer;
IRaster pRaster = rasterLayer.Raster;
IRasterProps rProp = pRaster as IRasterProps;
if (rProp == null)
{
return null;
}
if (rProp.PixelType == rstPixelType.PT_FLOAT || rProp.PixelType == rstPixelType.PT_DOUBLE) //判断栅格像元值是否是整型
{
return null;
}
IRasterDataset rasterDataset = GetRasterDataset(rasterLayer);
IRasterBandCollection pRasterbandCollection = pRaster as IRasterBandCollection;
if (rasterDataset != null)
{
IRasterDataset2 rd2 = rasterDataset as IRasterDataset2;
//重新构建全删格,读取所有波段数
IRaster raster = rd2.CreateFullRaster();
pRasterbandCollection = (IRasterBandCollection)raster;
//无法为多波段或浮点型栅格数据集构建 VAT
if (pRasterbandCollection.Count > 1)
{
return null;
}
}
IRasterBand rasterBand = pRasterbandCollection.Item(0);
ITable rTable = rasterBand.AttributeTable;
if (rTable != null) return rasterBand.AttributeTable; //直接获取属性表
try
{
IRasterDatasetEdit2 rasterDatasetEdit = (IRasterDatasetEdit2)rasterDataset;
if (rasterDatasetEdit == null)
{
return null;
}
//Build default raster attribute table with VALUE and COUNT
rasterDatasetEdit.BuildAttributeTable(); //建立属性表
//更新属性表
pRasterbandCollection = (IRasterBandCollection)rasterDataset;
rasterBand = pRasterbandCollection.Item(0);
}
catch (Exception ex)
{
PSKJ.Common.Core.Log.LogHelper.getInstance().GetLogger(typeof(RasterLayerWrapper)).Error(ex);
return null;
}
return rasterBand.AttributeTable; //重新获取属性表
}
/// <summary>
/// 判断栅格图层是否拥有属性表
/// </summary>
/// <param name="pRaster">栅格</param>
/// <returns>是否拥有属性表</returns>
public static bool IsRasterLayerHaveTable(IRaster pRaster)
{
IRasterProps pProp = pRaster as IRasterProps;
if (pProp == null)
{
return false;
}
if (pProp.PixelType == rstPixelType.PT_FLOAT || pProp.PixelType == rstPixelType.PT_DOUBLE) //判断栅格像元值是否是整型
{
return false;
}
IRasterBandCollection pRasterbandCollection = (IRasterBandCollection)pRaster;
IRasterBand rasterBand = pRasterbandCollection.Item(0);
ITable rTable = rasterBand.AttributeTable;
return rTable != null;
}
/// <summary>
/// 获取栅格波段,默认第0波段
/// </summary>
/// <param name="rasterlayer"></param>
/// <param name="bandIndex"></param>
/// <returns></returns>
public static IRasterBand GetBand(IRasterLayer rasterlayer, int bandIndex = 0)
{
IRaster pRaster = rasterlayer.Raster;
IRasterBandCollection pRasterbandCollection = (IRasterBandCollection)pRaster;
int count = pRasterbandCollection.Count;
IRasterBand rasterBand = null;
if (bandIndex + 1 <= count)
{
rasterBand = pRasterbandCollection.Item(bandIndex);
}
else
{
rasterBand = pRasterbandCollection.Item(0);
}
return rasterBand;
}
public static List<string> GetBandNameList(IRasterLayer rasterlayer)
{
IRasterBandCollection pRasterbandCollection = GetRasterBandCollection(rasterlayer);
int count = pRasterbandCollection.Count;
List<string> itemband = new List<string>();
for (int i = 0; i < count; i++)
{
itemband.Add(pRasterbandCollection.Item(i).Bandname);
}
return itemband;
}
public static int GetBandCount(IRasterLayer rasterlayer)
{
IRasterBandCollection rbc = GetRasterBandCollection(rasterlayer);
return rbc.Count;
}
public static IRasterDataset GetRasterDataset(IRasterLayer rasterlayer)
{
IRasterDataset rasterDataset = null;
string strPath = rasterlayer.FilePath;
string strDirName = System.IO.Path.GetDirectoryName(strPath);
string strRasterName = System.IO.Path.GetFileName(strPath);
IWorkspace ws = GetWorkspace(rasterlayer);
if (ws == null) return rasterDataset;
IDataset ds = rasterlayer as IDataset;
string workspaceFactoryType = WorkspaceWrapper.GetWorkspaceFactoryType(ws as IWorkspace);
strRasterName = ds.BrowseName.Contains("\\") ? ds.BrowseName.Split('\\')[0] : strRasterName;
IRasterWorkspaceEx rasterWorkspaceEx = ws as IRasterWorkspaceEx;
if (rasterWorkspaceEx != null)
{
rasterDataset = rasterWorkspaceEx.OpenRasterDataset(strRasterName);
}
else
{
IRasterWorkspace rasterWorkspace = ws as IRasterWorkspace;
if (rasterWorkspace != null)
{
rasterDataset = rasterWorkspace.OpenRasterDataset(strRasterName);
}
}
return rasterDataset;
}
public static IRasterBandCollection GetRasterBandCollection(IRasterLayer rasterlayer)
{
IRasterBandCollection rbc = rasterlayer.Raster as IRasterBandCollection;
IRasterDataset rasterDataset=GetRasterDataset(rasterlayer);
IRasterDataset2 rd2 = rasterDataset as IRasterDataset2;
//重新构建全删格,读取所有波段数
IRaster raster = rd2.CreateFullRaster();
rbc = (IRasterBandCollection)raster;
return rbc;
}
public static IWorkspace GetWorkspace(ILayer layer)
{
IDataset ds = layer as IDataset;
if (ds == null) return null;
IWorkspace ws = ds.Workspace;
return ws;
}
/// <summary>
/// 获取栅格行列对应的Value值
/// </summary>
/// <param name="rasterLayer"></param>
/// <param name="meanx">像元X值大小</param>
/// <param name="meany">像元X值大小</param>
/// <param name="XMin">栅格图层左上角范围X</param>
/// <param name="YMax">栅格图层左上角范围Y</param>
/// <returns></returns>
public static Dictionary<int[], double> GetRasterRowColumnValueArray(IRasterLayer rasterLayer, out double meanx, out double meany,
out double XMin, out double YMax)
{
IRaster raster = rasterLayer.Raster;
IRaster2 pRaster2 = raster as IRaster2;
IRasterProps pRasterProps = raster as IRasterProps;
IPnt meanPnt = pRasterProps.MeanCellSize();
meanx = meanPnt.X;
meany = meanPnt.Y;
//获取图层的行列值
int Height = pRasterProps.Height;
int Width = pRasterProps.Width;
XMin = pRasterProps.Extent.XMin;
YMax = pRasterProps.Extent.YMax;
//定义并初始化字典,用于存储栅格内所有像员像素值
Dictionary<int[], double> PixelValueDic = new Dictionary<int[], double>();
System.Array pixels;
//定义RasterCursor初始化,参数设为null,内部自动设置PixelBlock大小
IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(null);
//用于存储PixelBlock的长宽
long blockwidth = 0;
long blockheight = 0;
IPixelBlock3 pPixelBlock3;
try
{
do
{
//获取Cursor的左上角坐标
int left = (int)pRasterCursor.TopLeft.X;
int top = (int)pRasterCursor.TopLeft.Y;
pPixelBlock3 = pRasterCursor.PixelBlock as IPixelBlock3;
blockheight = pPixelBlock3.Height;
blockwidth = pPixelBlock3.Width;
pixels = (System.Array)pPixelBlock3.get_PixelData(0);
//获取该Cursor的PixelBlock中像素的值
for (int i = 0; i < blockheight; i++)
{
for (int j = 0; j < blockwidth; j++)
{
//一定要注意,pixels中的数组排序为[Width,Height]
PixelValueDic.Add(new int[] { left + j, top + i }, Convert.ToDouble(pixels.GetValue(j, i)));
}
}
}
while (pRasterCursor.Next() == true);
return PixelValueDic;
}
catch (Exception ex)
{
throw ex;
}
}