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

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

文章目录

  • [ArcGIS Pro SDK (八)地理数据库 6 版本控制](#ArcGIS Pro SDK (八)地理数据库 6 版本控制)
    • [1 连接到版本](#1 连接到版本)
    • [2 在单独的编辑会话中协调版本并将其与其父级进行核对和提交](#2 在单独的编辑会话中协调版本并将其与其父级进行核对和提交)
    • [3 在同一编辑会话中协调版本并将其与其父级提交](#3 在同一编辑会话中协调版本并将其与其父级提交)
    • [4 使用版本](#4 使用版本)
    • [5 使用默认版本](#5 使用默认版本)
    • [6 创建版本](#6 创建版本)
    • [7 创建历史版本](#7 创建历史版本)
    • [8 在版本之间切换](#8 在版本之间切换)
    • [9 部分过账](#9 部分过账)

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

1 连接到版本

csharp 复制代码
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 在单独的编辑会话中协调版本并将其与其父级进行核对和提交

csharp 复制代码
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 在同一编辑会话中协调版本并将其与其父级提交

csharp 复制代码
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 使用版本

csharp 复制代码
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 使用默认版本

csharp 复制代码
// 检查当前版本是否为默认版本
// 适用于分支和传统版本管理
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 创建版本

csharp 复制代码
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 创建历史版本

csharp 复制代码
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 在版本之间切换

csharp 复制代码
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 部分过账

csharp 复制代码
// 部分过账允许开发人员提交版本中的部分更改。
// 一个示例用例是电力公用事业公司使用版本来设计新的住宅细分区的设施。
// 在流程的某个阶段,一个街区的新房屋已经建成,而细分区的其余部分尚未建造。
// 部分过账允许用户提交已完成的工作,同时保留版本中尚未建成的要素,以便以后提交。
// 部分过账需要使用 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 处理结果的逻辑
    }
}
相关推荐
asdzx6719 小时前
使用 Python 精准操控 Word 字体:获取与替换方案
python·c#·word
疯狂学习GIS20 小时前
AI Agent 操作 QGIS 实测:Codex 与 Claude Code 能自己出图吗
人工智能·ai·大模型·gis·agent·qgis·codex
逝水无殇21 小时前
C# 特性详解
开发语言·后端·c#
LongtengGensSupreme1 天前
C#图像内存高速拷贝:使用Marshal.AllocHGlobal与ArrayPool实现内存拷贝的几个方法
开发语言·数码相机·c#
影寂ldy1 天前
C# 五大加密算法全套实战(AES/DES对称、RSA非对称、MD5/SHA256哈希)
开发语言·c#·哈希算法
互联网底层民工1 天前
异步 / 多线程并发(高频八股 · 详解版)
c#
逝水无殇1 天前
C# 反射详解
开发语言·后端·c#
神秘的MT1 天前
SQL Server 分页查询 ROW_NUMBER () OFFSET...FETCH游标(Cursor) 数据插入
数据库·sql·学习·c#·winform
我才是银古1 天前
一个人写出的 3D 几何引擎:Ara3D SDK 技术深度剖析
c#·三维几何·ai平台