自学内容网 自学内容网

ArcGIS Pro SDK (八)地理数据库 2 定义

ArcGIS Pro SDK (八)地理数据库 2 定义

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

1 从地理数据库获取定义

public async Task ObtainingDefinitionFromGeodatabase()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
            {
                // 对于企业级数据库,必须使用数据库名和用户名限定数据集名称。
                TableDefinition enterpriseTableDefinition = geodatabase.GetDefinition<TableDefinition>("LocalGovernment.GDB.CitizenContactInfo");

                // 无论数据集是否在 FeatureDataset 中,都没有关系。
                FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>("LocalGovernment.GDB.FireStation");

                // 获取 RelationshipClass 的定义。
                RelationshipClassDefinition relationshipClassDefinition = geodatabase.GetDefinition<RelationshipClassDefinition>("LocalGovernment.GDB.AddressPointHasSiteAddresses");

                // 获取 FeatureDataset 的定义。
                FeatureDatasetDefinition featureDatasetDefinition = geodatabase.GetDefinition<FeatureDatasetDefinition>("LocalGovernment.GDB.Address");
            }
        });
}

2 从地理数据库获取定义列表

public async Task ObtainingDefinitionsFromGeodatabase()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(
                new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
            {
                var enterpriseDefinitions = 
                    geodatabase.GetDefinitions<FeatureClassDefinition>();
                IEnumerable<Definition> featureClassesHavingGlobalID = 
                    enterpriseDefinitions.Where(definition => definition.HasGlobalID());

                var featureDatasetDefinitions = 
                    geodatabase.GetDefinitions<FeatureDatasetDefinition>();
                bool electionRelatedFeatureDatasets = 
                    featureDatasetDefinitions.Any(
                    definition => definition.GetName().Contains("Election"));

                var attributedRelationshipClassDefinitions = 
                    geodatabase.GetDefinitions<AttributedRelationshipClassDefinition>();

                var relationshipClassDefinitions = 
                    geodatabase.GetDefinitions<RelationshipClassDefinition>();
            }
        });
}

3 从地理数据库获取相关定义

public async Task ObtainingRelatedDefinitionsFromGeodatabase()
{
    await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(
        () =>
        {
            using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
            {
                // 对于 RelationshipClass,必须使用数据库名限定名称。
                RelationshipClassDefinition enterpriseDefinition = 
                    geodatabase.GetDefinition<RelationshipClassDefinition>("LocalGovernment.GDB.AddressPointHasSiteAddresses");
                IReadOnlyList<Definition> enterpriseDefinitions = 
                    geodatabase.GetRelatedDefinitions(enterpriseDefinition, DefinitionRelationshipType.DatasetsRelatedThrough);
                FeatureClassDefinition enterpriseAddressPointDefinition = 
                    enterpriseDefinitions.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressPoint")) as FeatureClassDefinition;

                FeatureDatasetDefinition featureDatasetDefinition = 
                    geodatabase.GetDefinition<FeatureDatasetDefinition>("LocalGovernment.GDB.Address");
                IReadOnlyList<Definition> datasetsInAddressDataset = 
                    geodatabase.GetRelatedDefinitions(featureDatasetDefinition, DefinitionRelationshipType.DatasetInFeatureDataset);
                FeatureClassDefinition addressPointInAddressDataset = 
                    datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressPoint")) as FeatureClassDefinition;

                RelationshipClassDefinition addressPointHasSiteAddressInAddressDataset = datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressPointHasSiteAddresses")) as RelationshipClassDefinition;
            }
        });
}

4 从层获取表定义

// GetDefinitionFromLayer - 这段代码即使在图层连接了另一个表的情况下也能正常工作
private TableDefinition GetDefinitionFromLayer(FeatureLayer featureLayer)
{
    // 从图层获取要素类
    FeatureClass featureClass = featureLayer.GetFeatureClass();

    // 确定要素类是否连接了另一个表
    if (featureClass.IsJoinedTable())
    {
        // 从要素类获取连接
        Join join = featureClass.GetJoin();

        // 从连接中获取源表
        Table originTable = join.GetOriginTable();

        // 从连接的源表返回要素类定义
        return originTable.GetDefinition();
    }
    else
    {
        return featureClass.GetDefinition();
    }
}

原文地址:https://blog.csdn.net/szy13323042191/article/details/140297647

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!