用AE搞gis开发,增加、修改、删除ifeature 是难免的,项目中写了一个通用的添加要素的通用方法,发出来,望大家批评指正
1)添加新要素
/// <summary> /// 添加新要素 /// </summary> /// <param name="pFeatClass">要更新的要素类</param> /// <param name="fieldAndValues">更新的字段值,key是字段的名称,value是字段的值</param> /// <param name="geometry">图?形?对?象?</param> /// <returns>是?否?添?加?成?功?</returns> public static bool NewFeature(IFeatureClass pFeatClass, Hashtable fieldAndValues, IGeometry geometry) { bool IsEdited = false; if (geometry == null) return false; if (pFeatClass == null) throw new Exception("要?更?新?的?要?素?类?不?能?为?空?!?"); if (pFeatClass.ShapeType != geometry.GeometryType) throw new Exception("要?更?新?的?要?素?类?和?要?素?的?对?象?类?型?不?一?致?,?无?法?更?新?!?"); IDataset pDS = pFeatClass as IDataset; IWorkspaceEdit pWSE = pDS.Workspace as IWorkspaceEdit; if (pWSE.IsBeingEdited()) { IsEdited = true; } else { pWSE.StartEditing(true); } pWSE.StartEditOperation(); IFeature pFeat = pFeatClass.CreateFeature(); try { foreach (string key in fieldAndValues.Keys) { int pIndex = pFeat.Fields.FindField(key); if (pIndex < 0) throw new Exception("要?素?不?存?在?名?称?为?"+key+ " 的?字?段?!?"); pFeat.set_Value(pIndex, fieldAndValues[key]); } pFeat.Shape = geometry; pFeat.Store(); pWSE.StopEditOperation(); if (!IsEdited) pWSE.StopEditing(true); return true; } catch (Exception ex) { pWSE.AbortEditOperation(); if (!IsEdited) pWSE.StopEditing(false); return false; } finally { ReleaseComObject(pDS); ReleaseComObject(pWSE); ReleaseComObject(pFeat); } }
2)更新新要素
输入的参数比新建的多了一个,strWhere 是pFeatClass搜索要删除的要素的过滤条件
public static bool UpdateFeature(IFeatureClass pFeatClass, string strWhere, Hashtable fieldNameAndValues, IGeometry geometry) { bool IsEdited = false; if (pFeatClass == null) throw new Exception("要更新的要素类不能为空!?"); if (pFeatClass.ShapeType != geometry.GeometryType) throw new Exception("要?更?新?的?要?素?类?和?要?素?的?对?象?类?型?不?一?致?,?无?法?更?新?!?"); IDataset pDS = pFeatClass as IDataset; IWorkspaceEdit pWSE = pDS.Workspace as IWorkspaceEdit; if (pWSE.IsBeingEdited()) { IsEdited = true; } else { pWSE.StartEditing(true); } pWSE.StartEditOperation(); IQueryFilter pQueryFilter = new QueryFilterClass(); pQueryFilter.WhereClause = strWhere; IFeatureCursor pFeatureCursor = pFeatClass.Search(pQueryFilter, false); IFeature pFeat = pFeatureCursor.NextFeature(); try { while (pFeat != null) { if (fieldNameAndValues != null) { foreach (object key in fieldNameAndValues.Keys) { int pIndex = pFeat.Fields.FindField(key.ToString()); if (pIndex > 0) pFeat.set_Value(pIndex,fieldNameAndValues[key]); else { throw new Exception(string.Format("要?素?类?'{0}'不?存?在?名?称?为?'{1}'的?属?性?!?",pFeatClass.AliasName,key.ToString())); } } } if (geometry != null) pFeat.Shape = geometry; pFeat.Store(); pFeat = pFeatureCursor.NextFeature(); } pWSE.StopEditOperation(); if (!IsEdited) pWSE.StopEditing(true); //Marshal.FinalReleaseComObject(pFeatureCursor); return true; } catch (Exception ex) { pWSE.AbortEditOperation(); if (!IsEdited) pWSE.StopEditing(false); return false; } finally { ReleaseComObject(pDS); ReleaseComObject(pWSE); ReleaseComObject(pQueryFilter); ReleaseComObject(pFeat); }
当条件 为Null 或“” 时,将删除所有数据
/// <summary> /// 删?除?符?合?条?件?的?要?素? /// </summary> /// <param name="pFeatClass">图?层?要?素?类?</param> /// <param name="strWhere">查?询?条?件?</param> /// <returns>是?否?删?除?成?功?</returns> public static bool DeleteFeature(IFeatureClass pFeatClass, string strWhere) { if (pFeatClass == null) return false; ITable pTable = pFeatClass as ITable; IQueryFilter pQueryFilter = new QueryFilterClass(); pQueryFilter.WhereClause = strWhere; try { pTable.DeleteSearchedRows(pQueryFilter); return true; } catch (Exception ex) { return false; } finally { ReleaseComObject(pTable); ReleaseComObject(pQueryFilter); } }