Features in a File Geodatabase Search. Pro SDK

2594
7
Jump to solution
01-18-2019 12:43 PM
MatthewDriscoll
MVP Alum

Is there a way to see if a feature class exists in a file geodatabase OR loop through and get a list of all the feature class in a file geodatabase using the Pro SDK?

0 Kudos
1 Solution

Accepted Solutions
RichRuh
Esri Regular Contributor

Matthew,

I assume you are asking about feature classes inside a file geodatabase (a feature layer is a map layer that obtains its data from a feature class).  The best way to check for feature class existence is the following:

public bool FeatureClassExists(Geodatabase geodatabase, string featureClassName)
{
  try
  {
    FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition(featureClassName);
    featureClassDefinition.Dispose();
    return true;
  }
  catch
  {
    // GetDefinition throws an exception if the definition doesn't exist
    return false;
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

To get a list of all of the feature classes in a geodatabase, use Geodatabase.GetDefinitions<FeatureClass>() and iterate through the returned list.

I hope this helps,

--Rich

View solution in original post

7 Replies
RichRuh
Esri Regular Contributor

Matthew,

I assume you are asking about feature classes inside a file geodatabase (a feature layer is a map layer that obtains its data from a feature class).  The best way to check for feature class existence is the following:

public bool FeatureClassExists(Geodatabase geodatabase, string featureClassName)
{
  try
  {
    FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition(featureClassName);
    featureClassDefinition.Dispose();
    return true;
  }
  catch
  {
    // GetDefinition throws an exception if the definition doesn't exist
    return false;
  }
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

To get a list of all of the feature classes in a geodatabase, use Geodatabase.GetDefinitions<FeatureClass>() and iterate through the returned list.

I hope this helps,

--Rich

MatthewDriscoll
MVP Alum

Thanks Rich!

I did mean feature class, I corrected the question.  When I tried your suggestion I get the error "Non-invocable member ;FeatureClassDefinition' cannot be used like a method.  Below is what I ended up trying, it runs without error, but it still does not see the feature class when it does exist.

string projGDBPath = Project.Current.DefaultGeodatabasePath;

string FC = TXB.Text; // From a text box in the xaml
            
string fcFINAL = System.IO.Path.Combine(projGDBPath, FC);
            

await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => {
                using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(projGDBPath))))
                {
                    try
                    {
                        FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(fcFINAL);
                        featureClassDefinition.Dispose();
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("It exists");
                    }

                    catch
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("It Does Not Exist");
                    }
                }
            });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RichRuh
Esri Regular Contributor

Ah, looks like I forgot the closing > in my code snippet.

I would suggest you get all of the feature class definitions inside the file geodatabase and make sure the list matches your expectations.  To do this call FeatureClass.GetDefinitions<FeatureClassDefinition>() and iterate through the returned list.

0 Kudos
MatthewDriscoll
MVP Alum

This does not seem to successfully get a list of the feature class either, it returns the definitions but not the actual name.  I cannot find any examples of doing this in the snippets or help either.    Probably going to switch to python to do this task, but I would like to avoid that because python takes a lot longer to run.

0 Kudos
RichRuh
Esri Regular Contributor

I'm not sure I understand.  Once you get the FeatureClassDefinition object, what is returned when you call FeatureClassDefinition.GetName()?

0 Kudos
RichRuh
Esri Regular Contributor

Are you sure you're looking at the right file geodatabase?  By default, the project workspace (Project.Current.DefaultGeodatabasePath), which is the file geodatabase that is automatically created when you create a project, *is* empty.  

By the way, going back to your original code snippet, you should call GetDefinition() with FC, not fcFinal.

MatthewDriscoll
MVP Alum

This worked, calling just the name of the feature class and not the whole path.  I was wanting the default database.  Thank you for sticking with me Rich!