/// <summary>
 /// Given that the RelationshipClass is selected, this method will
 /// 1. Get the Definitions of Datasets related by the selected relationship class
 /// 2. Set the RelatedFeatureClass property to the Table/FeatureClass which is related to the FeatureClass corresponding to the selected layer
 /// </summary>
 public void UpdateRelatedFeatureClass()
 {
     QueuedTask.Run(() =>
     {
         using (Table table = (MapView.Active.Map.Layers.First(layer => layer.Name.Equals(SelectedLayer)) as FeatureLayer).GetTable())
         {
             Geodatabase geodatabase = null;
             if (table != null && table.GetDatastore() is Geodatabase)
             {
                 geodatabase = table.GetDatastore() as Geodatabase;
             }
             if (geodatabase == null)
             {
                 return;
             }
             if (SelectedRelationship == null)
             {
                 return;
             }
             IReadOnlyList <Definition> relatedDefinitions = geodatabase.GetRelatedDefinitions(geodatabase.GetDefinition <RelationshipClassDefinition>(SelectedRelationship),
                                                                                               DefinitionRelationshipType.DatasetsRelatedThrough);
             relatedFeatureClassDefinition = relatedDefinitions.First(definition => definition.GetName() != table.GetDefinition().GetName());
             RelatedFeatureClass           = relatedFeatureClassDefinition.GetName();
         }
     });
 }
Exemplo n.º 2
0
 public async Task GetFeatureClassesInRelationshipClassAsync()
 {
     await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
     {
         using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
         {
             IReadOnlyList <RelationshipClassDefinition> relationshipClassDefinitions = geodatabase.GetDefinitions <RelationshipClassDefinition>();
             foreach (var relationshipClassDefintion in relationshipClassDefinitions)
             {
                 IReadOnlyList <Definition> definitions = geodatabase.GetRelatedDefinitions(relationshipClassDefintion,
                                                                                            DefinitionRelationshipType.DatasetsRelatedThrough);
                 foreach (var definition in definitions)
                 {
                     MessageBox.Show($"Feature class in the RelationshipClass is:{definition.GetName()}");
                 }
             }
         }
     });
 }
Exemplo n.º 3
0
 /// <summary>
 /// Gets all dataset feature class definitions. This method must be called on the MCT or within a method that will be called on the MCT. Use QueuedTask.Run.
 /// </summary>
 /// <returns>
 /// All dataset feature class definitions.
 /// </returns>
 private static IReadOnlyList <FeatureClassDefinition> GetDatasetFeatureClassDefinitions()
 {
     using (var geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(Project.Current.DefaultGeodatabasePath))))
         using (var featureDatasetDefinition = geodatabase.GetDefinition <FeatureDatasetDefinition>("FeatureDataset"))
             return(geodatabase.GetRelatedDefinitions(featureDatasetDefinition, DefinitionRelationshipType.DatasetInFeatureDataset).OfType <FeatureClassDefinition>().ToList());
 }
    public void MainMethodCode()
    {
      using (Geodatabase fileGeodatabase         = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
      using (FeatureDataset featureDataset       = fileGeodatabase.OpenDataset<FeatureDataset>("Address"))
      using (FeatureClass featureClass           = featureDataset.OpenDataset<FeatureClass>("AddressPoint"))
      using (RelationshipClass relationshipClass = featureDataset.OpenDataset<RelationshipClass>("AddressPointHasSiteAddresses"))
      {
        IReadOnlyList<Definition> definitions = fileGeodatabase.GetRelatedDefinitions(featureDataset.GetDefinition(), DefinitionRelationshipType.DatasetInFeatureDataset);

        foreach (Definition definition in definitions)
        {
          Dataset dataset = null;

          try
          {
            if (definition.DatasetType == DatasetType.Table)
            {
              dataset = featureDataset.OpenDataset<Table>(definition.GetName());
            }
            else if (definition.DatasetType == DatasetType.FeatureClass)
            {
              dataset = featureDataset.OpenDataset<FeatureClass>(definition.GetName());
            }
            else if (definition.DatasetType == DatasetType.RelationshipClass)
            {
              dataset = featureDataset.OpenDataset<RelationshipClass>(definition.GetName());
            }
            else if (definition.DatasetType == DatasetType.AttributedRelationshipClass)
            {
              dataset = featureDataset.OpenDataset<AttributedRelationshipClass>(definition.GetName());
            }
              
            // Do something with dataset
          }
          catch (InvalidOperationException exception)
          {
            //This exception is thrown if one of the datasets in the FeatureDatasets are not supported and thus marked as Unknown
            Console.WriteLine(exception);
          }
          finally
          {
            // Should explicitly dispose of a Dataset.

            if (dataset != null)
              dataset.Dispose();
          }
        }
      }
      
      // Opening a Non-Versioned SQL Server instance.

      DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
      {
        AuthenticationMode = AuthenticationMode.DBMS,
 
        // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
        Instance = @"testMachine\testInstance",

        // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
        Database = "LocalGovernment",

        // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
        User     = "******",
        Password = "******",
        Version  = "dbo.DEFAULT"
      };
      
      using (Geodatabase geodatabase                 = new Geodatabase(connectionProperties))
      using (FeatureDataset enterpriseFeatureDataset = geodatabase.OpenDataset<FeatureDataset>("LocalGovernment.GDB.ReferenceData"))
      using (FeatureClass communityCenter            = enterpriseFeatureDataset.OpenDataset<FeatureClass>("LocalGovernment.GDB.CommunityCenter"))
      using (FeatureClass library                    = enterpriseFeatureDataset.OpenDataset<FeatureClass>("LocalGovernment.GDB.Library"))
      {
        try
        {
          // Trying to open non-existent RelationshipClasses and AttributedRelationshipClasses.
          Dataset badRelationshipClass           = enterpriseFeatureDataset.OpenDataset<RelationshipClass>("SomeGibberish");
          Dataset badAttributedRelationshipClass = enterpriseFeatureDataset.OpenDataset<AttributedRelationshipClass>("SomeGibberish");
        }
        catch (GeodatabaseCatalogDatasetException exception)
        {
          // Thrown for RelationshipClass and AttributedRelationshipClass.
        }

        try
        {
          // Trying to open non-existent FeatureClasses.
          Dataset badFeatureClass = enterpriseFeatureDataset.OpenDataset<FeatureClass>("SomeGibberish");
        }
        catch (GeodatabaseDatasetException exception)
        {
          // Thrown for FeatureClasses.
        }

        try
        {
          // Tables and FeatureDatasets cannot exist in a FeatureDataset.
          Dataset badFeatureDataset = enterpriseFeatureDataset.OpenDataset<FeatureDataset>("SomeGibberish");
          Dataset badTable          = enterpriseFeatureDataset.OpenDataset<Table>("SomeGibberish");
        }
        catch (InvalidOperationException exception)
        {
          // Thrown for Table and FeatureDataset.
        }
      }
    }
        public void MainMethodCode()
        {
            // Opens a file geodatabase  This will open the geodatabase if the folder exists and contains a valid geodatabase.

            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
            {
                RelationshipClassDefinition definition  = fileGeodatabase.GetDefinition <RelationshipClassDefinition>("AddressPointHasSiteAddresses");
                IReadOnlyList <Definition>  definitions = fileGeodatabase.GetRelatedDefinitions(definition, DefinitionRelationshipType.DatasetsRelatedThrough);

                // This will not be null.
                FeatureClassDefinition addressPointDefinition = definitions.First(defn => defn.GetName().Equals("AddressPoint")) as FeatureClassDefinition;

                // This will not be null.
                FeatureClassDefinition siteAddressPointDefinition = definitions.First(defn => defn.GetName().Equals("SiteAddressPoint")) as FeatureClassDefinition;
            }

            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
            {
                // Remember the qualification of DatabaseName. for the RelationshipClass.
                RelationshipClassDefinition enterpriseDefinition  = geodatabase.GetDefinition <RelationshipClassDefinition>("LocalGovernment.GDB.AddressPointHasSiteAddresses");
                IReadOnlyList <Definition>  enterpriseDefinitions = geodatabase.GetRelatedDefinitions(enterpriseDefinition, DefinitionRelationshipType.DatasetsRelatedThrough);

                // This will not be null.
                FeatureClassDefinition enterpriseAddressPointDefinition = enterpriseDefinitions.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressPoint")) as FeatureClassDefinition;

                // This will not be null.
                FeatureClassDefinition enterpriseSiteAddressPointDefinition = enterpriseDefinitions.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.SiteAddressPoint")) as FeatureClassDefinition;

                FeatureDatasetDefinition   featureDatasetDefinition = geodatabase.GetDefinition <FeatureDatasetDefinition>("LocalGovernment.GDB.Address");
                IReadOnlyList <Definition> datasetsInAddressDataset = geodatabase.GetRelatedDefinitions(featureDatasetDefinition, DefinitionRelationshipType.DatasetInFeatureDataset);

                // This will not be null.
                FeatureClassDefinition addressPointInAddressDataset = datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressPoint")) as FeatureClassDefinition;

                // This will not be null.
                FeatureClassDefinition siteAddressPointInAddressDataset = datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.SiteAddressPoint")) as FeatureClassDefinition;

                // This will not be null.
                FeatureClassDefinition addressEntryPointInAddressDataset = datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressEntrancePoint")) as FeatureClassDefinition;

                // This will not be null.
                RelationshipClassDefinition addressPointHasSiteAddressInAddressDataset = datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.AddressPointHasSiteAddresses")) as RelationshipClassDefinition;

                // This will not be null.
                RelationshipClassDefinition siteAddressHasPostalAddressInAddressDataset = datasetsInAddressDataset.First(
                    defn => defn.GetName().Equals("LocalGovernment.GDB.SiteAddresshasPostalAddresses")) as RelationshipClassDefinition;

                FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition <FeatureClassDefinition>("LocalGovernment.GDB.AddressPoint");
                // This will be empty.
                IReadOnlyList <Definition> emptyDefinitionList = geodatabase.GetRelatedDefinitions(featureClassDefinition, DefinitionRelationshipType.DatasetInFeatureDataset);

                TableDefinition tableClassDefinition = geodatabase.GetDefinition <TableDefinition>("LocalGovernment.GDB.EmployeeInfo");
                // This will be empty.
                IReadOnlyList <Definition> anotherEmptyDefinitionList = geodatabase.GetRelatedDefinitions(tableClassDefinition, DefinitionRelationshipType.DatasetsRelatedThrough);
            }
        }