创建要素FeatureClass

创建要素数据
 /// 
        /// 在数据集下创建图层
        /// 
        /// 图层名称
        /// 属性结构
        /// 数据集名称
        /// 类型UID
        /// 工作空间
        /// 空间参考
        /// 几何类型
        /// 
        public static IFeatureClass CreateFeatureClass(string featureClassName, Dictionary dicFields, IFeatureDataset pfeaturedataset, UID classExtensionUID, IFeatureWorkspace featureWorkspace, esriGeometryType esrigeomtryType, ISpatialReference spatialRef)
        {
            IFeatureClass featureClass = null;

            featureClass = ExitFeatureClass((IWorkspace)featureWorkspace, featureClassName, esriDatasetType.esriDTFeatureClass);
            if (featureClass != null)
            {
                return featureClass;
            }

            try
            {
                //创建要素类的字段集合
                IFields fields = new FieldsClass();
                IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
                if (dicFields != null && dicFields.Count == 0)
                {
                    return null;
                }
                foreach (var item in dicFields)
                {
                    fieldsEdit.AddField(BuildField(item.Key, item.Value));
                }

                #region 添加Shape字段
                //添加Shape字段
                IGeometryDef geometryDef = new GeometryDefClass();
                IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
                geometryDefEdit.GeometryType_2 = esrigeomtryType;
                geometryDefEdit.SpatialReference_2 = spatialRef;

                IField geometryField = new FieldClass();
                IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField;
                geometryFieldEdit.Name_2 = "Shape";
                geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                geometryFieldEdit.GeometryDef_2 = geometryDef;
                fieldsEdit.AddField(geometryField);
                #endregion

                //创建一个Validated Fields集合
                IFieldChecker fieldChecker = new FieldCheckerClass();
                IEnumFieldError enumFieldError = null;
                IFields validatedFields = null;
                fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace;
                fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

                featureClass = pfeaturedataset.CreateFeatureClass(featureClassName, validatedFields, null, classExtensionUID,
               esriFeatureType.esriFTSimple, "Shape", "");
                return featureClass;
            }
            catch (Exception ex)
            {
            }
            return null;
        }
 /// 
       /// 创建属性结构
       /// 
       /// 属性结构名称
       /// 属性结构字段类型
       /// 
        private static IField BuildField(string fldname, string fldtype)
        {
            IFields fields = new FieldsClass();

            IField oidField = new FieldClass();
            IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
            oidFieldEdit.Name_2 = fldname;

            switch (fldtype.ToString().ToLower())
            {
                case "objectid":
                    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
                    break;

                case "longinteger":
                    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
                    break;

                case "shortinteger":
                    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
                    break;
                case "text":
                    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                    oidFieldEdit.Length_2 = 50;
                    break;

                case "double":
                    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                    break;

                case "geometry":
                    oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                    break;

            }



            return oidField;

        }

欢迎扫描微信订阅号:

创建要素FeatureClass_第1张图片

你可能感兴趣的:(C#,ARCGIS,Engine,ARCGIS,Engine)