C#Arcengine不使用gp服务,从一个GDB数据库中复制一个图层,然后粘贴到另一个GDB数据库
时间: 2024-03-12 22:46:49 浏览: 56
您可以使用C# ArcObjects中的以下代码从一个GDB数据库中复制一个图层并粘贴到另一个GDB数据库。假设您已经创建了两个工作空间`sourceWorkspace`和`targetWorkspace`,并且已经打开了源图层`sourceFeatureLayer`。
```
// 获取源图层的描述符
IDataset sourceDataset = (IDataset)sourceFeatureLayer;
IObjectClassDescription sourceDesc = new FeatureClassDescriptionClass();
IFields sourceFields = sourceFeatureLayer.FeatureClass.Fields;
IFieldChecker sourceFieldChecker = new FieldCheckerClass();
IEnumFieldError sourceEnumFieldError;
IFields validatedFields;
sourceFieldChecker.ValidateFields(out sourceFields, out sourceEnumFieldError, out validatedFields);
// 创建目标图层
IName sourceName = sourceDataset.FullName;
IDatasetName sourceDatasetName = (IDatasetName)sourceName;
IWorkspace targetWorkspace = targetWorkspace;
IWorkspaceName targetWorkspaceName = (IWorkspaceName)((IDataset)targetWorkspace).FullName;
IName targetName = targetWorkspaceName.CreateName("NewFeatureClassName", null, null);
IObjectClassDescription targetDesc = new FeatureClassDescriptionClass();
IFeatureClass targetFeatureClass = (IFeatureClass)targetDesc.CreateInstance(targetName, validatedFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
// 复制源图层到目标图层
IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
IEnumInvalidObject enumErrors = null;
featureDataConverter.ConvertFeatureClass(sourceDatasetName, null, null, targetName, null, validatedFields, "", 100, 0, true, enumErrors);
// 打开目标图层
IFeatureLayer targetFeatureLayer = new FeatureLayerClass();
targetFeatureLayer.FeatureClass = targetFeatureClass;
targetFeatureLayer.Name = "New Feature Layer";
// 显示目标图层
IMxDocument mxDocument = (IMxDocument)ArcMap.Application.Document;
IMap map = mxDocument.FocusMap;
map.AddLayer(targetFeatureLayer);
mxDocument.UpdateContents();
mxDocument.ActiveView.Refresh();
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)