ArcGIS Pro SDK (八)地理数据库 6 版本控制

ArcGIS Pro SDK (八)地理数据库 6 版本控制

环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0

1 连接到版本

public Geodatabase ConnectToVersion(Geodatabase geodatabase, string versionName)
{
    Geodatabase connectedVersion = null;

    if (geodatabase.IsVersioningSupported())
    {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
            using (Version version = versionManager.GetVersion(versionName))
        {
            connectedVersion = version.Connect();
        }
    }
    return connectedVersion;
}

2 在单独的编辑会话中协调版本并将其与其父级进行核对和提交

public void ReconcileAndPost(Geodatabase geodatabase)
{
    // 获取当前版本及其父版本的引用
    if (geodatabase.IsVersioningSupported())
    {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        using (Version currentVersion = versionManager.GetCurrentVersion())
        using (Version parentVersion = currentVersion.GetParent())
        {
            // 创建 ReconcileOptions 对象
            var reconcileOptions = new ReconcileOptions(parentVersion)
            {
                ConflictResolutionMethod = ConflictResolutionMethod.Continue, // 发现冲突时继续
                ConflictDetectionType = ConflictDetectionType.ByRow, // 默认值
                ConflictResolutionType = ConflictResolutionType.FavorTargetVersion // 或者 FavorEditVersion
            };

            // 执行协调
            ReconcileResult reconcileResult = currentVersion.Reconcile(reconcileOptions);
            if (!reconcileResult.HasConflicts)
            {
                // 无冲突,执行过账
                var postOptions = new PostOptions(parentVersion)
                {
                    ServiceSynchronizationType = ServiceSynchronizationType.Synchronous // 默认值
                };
                currentVersion.Post(postOptions);
            }
        }
    }
}

3 在同一编辑会话中协调版本并将其与其父级提交

public void ReconcileAndPost2(Geodatabase geodatabase)
{
    // 获取当前版本及其父版本的引用
    if (geodatabase.IsVersioningSupported())
    {
        using (VersionManager versionManager = geodatabase.GetVersionManager())
        using (Version currentVersion = versionManager.GetCurrentVersion())
        using (Version parentVersion = currentVersion.GetParent())
        {
            // 创建 ReconcileOptions 对象
            var reconcileOptions = new ReconcileOptions(parentVersion)
            {
                ConflictResolutionMethod = ConflictResolutionMethod.Continue, // 发现冲突时继续
                ConflictDetectionType = ConflictDetectionType.ByRow, // 默认值
                ConflictResolutionType = ConflictResolutionType.FavorTargetVersion // 或者 FavorEditVersion
            };

            // 创建 PostOptions 对象
            var postOptions = new PostOptions(parentVersion)
            {
                ServiceSynchronizationType = ServiceSynchronizationType.Synchronous // 默认值
            };

            // 执行协调和过账
            ReconcileResult reconcileResult = currentVersion.Reconcile(reconcileOptions, postOptions);
            if (reconcileResult.HasConflicts)
            {
                // 处理冲突
                // TODO 解决冲突
            }
        }
    }
}

4 使用版本

public async Task WorkingWithVersions()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
                using (VersionManager versionManager = geodatabase.GetVersionManager())
            {
                IReadOnlyList<Version> versionList = versionManager.GetVersions();

                // 默认版本的父版本为 null
                Version defaultVersion = versionList.First(version => version.GetParent() == null);

                IEnumerable<Version> publicVersions = versionList.Where(
                    version => version.GetAccessType() == VersionAccessType.Public);
                Version qaVersion = defaultVersion.GetChildren().First(
                    version => version.GetName().Contains("QA"));

                Geodatabase qaVersionGeodatabase = qaVersion.Connect();

                FeatureClass currentFeatureClass = geodatabase.OpenDataset<FeatureClass>("featureClassName");
                FeatureClass qaFeatureClass = qaVersionGeodatabase.OpenDataset<FeatureClass>("featureClassName");
            }
        });
}

5 使用默认版本

// 检查当前版本是否为默认版本
// 适用于分支和传统版本管理
public bool IsDefaultVersion(Version version)
{
    Version parentVersion = version.GetParent();
    if (parentVersion == null)
    {
        return true;
    }
    parentVersion.Dispose();
    return false;
}

public bool IsDefaultVersion(Geodatabase geodatabase)
{
    if (!geodatabase.IsVersioningSupported()) return false;
    using (VersionManager versionManager = geodatabase.GetVersionManager())
        using (Version currentVersion = versionManager.GetCurrentVersion())
    {
        return IsDefaultVersion(currentVersion);
    }
}

// 获取默认版本
// 适用于分支和传统版本管理
// 此方法依赖于 IsDefaultVersion() 方法
public Version GetDefaultVersion(Version version)
{
    if (IsDefaultVersion(version))
    {
        return version;
    }
    else
    {
        Version parent = version.GetParent();
        Version ancestor = GetDefaultVersion(parent);
        if (parent != ancestor)
        {
            parent.Dispose(); // 如果版本树超过两层深,释放中间版本对象
        }
        return ancestor;
    }
}

public Version GetDefaultVersion(Geodatabase geodatabase)
{
    if (!geodatabase.IsVersioningSupported()) return null;

    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        Version currentVersion = versionManager.GetCurrentVersion();
        Version defaultVersion = GetDefaultVersion(currentVersion);
        if (currentVersion != defaultVersion)
        {
            currentVersion.Dispose(); // 如果当前版本不是默认版本,释放当前版本对象
        }
        return defaultVersion;
    }
}

6 创建版本

public Version CreateVersion(Geodatabase geodatabase, string versionName, string description, VersionAccessType versionAccessType)
{
    if (!geodatabase.IsVersioningSupported()) return null;

    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        VersionDescription versionDescription = new VersionDescription(versionName, description, versionAccessType);
        return versionManager.CreateVersion(versionDescription);
    }
}

7 创建历史版本

public HistoricalVersion CreateHistoricalVersion(Geodatabase geodatabase, string versionName)
{
    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        HistoricalVersionDescription historicalVersionDescription = new HistoricalVersionDescription(versionName, DateTime.Now);
        HistoricalVersion historicalVersion = versionManager.CreateHistoricalVersion(historicalVersionDescription);

        return historicalVersion;
    }
}

8 在版本之间切换

public void ChangeVersions(Geodatabase geodatabase, string toVersionName)
{
    using (VersionManager versionManager = geodatabase.GetVersionManager())
    {
        VersionBaseType versionBaseType = versionManager.GetCurrentVersionBaseType();

        if (versionBaseType == VersionBaseType.Version)
        {
            Version fromVersion = versionManager.GetCurrentVersion();
            Version toVersion = versionManager.GetVersion(toVersionName);

            // 在版本之间切换
            MapView.Active.Map.ChangeVersion(fromVersion, toVersion);
        }

        if (versionBaseType == VersionBaseType.HistoricalVersion)
        {
            HistoricalVersion fromHistoricalVersion = versionManager.GetCurrentHistoricalVersion();
            HistoricalVersion toHistoricalVersion = versionManager.GetHistoricalVersion(toVersionName);

            // 在历史版本之间切换
            MapView.Active.Map.ChangeVersion(fromHistoricalVersion, toHistoricalVersion);
        }

        // 从历史版本切换到版本,反之亦然
        // MapView.Active.Map.ChangeVersion(fromHistoricalVersion, toVersion);
        // MapView.Active.Map.ChangeVersion(fromVersion, toHistoricalVersion);
    }
}

9 部分过账

// 部分过账允许开发人员提交版本中的部分更改。
// 一个示例用例是电力公用事业公司使用版本来设计新的住宅细分区的设施。
// 在流程的某个阶段,一个街区的新房屋已经建成,而细分区的其余部分尚未建造。
// 部分过账允许用户提交已完成的工作,同时保留版本中尚未建成的要素,以便以后提交。
// 部分过账需要使用 ArcGIS Enterprise 10.9 及更高版本的分支版本化要素服务。

// 指定已构建的要素集合
QueryFilter constructedFilter = new QueryFilter()
{
    WhereClause = "ConstructedStatus = 'True'"
};

// 此选择表示我们希望提交到支持结构要素类的插入和更新
using (Selection constructedSupportStructures = supportStructureFeatureClass.Select(
    constructedFilter, SelectionType.ObjectID, SelectionOption.Normal))
{
    // 指定要提交的删除要素稍微复杂,因为无法发出查询来获取已删除要素的集合,必须使用 ObjectIDs 的列表。
    using (Selection deletedSupportStructures = supportStructureFeatureClass.Select(
        null, SelectionType.ObjectID, SelectionOption.Empty))
    {
        deletedSupportStructures.Add(deletedSupportStructureObjectIDs);  // deletedSupportStructureObjectIDs 定义为 List<long>

        // 执行带有部分过账的协调
        // 在 2.x 版本中 -
        // ReconcileDescription reconcileDescription = new ReconcileDescription();
        // reconcileDescription.ConflictDetectionType = ConflictDetectionType.ByColumn;
        // reconcileDescription.ConflictResolutionMethod = ConflictResolutionMethod.Continue;
        // reconcileDescription.ConflictResolutionType = ConflictResolutionType.FavorEditVersion;
        // reconcileDescription.PartialPostSelections = new List<Selection>() { constructedSupportStructures, deletedSupportStructures };
        // reconcileDescription.WithPost = true;

        // ReconcileResult reconcileResult = designVersion.Reconcile(reconcileDescription);

        var reconcileOptions = new ReconcileOptions();  // 协调选项,针对默认版本
        reconcileOptions.ConflictDetectionType = ConflictDetectionType.ByColumn;
        reconcileOptions.ConflictResolutionMethod = ConflictResolutionMethod.Continue;
        reconcileOptions.ConflictResolutionType = ConflictResolutionType.FavorEditVersion;

        var postOptions = new PostOptions();  // 提交选项,针对默认版本
        postOptions.PartialPostSelections = new List<Selection>() { 
            constructedSupportStructures, deletedSupportStructures 
        };
        postOptions.ServiceSynchronizationType = ServiceSynchronizationType.Synchronous;

        var reconcileResult = designVersion.Reconcile(reconcileOptions, postOptions);

        // 处理结果
        // TODO 处理结果的逻辑
    }
}

相关推荐

  1. ArcGIS Pro SDK (地理数据库 6 版本控制

    2024-07-12 19:36:03       21 阅读
  2. Spring Boot(七十):实现API 多版本控制

    2024-07-12 19:36:03       24 阅读
  3. ArcGIS Pro SDK (地理数据库 3 数据

    2024-07-12 19:36:03       20 阅读
  4. ArcGIS Pro SDK (地理数据库 2 定义

    2024-07-12 19:36:03       20 阅读
  5. ArcGIS Pro SDK (地理数据库 5 编辑

    2024-07-12 19:36:03       19 阅读
  6. ArcGIS Pro SDK (地理数据库 4 查询

    2024-07-12 19:36:03       15 阅读
  7. ArcGIS Pro SDK (地理数据库 8 拓扑

    2024-07-12 19:36:03       18 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 19:36:03       50 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 19:36:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 19:36:03       43 阅读
  4. Python语言-面向对象

    2024-07-12 19:36:03       54 阅读

热门阅读

  1. SpringBoot使用手册

    2024-07-12 19:36:03       17 阅读
  2. ROS2-humble学习

    2024-07-12 19:36:03       19 阅读
  3. 代码随想录打卡第二十一天

    2024-07-12 19:36:03       20 阅读
  4. Unity 之 抖音小游戏集成排行榜功能详解

    2024-07-12 19:36:03       17 阅读
  5. adb参数详解

    2024-07-12 19:36:03       19 阅读
  6. Git常用命令

    2024-07-12 19:36:03       22 阅读
  7. 力扣2211.统计道路上的碰撞次数

    2024-07-12 19:36:03       21 阅读
  8. js【详解】声明常量和变量 const let var

    2024-07-12 19:36:03       23 阅读
  9. CVE漏洞爬虫脚本

    2024-07-12 19:36:03       15 阅读
  10. 掌握Postman的API请求模板:一键生成,效率倍增

    2024-07-12 19:36:03       19 阅读
  11. Vue中的Mixins与钩子函数:理解合并与调用

    2024-07-12 19:36:03       19 阅读
  12. Todo List

    2024-07-12 19:36:03       19 阅读
  13. ARM/Linux嵌入式面经(十):极氪

    2024-07-12 19:36:03       16 阅读
  14. 【网络安全】一文带你了解什么是【sql注入】

    2024-07-12 19:36:03       17 阅读