I'm aware of the ArcMap tool 'Add coded value to domain' but I'm looking for some help to create a list using a features attribute and use those values to generate coded value domains. I would be using each attribute entry as the coded value and description. I'm looking to accomplish this using ArcPy...Any suggestions?
I added some code to my previous post. I found TableToDomain to be faster than AddCodedValueToDomain when there were lots of values. Also, I'm not sure how AddCodedValueToDomain would react if it the code was already in the domain, but I expect it will generate an error.
TableToDomain will create the domain and can also append to an existing one. If the domain exists and the coded value is in the domain, an error will be generated if the update option is "APPEND". If using "REPLACE", the code and description are overwritten.
Thanks for your help on this
It should just be in your print output. For line 12 in your code, you can use: print x[0]
I ended up running the code below and it runs great except adds (u' ',) around the actual text I want to assign. Ideas on how to remove this?
<SPAN class="keyword token">import</SPAN> arcpy arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwrite <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> wrkspc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"G:\Temporary\Anthony\Workspaces\New File Geodatabase.gdb"</SPAN> poly <SPAN class="operator token">=</SPAN> <SPAN class="string token">"G:\Temporary\Anthony\Workspaces\New File Geodatabase.gdb\TempArea"</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>poly<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'poly_layer'</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><SPAN class="string token">'poly_layer'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Text'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> poly_cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> x <SPAN class="keyword token">in</SPAN> poly_cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> x arcpy<SPAN class="punctuation token">.</SPAN>AddCodedValueToDomain_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"TempArea"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"complete"</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>
output
I would use a search cursor to read unique values into a dictionary (key and value being the same). A list set would probably accomplish the same thing. Sort if desired. Convert that into a table (possibly in_memory). And then use the Table to domain tool.
UPDATE: Here's an example:
gdb <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Path\to\file.gdb"</SPAN> domainName <SPAN class="operator token">=</SPAN> <SPAN class="string token">"DomainName"</SPAN> domainDesc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Domain Description"</SPAN> layer <SPAN class="operator token">=</SPAN> <SPAN class="string token">"featureLayer"</SPAN> field <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'TextField'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># create a table in memory</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CreateTable_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'in_memory'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'domainValues'</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"domainValues"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"CODE"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"TEXT"</SPAN><SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddField_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"domainValues"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"DESCRIPT"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"TEXT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># read field into dictionary (these will be unique values - not tested for None value)</SPAN> domainDict <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>r<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>SearchCursor<SPAN class="punctuation token">(</SPAN>layer<SPAN class="punctuation token">,</SPAN> field<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token"># sort the dictionary keys (optional, you can also sort the domain later)</SPAN> codes <SPAN class="operator token">=</SPAN> sorted<SPAN class="punctuation token">(</SPAN>k <SPAN class="keyword token">for</SPAN> k <SPAN class="keyword token">in</SPAN> domainDict<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># insert the codes into the in memory table</SPAN> rows <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>InsertCursor<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'in_memory\domainValues'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'CODE'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'DESCRIPT'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> c <SPAN class="keyword token">in</SPAN> codes<SPAN class="punctuation token">:</SPAN> rows<SPAN class="punctuation token">.</SPAN>insertRow<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">,</SPAN>c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">del</SPAN> rows <SPAN class="comment token"># table to domain</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>TableToDomain_management<SPAN class="punctuation token">(</SPAN>in_table<SPAN class="operator token">=</SPAN><SPAN class="string token">"domainValues"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="comment token"># in_memory\domainValues table</SPAN> code_field<SPAN class="operator token">=</SPAN><SPAN class="string token">'CODE'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="comment token"># code</SPAN> description_field<SPAN class="operator token">=</SPAN><SPAN class="string token">'DESCRIPT'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="comment token"># description </SPAN> in_workspace<SPAN class="operator token">=</SPAN>gdb<SPAN class="punctuation token">,</SPAN> domain_name<SPAN class="operator token">=</SPAN>domainName<SPAN class="punctuation token">,</SPAN> domain_description<SPAN class="operator token">=</SPAN>domainDesc<SPAN class="punctuation token">,</SPAN> update_option<SPAN class="operator token">=</SPAN><SPAN class="string token">"REPLACE"</SPAN> <SPAN class="comment token"># or "APPEND"</SPAN> <SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Delete_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"in_memory\domainValues"</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I did a quick check of the script inside Arcmap 10.5. I found creating an in memory table and using TableToDomain to be faster than AddCodedValueToDomain (particularly with lots of values).
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.