在数据集下建立FeatureClass
占行
1 /// <summary> 2 /// 在数据集下创建图层 3 /// </summary> 4 /// <param name="featureClassName">图层名称</param> 5 /// <param name="dicFields">属性结构</param> 6 /// <param name="pfeaturedataset">数据集名称</param> 7 /// <param name="classExtensionUID">类型UID</param> 8 /// <param name="featureWorkspace">工作空间</param> 9 /// <param name="spatialRef">空间参考</param> 10 /// <param name="esrigeomtryType">几何类型</param> 11 /// <returns></returns> 12 public static IFeatureClass CreateFeatureClass(string featureClassName, Dictionary<string, string> dicFields, IFeatureDataset pfeaturedataset, UID classExtensionUID, IFeatureWorkspace featureWorkspace, esriGeometryType esrigeomtryType, ISpatialReference spatialRef) 13 { 14 IFeatureClass featureClass = null; 15 //featureClass = ExitFeatureClass((IWorkspace)featureWorkspace, featureClassName, esriDatasetType.esriDTFeatureClass); 16 if (featureClass != null) 17 { 18 return featureClass; 19 } 20 21 try 22 { 23 //创建要素类的字段集合 24 IFields fields = new FieldsClass(); 25 IFieldsEdit fieldsEdit = (IFieldsEdit)fields; 26 if (dicFields != null && dicFields.Count == 0) 27 { 28 return null; 29 } 30 foreach (var item in dicFields) 31 { 32 fieldsEdit.AddField(BuildField(item.Key, item.Value)); 33 } 34 35 #region 添加Shape字段 36 //添加Shape字段 37 IGeometryDef geometryDef = new GeometryDefClass(); 38 IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; 39 geometryDefEdit.GeometryType_2 = esrigeomtryType; 40 geometryDefEdit.SpatialReference_2 = spatialRef; 41 42 IField geometryField = new FieldClass(); 43 IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField; 44 geometryFieldEdit.Name_2 = "SHAPE"; 45 geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 46 geometryFieldEdit.GeometryDef_2 = geometryDef; 47 fieldsEdit.AddField(geometryField); 48 #endregion 49 50 //创建一个Validated Fields集合 51 IFieldChecker fieldChecker = new FieldCheckerClass(); 52 IEnumFieldError enumFieldError = null; 53 IFields validatedFields = null; 54 fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace; 55 fieldChecker.Validate(fields, out enumFieldError, out validatedFields); 56 57 featureClass = pfeaturedataset.CreateFeatureClass(featureClassName, validatedFields, null, classExtensionUID, 58 esriFeatureType.esriFTSimple, "SHAPE", ""); 59 return featureClass; 60 } 61 catch (Exception ex) 62 { 63 } 64 return null; 65 } 66 ///// <summary> 67 /// 创建属性结构 68 /// </summary> 69 /// <param name="fldname">属性结构名称</param> 70 /// <param name="fldtype">属性结构字段类型</param> 71 /// <returns></returns> 72 private static IField BuildField(string fldname, string fldtype) 73 { 74 IFields fields = new FieldsClass(); 75 IField oidField = new FieldClass(); 76 IFieldEdit oidFieldEdit = (IFieldEdit)oidField; 77 oidFieldEdit.Name_2 = fldname; 78 switch (fldtype.ToString().ToLower()) 79 { 80 case "objectid": 81 oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; 82 break; 83 case "longinteger": 84 oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; 85 break; 86 case "shortinteger": 87 oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; 88 break; 89 case "text": 90 oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; 91 oidFieldEdit.Length_2 = 50; 92 break; 93 case "double": 94 oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; 95 break; 96 case "geometry": 97 oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 98 break; 99 } 100 return oidField; 101 }