创建要素集
创建要素集可以通过IFeatureWorkspace接口的CreateFeatureclass方法实现。具体方法如下:
//建立SDE连接
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", server);
propertySet.SetProperty("INSTANCE", instance);
propertySet.SetProperty("DATABASE", database);
propertySet.SetProperty("USER", user);
propertySet.SetProperty("PASSWORD", password);
propertySet.SetProperty("VERSION", version);
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)new
SdeWorkspaceFactoryClass();
IWorkspace pWorkspace = workspaceFactory.Open(propertySet, 0);
//接口查询,通过IFeatureWorkspace接口创建要素集/图层
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
//设置字段
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
IField pField = new FieldClass();
IFieldEdit pFieldEdit = (IFieldEdit)pField;
//几何图形定义
IGeometryDef pGeomDef = new GeometryDefClass();
IGeometryDefEdit pGeomDefEdit = (IGeometryDefEdit)pGeomDef;
pGeomDefEdit.GeometryType = esriGeometryType.esriGeometryPolygon;
pgeomDefEdit.SpatialReference = new UnknownCoordinateSystem;//可从其它要素集获取,这里设为坐标系统未知。
//添加SHAPE字段
pFieldEdit.Name_2 = "SHAPE";
pFieldEdit.Type_2 = esriFieldType.esriFieldGeometry;
pFieldEdit.GeomtryDef_2 = pGeomDef;
pFieldsEdit.AddField(pField);
//添加一般字段
pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
pFieldEdit.Length = 30;
pFieldEdit.Name_2 = "text";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
//创建要素集/图层
IFeatureClass pFeatureClass;
pFeatureClass = pFeatureWorkspace.CreateFeatureClass
(strName,pFields,null,null,esriFeatureType.esriFTsimple,"SHAPE","");
//strName-图层名称;"SHAPE"-图形字段名称