C#arcengine新建要素数据集并在要素集下创建新图层
时间: 2024-01-17 17:04:20 浏览: 140
ArcEngine中的图层对象创建
3星 · 编辑精心推荐
您可以使用ArcObjects中的IFeatureDataset和IFeatureClassDescription接口来创建新的要素数据集和要素类。下面是一个基本的代码示例:
```
// 获取工作空间
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
IWorkspace workspace = workspaceFactory.OpenFromFile("C:\data.gdb", 0);
// 获取要素集描述
UID featureDatasetUID = new UIDClass();
featureDatasetUID.Value = "esriGeoDatabase.FeatureDataset";
IFeatureDatasetDescription featureDatasetDescription = new FeatureDatasetDescriptionClass();
IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass();
IObjectClassDescription objectClassDescription = (IObjectClassDescription)featureClassDescription;
// 创建要素集
IFeatureDataset featureDataset = ((IFeatureWorkspace)workspace).CreateFeatureDataset("myFeatureDataset", featureDatasetDescription);
// 创建要素类
UID featureClassUID = new UIDClass();
featureClassUID.Value = "esriGeoDatabase.FeatureClass";
IObjectClassDescription objectClassDescription = (IObjectClassDescription)featureClassDescription;
IFields fields = objectClassDescription.RequiredFields;
fields.AddField(CreateField("ID", esriFieldType.esriFieldTypeOID));
fields.AddField(CreateField("Name", esriFieldType.esriFieldTypeString));
IFeatureClass featureClass = featureDataset.CreateFeatureClass("myFeatureClass", fields, featureClassDescription.InstanceCLSID, featureClassDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "Shape", "");
// 创建图层
IMap map = new MapClass();
IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureClass;
featureLayer.Name = "myFeatureLayer";
map.AddLayer(featureLayer);
```
其中CreateField方法用于创建字段,示例代码如下:
```
private IField CreateField(string name, esriFieldType fieldType)
{
IField field = new FieldClass();
IFieldEdit fieldEdit = (IFieldEdit)field;
fieldEdit.Name_2 = name;
fieldEdit.Type_2 = fieldType;
return field;
}
```
阅读全文