using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using ESRI.ArcGIS.ADF; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using System.Data; namespace Kingo.RuleCheck.AEHelper { public class FeatureClassAPI : IFeatureClassAPI { private IFeatureClass _CurrentFeatureClass; public FeatureClassAPI(IFeatureClass pFeatureClass) { this._CurrentFeatureClass = pFeatureClass; } /// /// 当前要素类 /// public ESRI.ArcGIS.Geodatabase.IFeatureClass FeatureClass { get { return _CurrentFeatureClass; } } /// /// 关闭/释放当前要素类 /// public bool CloseFeatureClass() { try { if (this._CurrentFeatureClass != null) { //while (Marshal.ReleaseComObject(this._CurrentFeatureClass) > 0) { } Marshal.ReleaseComObject(this._CurrentFeatureClass); } return true; } catch (Exception ex) { throw ex; } } /// /// 条件过滤筛选要素 /// /// /// public List QueryFeatures(IQueryFilter pQueryFilter) { List result = new List(); try { if (this.FeatureClass != null) { //IFeatureCursor cursor = this.FeatureClass.Search(pQueryFilter, true); //IFeature feature = null; //while ((feature = cursor.NextFeature()) != null) //{ // result.Add(feature); //} //Marshal.ReleaseComObject(cursor); IList lstFeature = new List(); //查询条件 IFeatureCursor pFeatureCursor = FeatureClass.Search(pQueryFilter, false); IFeature pFeature = pFeatureCursor.NextFeature(); //将要素放到集合中 while (pFeature != null) { result.Add(pFeature); pFeature = pFeatureCursor.NextFeature(); } Marshal.ReleaseComObject(pFeatureCursor); //return lstFeature; } return result; } catch (Exception ex) { throw ex; } } /// /// 空间过滤筛选要素,压盖查询 /// /// /// public List SpatialFilterQueryFeatures(ISpatialFilter pFilter, bool IsCut = false) { List result = new List(); try { if (this.FeatureClass != null) { ITopologicalOperator pTopologicalOperator = pFilter.Geometry as ITopologicalOperator; IFeatureCursor cursor = this.FeatureClass.Search(pFilter, false); IFeature pFeature = cursor.NextFeature(); while (pFeature != null) { if (IsCut && pTopologicalOperator != null) { //IFeature feature = FeatureAPI.FeatureCopy(pFeature); pFeature.Shape = pTopologicalOperator.Intersect(pFeature.ShapeCopy, pFeature.ShapeCopy.Dimension); result.Add(pFeature); } else { result.Add(pFeature); } //Marshal.ReleaseComObject(pFeature); pFeature = cursor.NextFeature(); } Marshal.ReleaseComObject(cursor); } return result; } catch (Exception ex) { throw ex; } } /// /// 根据筛选条件查询要素到DataTable /// /// 筛选条件 /// public int QueryFeaturesToDataTable(IQueryFilter pQueryFilter, int pQueryCount, out DataTable pResult) { int DataCount = 0; try { pResult = new DataTable(); if (this.FeatureClass != null) { #region 将IField转化为DataColumn for (int i = 0; i < FeatureClass.Fields.FieldCount; i++) { IField field = FeatureClass.Fields.get_Field(i); if (field.Name.ToUpper().Contains("SHAPE")) continue; DataColumn col = new DataColumn(); col.ExtendedProperties.Add("index", i); col.ColumnName = field.Name; col.Caption = field.AliasName; switch (field.Type) { case esriFieldType.esriFieldTypeSmallInteger: col.DataType = typeof(Int16); break; case esriFieldType.esriFieldTypeInteger: col.DataType = typeof(Int32); break; case esriFieldType.esriFieldTypeSingle: break; case esriFieldType.esriFieldTypeDouble: col.DataType = typeof(double); break; case esriFieldType.esriFieldTypeString: col.DataType = typeof(string); break; case esriFieldType.esriFieldTypeDate: col.DataType = typeof(DateTime); break; case esriFieldType.esriFieldTypeOID: col.DataType = typeof(Int32); break; case esriFieldType.esriFieldTypeGeometry: break; case esriFieldType.esriFieldTypeBlob: break; case esriFieldType.esriFieldTypeRaster: break; case esriFieldType.esriFieldTypeGUID: break; case esriFieldType.esriFieldTypeGlobalID: break; case esriFieldType.esriFieldTypeXML: break; default: break; } //if (field.Name.Equals("TBBH", StringComparison.CurrentCultureIgnoreCase)) //{ // col.DataType = typeof(Int32); //} col.ReadOnly = !field.Editable;//编辑状态启用默认是否可以编辑 pResult.Columns.Add(col); System.Runtime.InteropServices.Marshal.ReleaseComObject(field); } #endregion IList lstFeature = new List(); //查询条件 IFeatureCursor pFeatureCursor = FeatureClass.Search(pQueryFilter, false); IFeature pFeature = null; //将要素放到集合中 while ((pFeature = pFeatureCursor.NextFeature()) != null) { DataCount++; using (ComReleaser releaser = new ComReleaser()) { releaser.ManageLifetime(pFeature); DataRow dr = pResult.NewRow(); for (int i = 0; i < pResult.Columns.Count; i++) { object obj = pFeature.get_Value((int)pResult.Columns[i].ExtendedProperties["index"]); if (obj == null) { continue; } else { if ((obj.ToString()).Contains("1899/12/30 0:00:00")) { obj = System.DBNull.Value; } } //字符串时,去空格,对应bug10473 if (obj is string) { obj = obj.ToString().Trim(); } if ((pResult.Columns[i] as DataColumn).DataType == typeof(Int32) && !(obj is Int32)) { int TBBH; if (int.TryParse(obj.ToString(), out TBBH)) { dr[i] = obj; } } else { dr[i] = obj; } } pResult.Rows.Add(dr); } } Marshal.ReleaseComObject(pFeatureCursor); } return DataCount; } catch (Exception ex) { throw ex; } } /// /// 从当前的要素类中获取指定的要素对象 /// /// OID public ESRI.ArcGIS.Geodatabase.IFeature GetFeature(int OID) { return FeatureClass.GetFeature(OID); } /// /// 添加要素到当前要素类 /// /// 要素 public bool AddFeature(ESRI.ArcGIS.Geodatabase.IFeature pFeature) { bool result = false; IFeatureCursor cursor = null; try { if (this.FeatureClass != null) { using (ComReleaser comReleaser = new ComReleaser()) { IFeatureBuffer newFeature = this.FeatureClass.CreateFeatureBuffer(); comReleaser.ManageLifetime(newFeature); IGeometry geo = pFeature.ShapeCopy; //ITopologicalOperator2 topo = geo as ITopologicalOperator2; //if (!topo.IsSimple) // topo.Simplify(); newFeature.Shape = geo;// pFeature.ShapeCopy; for (int i = 0; i < newFeature.Fields.FieldCount; i++) { IField field = newFeature.Fields.get_Field(i); if (!field.Editable) continue; if (field.Name.ToUpper() == "SHAPE") continue; int index = pFeature.Fields.FindField(field.Name); if (index == -1) continue; if (pFeature.Fields.get_Field(index).Type == field.Type) { newFeature.set_Value(i, pFeature.get_Value(index)); } } cursor = this.FeatureClass.Insert(true); comReleaser.ManageLifetime(cursor); object obj = cursor.InsertFeature(newFeature); pFeature = newFeature as IFeature; cursor.Flush(); //newFeature.Store(); result = true; } } } catch (Exception ex) { throw ex; } return result; } /// /// 添加要素到当前要素类 /// /// 要素 public IFeature AddFeatureAndReturn(ESRI.ArcGIS.Geodatabase.IFeature pFeature) { IFeature result = null; IFeatureCursor cursor = null; try { if (this.FeatureClass != null) { IFeatureBuffer newFeature = this.FeatureClass.CreateFeatureBuffer(); IGeometry geo = pFeature.ShapeCopy; ITopologicalOperator2 topo = geo as ITopologicalOperator2; //if (!topo.IsSimple) topo.Simplify(); newFeature.Shape = geo;// pFeature.ShapeCopy; for (int i = 0; i < newFeature.Fields.FieldCount; i++) { IField field = newFeature.Fields.get_Field(i); if (!field.Editable) continue; if (field.Name.ToUpper() == "SHAPE") continue; int index = pFeature.Fields.FindField(field.Name); if (index == -1) continue; if (pFeature.Fields.get_Field(index).Type == field.Type) { newFeature.set_Value(i, pFeature.get_Value(index)); } } cursor = this.FeatureClass.Insert(true); object obj = cursor.InsertFeature(newFeature); cursor.Flush(); Marshal.ReleaseComObject(newFeature); result = this.FeatureClass.GetFeature(Convert.ToInt32(obj)); //newFeature.Store(); } } catch (Exception ex) { throw ex; } finally { if (cursor != null) { Marshal.ReleaseComObject(cursor); } } return result; } /// /// 添加要素到当前要素类 /// /// 要素 public bool AddFeature(List pDicData) { bool result = false; IFeatureCursor cursor = null; try { if (this.FeatureClass != null) { IFeatureBuffer newFeature = this.FeatureClass.CreateFeatureBuffer(); for (int i = 0; i < newFeature.Fields.FieldCount; i++) { IField field = newFeature.Fields.get_Field(i); if (!field.Editable) continue; K_Field dicField = pDicData.FirstOrDefault(e => e.Name.ToUpper() == field.Name.ToUpper()); if (dicField != null) { if (dicField.Type == field.Type && !(dicField.Value is DBNull)) { newFeature.set_Value(i, dicField.Value); } } } cursor = this.FeatureClass.Insert(true); cursor.InsertFeature(newFeature); cursor.Flush(); result = true; } } catch (Exception ex) { throw ex; } finally { if (cursor != null) { Marshal.ReleaseComObject(cursor); } } return result; } /// /// 获取所有字段 /// /// public List GetFields() { List result = new List(); try { if (this.FeatureClass != null) { for (int i = 0; i < this.FeatureClass.Fields.FieldCount; i++) { K_Field field = new K_Field() { Name = this.FeatureClass.Fields.get_Field(i).Name, AliasName = this.FeatureClass.Fields.get_Field(i).AliasName, Type = this.FeatureClass.Fields.get_Field(i).Type }; result.Add(field); } } } catch (Exception ex) { throw ex; } return result; } /// /// 更新要素 /// /// /// public bool UpdateFeature(ESRI.ArcGIS.Geodatabase.IFeature pFeature) { bool result = false; try { if (this.FeatureClass != null && pFeature != null && this.FeatureClass == pFeature.Class) { IFeature feature = GetFeature(pFeature.OID); feature.Shape = pFeature.ShapeCopy; for (int i = 0; i < feature.Fields.FieldCount; i++) { IField field = feature.Fields.get_Field(i); if (field.Name.ToUpper() == "SHAPE") continue; int index = pFeature.Fields.FindField(field.Name); if (index == -1) continue; if (pFeature.Fields.get_Field(index).Type == field.Type) { feature.set_Value(i, pFeature.get_Value(index)); } } feature.Store(); result = true; } } catch (Exception ex) { throw ex; } return result; } /// /// 从当前的要素类中删除指定的要素对象 /// /// OID public bool DelFeatures(int OID) { bool result = false; try { if (this.FeatureClass != null) { IFeature feature = this.FeatureClass.GetFeature(OID); feature.Delete(); feature.Store(); result = true; } return result; } catch (Exception ex) { throw ex; } } /// /// 向当前要素类添加字段 /// /// 字段 public bool AddField(ESRI.ArcGIS.Geodatabase.IField pField) { ISchemaLock pSchemaLock = FeatureClass as ISchemaLock; try { if (pSchemaLock == null) { return false; } pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); if (FeatureClass.Fields.FindField(pField.Name) == -1) FeatureClass.AddField(pField); return true; } catch (Exception ex) { throw ex; } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } /// /// 向当前要素类添加字段 /// /// 字段 public bool AddField(string pFieldName, esriFieldType pFieldType, string pAliasName) { ISchemaLock pSchemaLock = FeatureClass as ISchemaLock; try { if (pSchemaLock == null || string.IsNullOrWhiteSpace(pFieldName)) { return false; } if (FeatureClass.Fields.FindField(pFieldName) == -1) { IFieldEdit field = new FieldClass(); field.Name_2 = pFieldName; field.AliasName_2 = pAliasName; field.Type_2 = pFieldType; pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); FeatureClass.AddField(field); } return true; } catch (Exception ex) { throw ex; } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } /// /// 重命名要素类 /// /// 修改后的要素类名称 public bool RenameFeatureClass(string newName) { bool renameResult = false; ISchemaLock pSchemaLock = null; try { if (this.FeatureClass == null) { return renameResult; } IDataset dataSet = (FeatureClass as IDataset); if (dataSet != null && dataSet.CanRename()) { pSchemaLock = FeatureClass as ISchemaLock; if (pSchemaLock != null) { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); } dataSet.Rename(newName); return true; } return renameResult; } catch (Exception ex) { throw new Exception("重命名要素类(" + (FeatureClass as FeatureClass).BrowseName + "-" + newName + ")失败:" + ex.Message); } finally { if (pSchemaLock != null) { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } } /// /// 从当前要素类中删除指定的字段 /// /// 字段名称 public bool DeleteField(string pFieldName) { ISchemaLock pSchemaLock = FeatureClass as ISchemaLock; try { if (pSchemaLock == null) return false; pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); int index = FeatureClass.FindField(pFieldName); if (index == -1) return true; FeatureClass.DeleteField(FeatureClass.Fields.get_Field(index)); return true; } catch (Exception ex) { throw ex; } finally { pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } /// /// 将数据拷贝到目标要素类中 /// /// 目标要素类 /// 过滤条件 /// 是否裁剪 public bool FcToFc(IFeatureClass pFc, IQueryFilter pFilter, bool pIsCut) { IFeatureCursor S_Cursor = null; IFeatureCursor T_Cursor = null; try { if (FeatureClass == null || pFc == null) return false; Dictionary dicField = new Dictionary(); for (int i = 0; i < pFc.Fields.FieldCount; i++) { IField field = pFc.Fields.Field[i]; if (field.Name == pFc.ShapeFieldName || field.Name.Contains(pFc.ShapeFieldName) || field.Name == pFc.OIDFieldName || !field.Editable) continue; int index = FeatureClass.Fields.FindField(field.Name); if (index == -1) index = FeatureClass.Fields.FindFieldByAliasName(field.AliasName); if (index == -1) continue; dicField.Add(i, index); } S_Cursor = FeatureClass.Search(pFilter, true); IFeature f = null; IFeatureBuffer buffer = pFc.CreateFeatureBuffer(); T_Cursor = pFc.Insert(true); while ((f = S_Cursor.NextFeature()) != null) { buffer.Shape = f.ShapeCopy; if (pFilter is ISpatialFilter && pIsCut) { if (FeatureAPI.IsInterSect((pFilter as ISpatialFilter).Geometry, f.ShapeCopy)) { buffer.Shape = FeatureAPI.InterSect(f.ShapeCopy, (pFilter as SpatialFilterClass).Geometry); } else { continue; } } foreach (int item in dicField.Keys) { buffer.Value[item] = f.Value[dicField[item]]; } T_Cursor.InsertFeature(buffer); } T_Cursor.Flush(); return true; } catch (Exception) { throw; } } } }