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?
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:
<SPAN class="keyword token">public</SPAN> <SPAN class="keyword token">bool</SPAN> <SPAN class="token function">FeatureClassExists</SPAN><SPAN class="punctuation token">(</SPAN>Geodatabase geodatabase<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">string</SPAN> featureClassName<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">try</SPAN> <SPAN class="punctuation token">{</SPAN> FeatureClassDefinition featureClassDefinition <SPAN class="operator token">=</SPAN> geodatabase<SPAN class="punctuation token">.</SPAN>GetDefinition<SPAN class="operator token"><</SPAN><SPAN class="token function">FeatureClassDefinition</SPAN><SPAN class="punctuation token">(</SPAN>featureClassName<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> featureClassDefinition<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Dispose</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">true</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">catch</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// GetDefinition throws an exception if the definition doesn't exist</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="keyword token">false</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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
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.
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.
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.
<SPAN class="keyword token">string</SPAN> projGDBPath <SPAN class="operator token">=</SPAN> Project<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN>DefaultGeodatabasePath<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">string</SPAN> FC <SPAN class="operator token">=</SPAN> TXB<SPAN class="punctuation token">.</SPAN>Text<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// From a text box in the xaml</SPAN> <SPAN class="keyword token">string</SPAN> fcFINAL <SPAN class="operator token">=</SPAN> System<SPAN class="punctuation token">.</SPAN>IO<SPAN class="punctuation token">.</SPAN>Path<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Combine</SPAN><SPAN class="punctuation token">(</SPAN>projGDBPath<SPAN class="punctuation token">,</SPAN> FC<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">await</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Framework<SPAN class="punctuation token">.</SPAN>Threading<SPAN class="punctuation token">.</SPAN>Tasks<SPAN class="punctuation token">.</SPAN>QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">using</SPAN> <SPAN class="punctuation token">(</SPAN>Geodatabase geodatabase <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Geodatabase</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">FileGeodatabaseConnectionPath</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Uri</SPAN><SPAN class="punctuation token">(</SPAN>projGDBPath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">try</SPAN> <SPAN class="punctuation token">{</SPAN> FeatureClassDefinition featureClassDefinition <SPAN class="operator token">=</SPAN> geodatabase<SPAN class="punctuation token">.</SPAN>GetDefinition<SPAN class="operator token"><</SPAN>FeatureClassDefinition<SPAN class="operator token">></SPAN><SPAN class="punctuation token">(</SPAN>fcFINAL<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> featureClassDefinition<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Dispose</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Framework<SPAN class="punctuation token">.</SPAN>Dialogs<SPAN class="punctuation token">.</SPAN>MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"It exists"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">catch</SPAN> <SPAN class="punctuation token">{</SPAN> ArcGIS<SPAN class="punctuation token">.</SPAN>Desktop<SPAN class="punctuation token">.</SPAN>Framework<SPAN class="punctuation token">.</SPAN>Dialogs<SPAN class="punctuation token">.</SPAN>MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"It Does Not Exist"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.