本文主要对数据导入等里 常用的不同格式数据创建、转换等 进行代码示例。主要用到IFeatureDataConverter.ConvertFeatureClass方法。
代码如下,难度不大,只是个技巧问题:
函数:
private void ConvertFeaCls(IWorkspace workspaceSource, IWorkspace workspaceTar, stringsSorceFeaClsName, string sTarFeaClsName, string sFeaDTName) { IFeatureClass feaCls = (workspaceSource asIFeatureWorkspace).OpenFeatureClass(sSorceFeaClsName); String shapeFieldName = feaCls.ShapeFieldName; int shapeFieldIndex = feaCls.FindField(shapeFieldName); IField shapeField = feaCls.Fields.get_Field(shapeFieldIndex); IGeometryDef geometryDef = shapeField.GeometryDef; //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)workspaceSource; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = sSorceFeaClsName; //create target workspace name IDataset tarWorkspaceDataset = (IDataset)workspaceTar; IWorkspaceName tarWorkspaceName = (IWorkspaceName)tarWorkspaceDataset.FullName; //create target dataset name IFeatureClassName tarFeatureClassName = new FeatureClassNameClass(); IDatasetName tarDatasetName = (IDatasetName)tarFeatureClassName; tarDatasetName.WorkspaceName = tarWorkspaceName; tarDatasetName.Name = sTarFeaClsName; IFeatureDatasetName tarFeatureDatasetName = new FeatureDatasetNameClass(); (tarFeatureDatasetName as IDatasetName).WorkspaceName = tarWorkspaceName; (tarFeatureDatasetName as IDatasetName).Name = sFeaDTName; if (string.IsNullOrEmpty(sFeaDTName)) { tarFeatureDatasetName = null; } IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, null, tarFeatureDatasetName, tarFeatureClassName, geometryDef, feaCls.Fields, "", , ); // Check for errors. }
调用:
IWorkspaceFactory2 workspaceFactory = new ShapefileWorkspaceFactoryClass() asIWorkspaceFactory2; IWorkspace workspace = workspaceFactory.OpenFromFile(@"E:\jhl", ); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(Functions.g_ROAD); IWorkspace workspace2 = workspaceFactory.OpenFromFile(@"E:\", ); DeleteShapeFileIFExist(@"E:\", "test6"); ConvertFeaCls(workspace, workspace2, Functions.g_ROAD, "test6",string.Empty);