C#arcengine复制相同的图层字段到另一个图层中
时间: 2023-09-19 14:10:51 浏览: 132
您可以使用C# ArcObjects代码将一个图层的字段复制到另一个图层中。下面是一个简单的示例代码:
```csharp
//打开源图层和目标图层
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
IWorkspace workspace = workspaceFactory.OpenFromFile("C:\data.gdb", 0);
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
IFeatureClass sourceFeatureClass = featureWorkspace.OpenFeatureClass("sourceClassName");
IFeatureClass targetFeatureClass = featureWorkspace.OpenFeatureClass("targetClassName");
//获取源图层的字段集合
IFields sourceFields = sourceFeatureClass.Fields;
//遍历源图层的字段,并将其添加到目标图层中
for (int i = 0; i < sourceFields.FieldCount; i++)
{
IField sourceField = sourceFields.get_Field(i);
if (sourceField.Type != esriFieldType.esriFieldTypeGeometry && sourceField.Type != esriFieldType.esriFieldTypeOID)
{
IFieldEdit fieldEdit = new FieldClass();
fieldEdit.Name_2 = sourceField.Name;
fieldEdit.Type_2 = sourceField.Type;
fieldEdit.Length_2 = sourceField.Length;
fieldEdit.Scale_2 = sourceField.Scale;
fieldEdit.Precision_2 = sourceField.Precision;
targetFeatureClass.AddField(fieldEdit as IField);
}
}
```
在这个示例代码中,我们首先打开源图层和目标图层。然后,我们获取源图层的字段集合,并遍历它们。对于每个字段,我们检查它是否为几何类型或对象ID类型。如果不是,我们使用源字段的信息创建一个新的字段,并将其添加到目标图层中。这样,目标图层将具有与源图层相同的字段。
阅读全文