Any ideas on how to list unused domain values from within a coded domain?
An example is we had PKWY EAST in a domain for street suffixes. I would like to generate a list of domain values that are not being used in a feature class.
Here's a sample script using ideas in my previous posts:
<SPAN class="keyword token">import</SPAN> arcpy gdb <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Path\To\file.gdb"</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> gdb <SPAN class="comment token"># set environment for arcpy</SPAN> <SPAN class="comment token"># gdb domains to dictionary # # # # # # #</SPAN> domDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># empty dictionary</SPAN> domains <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>ListDomains<SPAN class="punctuation token">(</SPAN>gdb<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> domain <SPAN class="keyword token">in</SPAN> domains<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> domain<SPAN class="punctuation token">.</SPAN>domainType <SPAN class="operator token">==</SPAN> <SPAN class="string token">'CodedValue'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> domain<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> domDict<SPAN class="punctuation token">:</SPAN> vList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># empty list</SPAN> coded_values <SPAN class="operator token">=</SPAN> domain<SPAN class="punctuation token">.</SPAN>codedValues <SPAN class="keyword token">for</SPAN> val<SPAN class="punctuation token">,</SPAN> desc <SPAN class="keyword token">in</SPAN> coded_values<SPAN class="punctuation token">.</SPAN>items<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> vList<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN>val<SPAN class="punctuation token">:</SPAN>desc<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN> domDict<SPAN class="punctuation token">[</SPAN>domain<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> vList <SPAN class="comment token"># print domDict</SPAN> <SPAN class="comment token"># read feature's fields and domains information # # # # # # #</SPAN> fc <SPAN class="operator token">=</SPAN> <SPAN class="string token">'MyFeature'</SPAN> <SPAN class="comment token"># a feature in the geodatabase</SPAN> fields <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListFields<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">)</SPAN> fldDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># empty dictionary</SPAN> <SPAN class="keyword token">for</SPAN> field <SPAN class="keyword token">in</SPAN> fields<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>field<SPAN class="punctuation token">.</SPAN>domain<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> fldDict<SPAN class="punctuation token">[</SPAN>field<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> field<SPAN class="punctuation token">.</SPAN>domain <SPAN class="comment token"># print fldDict</SPAN> <SPAN class="comment token"># count values in fields with domains # # # # # # #</SPAN> domCount <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> fldList <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>fldDict<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> fldList<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> rows<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> rows<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> i<SPAN class="punctuation token">,</SPAN> f <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>fldList<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># print i, fldList<I>, row<I> # index, field name, field value</I></I></SPAN> <SPAN class="keyword token">if</SPAN> fldList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> domCount<SPAN class="punctuation token">:</SPAN> domCount<SPAN class="punctuation token">[</SPAN>fldList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> domCount<SPAN class="punctuation token">[</SPAN>fldList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> domCount<SPAN class="punctuation token">[</SPAN>fldList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> domCount<SPAN class="punctuation token">[</SPAN>fldList<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">del</SPAN> rows <SPAN class="comment token"># release any locks</SPAN> <SPAN class="comment token"># print domCount</SPAN> <SPAN class="comment token"># output the results # # # # # # #</SPAN> <SPAN class="keyword token">for</SPAN> k<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> domCount<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Field: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>k<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" Domain: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>fldDict<SPAN class="punctuation token">[</SPAN>k<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> dLst <SPAN class="keyword token">in</SPAN> domDict<SPAN class="punctuation token">[</SPAN>fldDict<SPAN class="punctuation token">[</SPAN>k<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> k1<SPAN class="punctuation token">,</SPAN> v1 <SPAN class="keyword token">in</SPAN> dLst<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> k1 <SPAN class="keyword token">in</SPAN> v<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" Key: {} - Description: {} - Count: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>k1<SPAN class="punctuation token">,</SPAN> v1<SPAN class="punctuation token">,</SPAN> v<SPAN class="punctuation token">[</SPAN>k1<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" Key: {} - Description: {} - Count: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>k1<SPAN class="punctuation token">,</SPAN> v1<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># may need to add code to check for invalid codes in fields ?</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></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><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
And my test results:
Field: MyType Domain: MyCode Key: 1234 - Description: One - Count: 3 Key: 2345 - Description: Two - Count: 2 Key: 3456 - Description: Three - Count: 0 Key: 6789 - Description: Six - Count: 0 Key: 4567 - Description: Four - Count: 0 Key: 5678 - Description: Five - Count: 0 Key: 7890 - Description: Seven - Count: 1 Field: MyColor Domain: MyColor Key: Y - Description: Yellow - Count: 1 Key: P - Description: Purple - Count: 0 Key: B - Description: Blue - Count: 1 Key: R - Description: Red - Count: 2 Key: G - Description: Green - Count: 2 <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>
And in place of TableToDomain/DomainToTable, see ListDomains on how to get a list of domains and coded/max/min values. You can use something like ListFields to get the domain name a field uses (field.domain).
Similar to Forest's approach, I would load the feature's field with the domains you are interested in into a dictionary using code such as:
<SPAN class="keyword token">import</SPAN> arcpy fc <SPAN class="operator token">=</SPAN> <SPAN class="operator token"><</SPAN>your feature<SPAN class="operator token">></SPAN> field <SPAN class="operator token">=</SPAN> <SPAN class="operator token"><</SPAN>domain field<SPAN class="operator token">></SPAN> d <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>field<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> rows<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> rows<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> d<SPAN class="punctuation token">:</SPAN> d<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> d<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="keyword token">for</SPAN> k<SPAN class="punctuation token">,</SPAN> v <SPAN class="keyword token">in</SPAN> d<SPAN class="punctuation token">.</SPAN>iteritems<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># something like if k not in domainTable ....</SPAN> <SPAN class="comment token"># or if domainTable[value] not in d.keys() ...</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>
And then you can compare with the table to domain information. This would also count the number of rows with specific values.
Clever 🙂
What about comparing all used domain values with a table of all domain values?
It's outside the scope of your request, but identifying orphaned domains is out of the box functionality for Laurel Hills GIS GenSentry application.
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.