Loading

Arcengine 转换方法

要素转要素

/// <summary>
/// 要素转要素
/// </summary>
/// <param name="sourceWorkspace">源要素的工作空间</param>
/// <param name="targetWorkspace">目标要素的工作空间</param>
/// <param name="nameOfSourceFeatureClass">源要素的名称</param>
/// <param name="nameOfTargetFeatureClass">目标要素的名称</param>
/// <param name="queryFilter">基于源数据的查询语句,可以设置为null</param>
/// <returns></returns>
public static bool IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,
                                                             string nameOfSourceFeatureClass, string nameOfTargetFeatureClass, IQueryFilter queryFilter)
{
    //创建一个源数据的工作空间的name  
    IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
    IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
    //创建源数据Name作为转换参数   
    IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
    IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
    sourceDatasetName.WorkspaceName = sourceWorkspaceName;
    //这个参数不能乱填(shp文件就是文件名没后缀,FeatureClass或者FeatureDataset就是AliasName)
    sourceDatasetName.Name = nameOfSourceFeatureClass;
    //创建目标(导出)数据空间的name 
    IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
    IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
    //创建目标数据Name作为转换参数   
    IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
    IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
    targetDatasetName.WorkspaceName = targetWorkspaceName;
    //这个命名也很重要,如果是mdb,那么这个命名在数据库中必须是本来不存在的(当然你可以先清空数据库)
    targetDatasetName.Name = nameOfTargetFeatureClass;
    //根据FetureClassName打开数据   
    IName sourceName = (IName)sourceFeatureClassName;
    IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();

    //在两个不同的工作空间转换数据要检查字段的有效性
    //一些检查有效性的参数
    IFieldChecker fieldChecker = new FieldCheckerClass();
    IFields targetFeatureClassFields;
    IFields sourceFeatureClassFields = sourceFeatureClass.Fields;
    IEnumFieldError enumFieldError;
    //设置检查有效性的源数据工作空间    
    fieldChecker.InputWorkspace = sourceWorkspace;
    fieldChecker.ValidateWorkspace = targetWorkspace;
    //(这个函数没有返回值我不知道它如何确定有效性滴)
    fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
    //通过输出字段循环找出几何字段
    IField geometryField;
    //遍历字段
    for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
    {
        //找到定义几何要素类型的字段
        if (targetFeatureClassFields.Field[i].Type != esriFieldType.esriFieldTypeGeometry) continue;
        //获取当前字段
        geometryField = targetFeatureClassFields.Field[i];
        //编辑几何要素类型字段
        IGeometryDef geometryDef = geometryField.GeometryDef;
        IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
        //说实话,下面两行我实在不知道用处。。。参数也是看文档抄袭的
        targetFCGeoDefEdit.GridCount_2 = 1;
        targetFCGeoDefEdit.set_GridSize(0, 0);
        //保证空间参考      SpatialReference_2属性字段才是可写的,SpatialReference属性只可读
        targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
        //开始转换
        IFeatureDataConverter myConvert = new FeatureDataConverterClass();
        myConvert.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
        return true;

    }
    return false;
}

要素集转要素集

/// <summary>
/// 要素集转要素集
/// </summary>
/// <param name="sourceWorkspace">源要素的工作空间</param>
/// <param name="targetWorkspace">目标要素的工作空间</param>
/// <param name="nameOfSourceFeatureDataset">源要素集的名称</param>
/// <param name="nameOfTargetFeatureDataset">目标要素集的名称</param>
public static void IFeatureDataConverter_ConvertFeatureDataset(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset)
{
    //create source workspace name
    IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
    IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;

    //create source dataset name
    IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass();
    IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName;
    sourceDatasetName.WorkspaceName = sourceWorkspaceName;
    sourceDatasetName.Name = nameOfSourceFeatureDataset;

    //create target workspace name
    IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
    IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;

    //create target dataset name
    IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass();
    IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName;
    targetDatasetName.WorkspaceName = targetWorkspaceName;
    targetDatasetName.Name = nameOfTargetFeatureDataset;

    //Convert feature dataset
    IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
    featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0);
}
public static void GdbTransfer(IWorkspaceName sourceWorkspaceName, IWorkspaceName targetWorkspaceName, string sourceDatasetName)
{
    //// Create workspace name objects.
    //IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass();
    //IWorkspaceName targetWorkspaceName = new WorkspaceNameClass();
    IName targetName = (IName) targetWorkspaceName;

    //// Set the workspace name properties.
    //sourceWorkspaceName.PathName = @"C:\arcgis\ArcTutor\BuildingaGeodatabase\Montgomery.gdb";
    //sourceWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
    //targetWorkspaceName.PathName = @"PartialMontgomery.gdb";
    //targetWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";

    // Create a name object for the source feature class.
    IFeatureClassName featureClassName = new FeatureClassNameClass();

    // Set the featureClassName properties.
    IDatasetName pSourceDatasetName = (IDatasetName) featureClassName;
    pSourceDatasetName.WorkspaceName = sourceWorkspaceName;
    pSourceDatasetName.Name = sourceDatasetName;
    IName sourceName = (IName)pSourceDatasetName;

    // Create an enumerator for source datasets.
    IEnumName sourceEnumName = new NamesEnumeratorClass();
    IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit) sourceEnumName;

    // Add the name object for the source class to the enumerator.
    sourceEnumNameEdit.Add(sourceName);

    // Create a GeoDBDataTransfer object and a null name mapping enumerator.
    IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
    IEnumNameMapping enumNameMapping;

    // Use the data transfer object to create a name mapping enumerator.
    Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName, targetName, out enumNameMapping);
    enumNameMapping.Reset();

    // Check for conflicts.
    if (conflictsFound)
    {
        // Iterate through each name mapping.
        INameMapping nameMapping = null;
        while ((nameMapping = enumNameMapping.Next()) != null)
        {
            // Resolve the mapping's conflict (if there is one).
            if (nameMapping.NameConflicts)
                nameMapping.TargetName = nameMapping.GetSuggestedName(targetName);

            // See if the mapping's children have conflicts.
            IEnumNameMapping childEnumNameMapping = nameMapping.Children;
            if (childEnumNameMapping == null) continue;
            childEnumNameMapping.Reset();

            // Iterate through each child mapping.
            INameMapping childNameMapping = null;
            while ((childNameMapping = childEnumNameMapping.Next()) != null)
            {
                if (childNameMapping.NameConflicts)
                    childNameMapping.TargetName = childNameMapping.GetSuggestedName(targetName);
            }
        }
    }

    // Start the transfer.
    geoDBDataTransfer.Transfer(enumNameMapping, targetName);
}

posted @ 2019-08-08 16:04  F(x)_King  阅读(421)  评论(0编辑  收藏  举报