/// <summary> /// 添加新字段 /// </summary> /// <param name="layer">图层</param> /// <param name="fieldName">字段名称</param> /// <param name="fieldType">字段类型</param> /// <param name="isNullable"></param> /// <param name="isRequired"></param> /// <param name="isEditable"></param> /// <returns></returns> public bool AddNewField(ILayer layer, string fieldName, esriFieldType fieldType, bool isNullable = true, bool isRequired = false, bool isEditable = true) { ITable iTable = AttributeTableClass.GetITableByLayer(layer); return(AddNewField(iTable, fieldName, fieldType, isNullable, isRequired, isEditable)); }
/// <summary> /// 添加表格的多列数据到图层 /// </summary> /// <param name="fieldNames"></param> private void AddTableColumns(List <string> fieldNames) { ITable pTable = AttributeTableClass.GetITableByLayer(newFieldLayer); if (pTable == null) { return; } Dictionary <int, string> indexAndNames = GetFieldIndexAndName(pTable, fieldNames); if (indexAndNames.Count == 0) { return; } IQueryFilter pFilter = GetQueryFilter(indexAndNames.Values.ToList()); ICursor pCursor = pTable.Update(pFilter, false); IRow pRow = pCursor.NextRow(); int rowCount = pTable.RowCount(null); for (int i = 0; i < rowCount; i++) { foreach (var indexAndName in indexAndNames) { object rowValue = newFieldTable.Rows[i][indexAndName.Value]; //非空 if (rowValue == DBNull.Value) { continue; } pRow.set_Value(indexAndName.Key, rowValue); pCursor.UpdateRow(pRow); } pRow = pCursor.NextRow(); } }
/// <summary> /// 添加表格的数据到图层 /// </summary> /// <param name="fieldName">字段名称</param> /// <param name="columnName">列名称</param> private void AddTableColumnByDictionary(string fieldName, string columnName) { if (!newFieldTable.Columns.Contains(columnName)) { MessageBox.Show(@"The column is not in the table!"); return; } ITable pTable = AttributeTableClass.GetITableByLayer(newFieldLayer); if (pTable == null) { return; } int addFieldIndex = pTable.Fields.FindField(fieldName); if (addFieldIndex == -1) { return; } IQueryFilter pFilter = new QueryFilterClass(); pFilter.SubFields = fieldName; //设置过滤字段,提高效率 ICursor pCursor = pTable.Update(pFilter, false); UpdateRow(pCursor, addFieldIndex, columnName); }
/// <summary> /// 获取栅格图层的属性表 /// </summary> /// <param name="layer">栅格图层</param> /// <param name="queryFilter2">过滤条件</param> /// <returns></returns> public static DataTable GetRasterTableByLayer(ILayer layer, IQueryFilter2 queryFilter2 = null) { ITable iTable = BuildRasterAttributeTable(layer); //ITable iTable = (ITable)layer; //若已存在属性表,直接用该行代码 return(AttributeTableClass.GetAttributeTableByITable(iTable)); }
/// <summary> /// 获取栅格数据集的属性表 /// </summary> /// <param name="geoDataset">输入几何数据集</param> /// <returns></returns> public static DataTable GetRasterTableByGeoDataset(IGeoDataset geoDataset) { IRaster raster = (IRaster)geoDataset; ITable iTable = GetTableByRaster(raster); return(AttributeTableClass.GetAttributeTableByITable(iTable)); }
/// <summary> /// 添加新字段 /// </summary> /// <param name="fieldName">字段名称</param> /// <param name="fieldType">字段类型</param> public void AddNewFiled(string fieldName, esriFieldType fieldType) { ITable iTable = AttributeTableClass.GetITableByLayer(newFieldLayer); //先删除 DeleteFieldClass.DeleteField(iTable, fieldName); //再添加 iTable.AddField(NewFieldClass.GetNewFieldEdit(fieldName, fieldType)); AddTableColumn(fieldName, fieldName); }
/// <summary> /// 获取字段索引 /// </summary> /// <param name="layer"></param> /// <param name="fieldName"></param> /// <returns></returns> public static int GetFieldIndex(ILayer layer, string fieldName) { ITable iTable = AttributeTableClass.GetITableByLayer(layer); if (iTable == null) { return(-1); } return(iTable.Fields.FindField(fieldName)); }
/// <summary> /// 获取不在指定类型中的字段 /// </summary> /// <param name="layer">图层</param> /// <param name="fieldTypes">指定类型的字段</param> /// <returns></returns> private static List <string> GetFieldNameNotInFieldTypes(ILayer layer, Dictionary <esriFieldType, int> fieldTypes) { ITable iTable = AttributeTableClass.GetITableByLayer(layer); if (iTable == null) { return(null); } return(GetFieldNameNotInFieldTypes(iTable.Fields, fieldTypes)); }
/// <summary> /// 添加多个新字段 /// </summary> /// <param name="fieldNames"></param> /// <param name="fieldType">字段类型</param> public void AddNewFields(List <string> fieldNames, esriFieldType fieldType) { ITable iTable = AttributeTableClass.GetITableByLayer(newFieldLayer); foreach (string fieldName in fieldNames) { //先删除 DeleteFieldClass.DeleteField(iTable, fieldName); //再添加 iTable.AddField(NewFieldClass.GetNewFieldEdit(fieldName, fieldType)); } AddTableColumns(fieldNames); }
/// <summary> /// 获取图层表格数据统计对象 /// </summary> /// <param name="layer"></param> /// <param name="fieldName"></param> /// <returns></returns> public static IDataStatistics GetDataStatistics(ILayer layer, string fieldName) { ITable iTable = AttributeTableClass.GetITableByLayer(layer); if (iTable == null || iTable.FindField(fieldName) == -1) { return(null); } IQueryFilter2 queryFilter = new QueryFilterClass(); queryFilter.SubFields = fieldName; //获得游标 ICursor pCursor = iTable.Search(queryFilter, false); IDataStatistics dataStatistics = new DataStatisticsClass(); dataStatistics.Field = fieldName; //字段 dataStatistics.Cursor = pCursor; return(dataStatistics); }
/// <summary> /// 添加表格的单列数据到图层 /// </summary> /// <param name="fieldName">字段名称</param> /// <param name="columnName">列名称</param> private void AddTableColumn(string fieldName, string columnName) { if (!newFieldTable.Columns.Contains(columnName)) { MessageBox.Show(@"The column is not in the table!"); return; } ITable pTable = AttributeTableClass.GetITableByLayer(newFieldLayer); if (pTable == null) { return; } int addFieldIndex = pTable.Fields.FindField(fieldName); if (addFieldIndex == -1) { return; } IQueryFilter pFilter = new QueryFilterClass(); pFilter.SubFields = fieldName; //设置过滤字段,提高效率 ICursor pCursor = pTable.Update(pFilter, false); IRow pRow = pCursor.NextRow(); int rowCount = pTable.RowCount(null); for (int i = 0; i < rowCount; i++) { object rowValue = newFieldTable.Rows[i][columnName]; //非空 if (rowValue != DBNull.Value) { pRow.set_Value(addFieldIndex, rowValue); pCursor.UpdateRow(pRow); } pRow = pCursor.NextRow(); } }
/// <summary> /// 获取不可编辑字段 /// </summary> /// <param name="layer">图层</param> /// <returns></returns> public static List <string> GetReadonlyFieldNames(ILayer layer) { ITable iTable = AttributeTableClass.GetITableByLayer(layer); if (iTable == null) { return(null); } IFields fields = iTable.Fields; List <string> outField = new List <string>(); for (int i = 0; i < fields.FieldCount; i++) { IField curField = fields.Field[i]; //字段可编辑,则跳过 if (IsFieldEditable(curField)) { continue; } outField.Add(curField.Name); } return(outField); }
/// <summary> /// 修改字段 /// </summary> /// <param name="layer"></param> public ChangeFieldClass(ILayer layer) { fieldTable = AttributeTableClass.GetITableByLayer(layer); }
/// <summary> /// 获取栅格的属性表 /// </summary> /// <param name="raster">栅格</param> /// <returns></returns> public static DataTable GetRasterTableByIRaster(IRaster raster) { ITable iTable = GetTableByRaster(raster); return(AttributeTableClass.GetAttributeTableByITable(iTable)); }
/// <summary> /// 根据名称获取字段 /// </summary> /// <param name="layer">图层</param> /// <param name="fieldName">字段名称</param> /// <returns></returns> public static IField GetFieldByName(ILayer layer, string fieldName) { return(GetFieldByName(AttributeTableClass.GetITableByLayer(layer), fieldName)); }