转自:http://blog.csdn.net/chanyinhelv/article/details/8696538
非常感谢!
---------------------------------------------------------------------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using ESRI.ArcGIS.Geodatabase; 5 using ESRI.ArcGIS.Geometry; 6 using ESRI.ArcGIS.Carto; 7 namespace MyGISClass 8 { 9 /// <summary> 10 /// 该类主要包含了要素类的复制以及同要素类数据的加载 11 /// 函数主要用于SDE与Personal GDB之间数据的处理 12 /// </summary> 13 class FeatureClassDataManager 14 { 15 /// <summary> 16 /// 根据传入的源要素类OldFeatureClass,新空间范围,要素存储工作空间,新要素类名 17 /// 产生具有相同字段结构和不同空间范围的要素类 18 /// </summary> 19 /// <param name="OldFeatureClass">源要素类</param> 20 /// <param name="SaveFeatWorkspace">存储工作空间</param> 21 /// <param name="FeatClsName">新要素类名</param> 22 /// <param name="pDomainEnv">新空间范围,可为null</param> 23 /// <returns></returns> 24 public IFeatureClass CloneFeatureClassInWorkspace(IFeatureClass OldFeatureClass, IFeatureWorkspace SaveFeatWorkspace, string FeatClsName, IEnvelope pDomainEnv) 25 { 26 IFields pFields = CloneFeatureClassFields(OldFeatureClass, pDomainEnv); 27 return SaveFeatWorkspace.CreateFeatureClass(FeatClsName, pFields, null, null, esriFeatureType.esriFTSimple, OldFeatureClass.ShapeFieldName, ""); 28 } 29 /// <summary> 30 /// 复制AnnotationClass,未完待续 31 /// </summary> 32 /// <param name="OldFeatureClass"></param> 33 /// <param name="SaveFeatWorkspace"></param> 34 /// <param name="FeatClsName"></param> 35 /// <param name="pDomainEnv"></param> 36 /// <returns></returns> 37 public IFeatureClass CloneAnnotationClassInWorkspace(IFeatureClass OldFeatureClass, IFeatureWorkspace SaveFeatWorkspace, string FeatClsName, IEnvelope pDomainEnv) 38 { 39 IFeatureWorkspaceAnno pFWSAnno = (IFeatureWorkspaceAnno)SaveFeatWorkspace; 40 IAnnoClass pAnnoClass = (IAnnoClass)OldFeatureClass.Extension; 41 return null; 42 } 43 /// <summary> 44 /// 将inFeatureClass要素类中所有符合pQueryFilter的要素复制到saveFeatureClass中,仅复制不做任何修改 45 /// </summary> 46 /// <param name="inFeatureClass">源要素类</param> 47 /// <param name="saveFeatureClass">存储要素类</param> 48 /// <param name="pQueryFilter">过滤参数</param> 49 /// <returns></returns> 50 public bool LoadFeatureClass(IFeatureClass inFeatureClass, IFeatureClass saveFeatureClass, IQueryFilter pQueryFilter) 51 { 52 //生成两个要素类字段的对应表 53 Dictionary<int, int> pFieldsDict = new Dictionary<int, int>(); 54 this.GetFCFieldsDirectory(inFeatureClass, saveFeatureClass, ref pFieldsDict); 55 IFeatureCursor pinFeatCursor = inFeatureClass.Search(pQueryFilter, false); 56 long nCount = inFeatureClass.FeatureCount(pQueryFilter); 57 IFeature pinFeat = pinFeatCursor.NextFeature(); 58 IFeatureCursor psaveFeatCursor = saveFeatureClass.Insert(true); 59 //使用IFeatureBuffer在内存中产生缓存避免多次打开,关闭数据库 60 IFeatureBuffer psaveFeatBuf = null; 61 IFeature psaveFeat = null; 62 long n = 0; 63 while (pinFeat != null) 64 { 65 try 66 { 67 psaveFeatBuf = saveFeatureClass.CreateFeatureBuffer(); 68 psaveFeat = psaveFeatBuf as IFeature; 69 if (inFeatureClass.FeatureType == esriFeatureType.esriFTAnnotation) 70 { 71 IAnnotationFeature pAF = (IAnnotationFeature)pinFeat; 72 IAnnotationFeature pNAF = (IAnnotationFeature)psaveFeat; 73 if (pAF.Annotation != null) 74 { 75 pNAF.Annotation = pAF.Annotation; 76 } 77 } 78 psaveFeat.Shape = pinFeat.Shape; 79 foreach (KeyValuePair<int, int> keyvalue in pFieldsDict) 80 { 81 if (pinFeat.get_Value(keyvalue.Key).ToString() == "") 82 { 83 if (psaveFeat.Fields.get_Field(keyvalue.Value).Type == esriFieldType.esriFieldTypeString) 84 { 85 psaveFeat.set_Value(keyvalue.Value, ""); 86 } 87 else 88 { 89 psaveFeat.set_Value(keyvalue.Value, 0); 90 } 91 } 92 else 93 { 94 psaveFeat.set_Value(keyvalue.Value, pinFeat.get_Value(keyvalue.Key)); 95 } 96 } 97 psaveFeatCursor.InsertFeature(psaveFeatBuf); 98 } 99 catch (Exception ex) { } 100 finally 101 { 102 psaveFeat = null; 103 n++; 104 if (n % 2000 == 0) 105 { 106 psaveFeatCursor.Flush(); 107 } 108 pinFeat = pinFeatCursor.NextFeature(); 109 } 110 } 111 psaveFeatCursor.Flush(); 112 return true; 113 } 114 private IFields CloneFeatureClassFields(IFeatureClass pFeatureClass, IEnvelope pDomainEnv) 115 { 116 IFields pFields = new FieldsClass(); 117 IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; 118 //根据传入的要素类,将除了shape字段之外的字段复制 119 long nOldFieldsCount = pFeatureClass.Fields.FieldCount; 120 long nOldGeoIndex = pFeatureClass.Fields.FindField(pFeatureClass.ShapeFieldName); 121 for (int i = 0; i < nOldFieldsCount; i++) 122 { 123 if (i != nOldGeoIndex) 124 { 125 pFieldsEdit.AddField(pFeatureClass.Fields.get_Field(i)); 126 } 127 else 128 { 129 IGeometryDef pGeomDef = new GeometryDefClass(); 130 IGeometryDefEdit pGeomDefEdit = (IGeometryDefEdit)pGeomDef; 131 ISpatialReference pSR = null; 132 if (pDomainEnv != null) 133 { 134 pSR = new UnknownCoordinateSystemClass(); 135 pSR.SetDomain(pDomainEnv.XMin, pDomainEnv.XMax, pDomainEnv.YMin, pDomainEnv.YMax); 136 } 137 else 138 { 139 IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset; 140 pSR = CloneSpatialReference(pGeoDataset.SpatialReference); 141 } 142 //设置新要素类Geometry的参数 143 pGeomDefEdit.GeometryType_2 = pFeatureClass.ShapeType; 144 pGeomDefEdit.GridCount_2 = 1; 145 pGeomDefEdit.set_GridSize(0, 10); 146 pGeomDefEdit.AvgNumPoints_2 = 2; 147 pGeomDefEdit.SpatialReference_2 = pSR; 148 //产生新的shape字段 149 IField pField = new FieldClass(); 150 IFieldEdit pFieldEdit = (IFieldEdit)pField; 151 pFieldEdit.Name_2 = "shape"; 152 pFieldEdit.AliasName_2 = "shape"; 153 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 154 pFieldEdit.GeometryDef_2 = pGeomDef; 155 pFieldsEdit.AddField(pField); 156 } 157 } 158 return pFields; 159 } 160 private ISpatialReference CloneSpatialReference(ISpatialReference pSrcSpatialReference) 161 { 162 double xmin, xmax, ymin, ymax; 163 pSrcSpatialReference.GetDomain(out xmin, out xmax, out ymin, out ymax); 164 ISpatialReference pSR = new UnknownCoordinateSystemClass(); 165 pSR.SetDomain(xmin, xmax, ymin, ymax); 166 return pSR; 167 } 168 private void GetFCFieldsDirectory(IFeatureClass pFCold,IFeatureClass pFCnew,ref Dictionary<int,int> FieldsDictionary) 169 { 170 for(int i=0;i<pFCold.Fields.FieldCount;i++) 171 { 172 string tmpstrold=pFCold.Fields.get_Field(i).Name.ToUpper(); 173 switch(tmpstrold) 174 { 175 case "OBJECTID": 176 case "SHAPE": 177 case "SHAPE_LENGTH": 178 case "SHAPE_AREA": 179 case "FID": 180 { 181 //以上字段由系统自动生成 182 break; 183 } 184 default: 185 { 186 for(int j=0;j<pFCnew.Fields.FieldCount;j++) 187 { 188 string tmpstrnew=pFCnew.Fields.get_Field(j).Name.ToUpper(); 189 if(tmpstrold==tmpstrnew) 190 { 191 FieldsDictionary.Add(i,j); 192 break; 193 } 194 } 195 break; 196 } 197 } 198 } 199 } 200 } 201 }
方法二:
转自:http://www.cnblogs.com/henyihanwobushi/archive/2013/03/21/2972415.html
1 /// <summary> 2 3 ///复制一个IFeatureClass中的要素到另外一个当中 4 5 /// </summary> 6 7 /// <param name="pFromFeatureClass">源数据所在的IFeatureClass</param> 8 9 /// <param name="pToFeatureClass">目标数据所在的IFeatureClass</param> 10 11 private void CopyFeatureClass(IFeatureClass pFromFeatureClass, IFeatureClass pToFeatureClass) 12 13 { 14 15 try { 16 17 IFeatureCursor pFromFeatureCursor = pFromFeatureClass.Search(null, false); 18 19 IFeatureCursor pToFeatureCursor = pToFeatureClass.Insert(true); 20 21 IFeatureBuffer pFeatureBuffer = pToFeatureClass.CreateFeatureBuffer(); 22 23 IFeature pFromFeature = pFromFeatureCursor.NextFeature(); 24 25 while (pFromFeature != null) 26 27 { 28 29 int IndexShape = pFeatureBuffer.Fields.FindField("Shape"); 30 31 pFeatureBuffer.set_Value(IndexShape, pFromFeature); 32 33 pFeatureBuffer.Shape = pFromFeature.Shape; 34 35 pToFeatureCursor.InsertFeature(pFeatureBuffer); 36 37 pFromFeature = pFromFeatureCursor.NextFeature(); 38 39 pFeatureBuffer = pToFeatureClass.CreateFeatureBuffer(); 40 41 } 42 43 pToFeatureCursor.Flush(); 44 45 } 46 47 catch (Exception ex) 48 49 { 50 51 throw ex; 52 53 } 54 55 }