다음은 Grid의 VAT(Value Attribute Table)를 C# 코드에서 활용하는 예제입니다.
- VAT에 필드 추가하기
- VAT에 셀값의 면적 계산하기
▣ Sample Integer GRID
▣ ArcObjects Interface
- IGridTableOp
▣ Code Snippet
using System; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.GeoAnalyst;■ VAT에 필드 추가하기
public static bool AddVatField(IRaster raster, string fieldName, esriFieldType fieldType, int fieldLength) { IRasterBandCollection bandCol = (IRasterBandCollection)raster; IRasterBand band = bandCol.Item(0); bool hasTable = false; band.HasTable(out hasTable); if (hasTable) { ITable attTable = band.AttributeTable; if (attTable.FindField(fieldName) != -1) { return true; } IField newField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)newField; fieldEdit.Name_2 = fieldName; fieldEdit.Type_2 = fieldType; fieldEdit.Editable_2 = true; fieldEdit.IsNullable_2 = true; fieldEdit.Length_2 = fieldLength; IGridTableOp gridTableOp = new GridTableOpClass(); gridTableOp.AddField(band.RasterDataset, newField); System.Runtime.InteropServices.Marshal.ReleaseComObject(gridTableOp); return true; } return false; }■ VAT에 면적 필드를 추가하고 계산하기
public static void CalculateAreaField(IRaster raster, string areaField) { IRasterBandCollection bandCol = (IRasterBandCollection)raster; IRasterBand band = bandCol.Item(0); bool hasTable = false; band.HasTable(out hasTable); if (!hasTable) return; // Add Field if (AddVatField(raster, areaField, esriFieldType.esriFieldTypeDouble, 38)) { // calculate cell size IRasterProps rstProps = (IRasterProps)raster; IPnt pnt = rstProps.MeanCellSize(); double cellSize = (pnt.X + pnt.Y) / 2.0; // get fields index ITable attTable = band.AttributeTable; int idxArea = attTable.FindField(areaField); int idxCount = attTable.FindField("COUNT"); // using update cursor IGridTableOp gridTableOp = new GridTableOpClass(); ICursor updateCursor = gridTableOp.Update(band.RasterDataset, null, false); IRow updateRow = updateCursor.NextRow(); while (updateRow != null) { int cellCount = Convert.ToInt32(updateRow.get_Value(idxCount)); double cellArea = cellCount * (cellSize * cellSize); updateRow.set_Value(idxArea, cellArea); updateCursor.UpdateRow(updateRow); updateRow = updateCursor.NextRow(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(gridTableOp); System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor); } }
댓글 없음:
댓글 쓰기