用Arcengine创建图层

别人写的,感觉不错

///


  2        /// 创建要素类
  3        ///

  4        /// IWorkspace或者IFeatureDataset对象
  5        /// 要素类名称
  6        /// 空间参考
  7        /// 要素类型
  8        /// 几何类型
  9        /// 字段集
 10        /// CLSID值
 11        /// EXTCLSID值
 12        /// 配置信息关键词
 13        /// 返回IFeatureClass
 14        public static IFeatureClass CreateFeatureClass(object pObject, string pName, ISpatialReference pSpatialReference, esriFeatureType pFeatureType,
 15                                       esriGeometryType pGeometryType, IFields pFields, UID pUidClsId, UID pUidClsExt, string pConfigWord)
 16        {
 17            错误检测#region 错误检测
 18            if (pObject == null)
 19            {
 20                throw (new Exception("[pObject] 不能为空!"));
 21            }
 22            if (!((pObject is IFeatureWorkspace) || (pObject is IFeatureDataset)))
 23            {
 24                throw (new Exception("[pObject] 必须为IFeatureWorkspace 或者 IFeatureDataset"));
 25            }
 26            if (pName.Length == 0)
 27            {
 28                throw (new Exception("[pName] 不能为空!"));
 29            }
 30            if ((pObject is IWorkspace) && (pSpatialReference == null))
 31            {
 32                throw (new Exception("[pSpatialReference] 不能为空(对于单独的要素类)"));
 33            }
 34            #endregion
 35
 36            pUidClsID字段为空时#region pUidClsID字段为空时
 37            if (pUidClsId == null)
 38            {
 39                pUidClsId = new UIDClass();
 40                switch (pFeatureType)
 41                {
 42                    case (esriFeatureType.esriFTSimple):
 43                        if (pGeometryType == esriGeometryType.esriGeometryLine)
 44                            pGeometryType = esriGeometryType.esriGeometryPolyline;
 45                        pUidClsId.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
 46                        break;
 47                    case (esriFeatureType.esriFTSimpleJunction):
 48                        pGeometryType = esriGeometryType.esriGeometryPoint;
 49                        pUidClsId.Value = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
 50                        break;
 51                    case (esriFeatureType.esriFTComplexJunction):
 52                        pUidClsId.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
 53                        break;
 54                    case (esriFeatureType.esriFTSimpleEdge):
 55                        pGeometryType = esriGeometryType.esriGeometryPolyline;
 56                        pUidClsId.Value = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
 57                        break;
 58                    case (esriFeatureType.esriFTComplexEdge):
 59                        pGeometryType = esriGeometryType.esriGeometryPolyline;
 60                        pUidClsId.Value = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
 61                        break;
 62                    case (esriFeatureType.esriFTAnnotation):
 63                        pGeometryType = esriGeometryType.esriGeometryPolygon;
 64                        pUidClsId.Value = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
 65                        break;
 66                    case (esriFeatureType.esriFTDimension):
 67                        pGeometryType = esriGeometryType.esriGeometryPolygon;
 68                        pUidClsId.Value = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
 69                        break;
 70                }
 71            }
 72            #endregion
 73
 74            pUidClsExt字段为空时#region pUidClsExt字段为空时
 75            if (pUidClsExt == null)
 76            {
 77                switch (pFeatureType)
 78                {
 79                    case esriFeatureType.esriFTAnnotation:
 80                        pUidClsExt = new UIDClass();
 81                        pUidClsExt.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
 82                        break;
 83                    case esriFeatureType.esriFTDimension:
 84                        pUidClsExt = new UIDClass();
 85                        pUidClsExt.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
 86                        break;
 87                }
 88            }
 89            #endregion
 90
 91            字段集合为空时#region 字段集合为空时
 92            if (pFields == null)
 93            {
 94                //实倒化字段集合对象
 95                pFields = new FieldsClass();
 96                IFieldsEdit tFieldsEdit = (IFieldsEdit)pFields;
 97
 98                //创建几何对象字段定义
 99                IGeometryDef tGeometryDef = new GeometryDefClass();
100                IGeometryDefEdit tGeometryDefEdit = tGeometryDef as IGeometryDefEdit;
101
102                //指定几何对象字段属性值
103                tGeometryDefEdit.GeometryType_2 = pGeometryType;
104                tGeometryDefEdit.GridCount_2 = 1;
105                tGeometryDefEdit.set_GridSize(0, 1000);
106                if (pObject is IWorkspace)
107                {
108                    tGeometryDefEdit.SpatialReference_2 = pSpatialReference;
109                }
110
111                //创建OID字段
112                IField fieldOID = new FieldClass();
113                IFieldEdit fieldEditOID = fieldOID as IFieldEdit;
114                fieldEditOID.Name_2 = "OBJECTID";
115                fieldEditOID.AliasName_2 = "OBJECTID";
116                fieldEditOID.Type_2 = esriFieldType.esriFieldTypeOID;
117                tFieldsEdit.AddField(fieldOID);
118
119                //创建几何字段
120                IField fieldShape = new FieldClass();
121                IFieldEdit fieldEditShape = fieldShape as IFieldEdit;
122                fieldEditShape.Name_2 = "SHAPE";
123                fieldEditShape.AliasName_2 = "SHAPE";
124                fieldEditShape.Type_2 = esriFieldType.esriFieldTypeGeometry;
125                fieldEditShape.GeometryDef_2 = tGeometryDef;
126                tFieldsEdit.AddField(fieldShape);
127            }
128            #endregion
129
130            //几何对象字段名称
131            string strShapeFieldName = "";
132            for (int i = 0; i < pFields.FieldCount; i++)
133            {
134                if (pFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
135                {
136                    strShapeFieldName = pFields.get_Field(i).Name;
137                    break;
138                }
139            }
140
141            if (strShapeFieldName.Length == 0)
142            {
143                throw (new Exception("字段集中找不到几何对象定义"));
144            }
145
146            IFeatureClass tFeatureClass = null;
147            if (pObject is IWorkspace)
148            {
149                //创建独立的FeatureClass
150                IWorkspace tWorkspace = pObject as IWorkspace;
151                IFeatureWorkspace tFeatureWorkspace = tWorkspace as IFeatureWorkspace;
152                tFeatureClass = tFeatureWorkspace.CreateFeatureClass(pName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, pConfigWord);
153            }
154            else if (pObject is IFeatureDataset)
155            {
156                //在要素集中创建FeatureClass
157                IFeatureDataset tFeatureDataset = (IFeatureDataset)pObject;
158                tFeatureClass = tFeatureDataset.CreateFeatureClass(pName, pFields, pUidClsId, pUidClsExt, pFeatureType, strShapeFieldName, pConfigWord);
159            }
160
161            return tFeatureClass;
162        }

你可能感兴趣的:(用Arcengine创建图层)