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 处理结果的逻辑
}
}
标签:版本控制,Pro,versionManager,ArcGIS,Version,版本,new,using,geodatabase
From: https://blog.csdn.net/szy13323042191/article/details/140348150