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
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
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");
}
}
});
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.
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.
I'm not sure I understand. Once you get the FeatureClassDefinition object, what is returned when you call FeatureClassDefinition.GetName()?
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.
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!