Pro:How to check if domain exists, if not, do something else skip

3998
13
06-10-2018 11:35 AM
ThomasColson
MVP Frequent Contributor

I'm attempting to use py to check if a domain exists, if not, create it, else do nothing. After consulting How do I check if a domain already exists? , arcgis 10.0 - Using arcpy.Exists for domains? - Geographic Information Systems Stack Exchange , and esri geodatabase - Checking if domain already exists using ArcPy? - Geographic Information Systems S... I came up with the attached code. If neither domain exists, it fails with:

Start Time: Sunday, June 10, 2018 2:28:13 PM
Running script Add Extended Core Fields...
Failed script Add Extended Core Fields...
Traceback (most recent call last):
 File "C:\Temp\PRO_FOLDER_STRUCTURE\EXTENDED_CORE_FIELDS.py", line 24, in <module>
 arcpy.CreateDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", "Is the feature physically observable", "TEXT", "CODED")
 File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 1332, in CreateDomain
 raise e
 File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 1329, in CreateDomain
 retval = convertArcObjectToPythonObject(gp.CreateDomain_management(*gp_fixargs((in_workspace, domain_name, domain_description, field_type, domain_type, split_policy, merge_policy), True)))
 File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 496, in <lambda>
 return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
 ERROR 000192: Invalid value for Domain Name
Failed to execute (CreateDomain).
 Failed to execute (AddExtendedCoreFields).
Failed at Sunday, June 10, 2018 2:28:14 PM (Elapsed Time: 1.52 seconds)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

However it creates the domain!

If I run it again knowing the domain exists, same error message. 

import os
import errno
import arcpy
#Get user input
gdb_name = arcpy.GetParameterAsText(0)
fc_name = arcpy.GetParameterAsText(1)
isOBSERVABLEchecked = arcpy.GetParameterAsText(2)
isISEXTANTchecked = arcpy.GetParameterAsText(3)





#Create the Optional OBSERVABLE domain

if str(isOBSERVABLEchecked) == 'true':
        existingDomains = arcpy.da.ListDomains(gdb_name)
        isthere = False
        for domain in existingDomains:
                if domain.name == 'DOM_YES_NO_UNK_NPS2016':
                        isthere = True
                        break
                if isthere == False:
                        arcpy.CreateDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", "Is the feature physically observable", "TEXT", "CODED")
                        YESNODict = {"Unknown":"Unknown", "Yes": "Yes", "No": "No"}
                        for code in YESNODict:        
                                arcpy.AddCodedValueToDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", code, YESNODict[code])
                else:
                        arcpy.AddMessage("DOM_YES_NO_UNK_NPS2016 already exists")
                        


#Create the Optional ISEXTANT domain
if str(isISEXTANTchecked) == 'true':
        existingDomains = arcpy.da.ListDomains(gdb_name)
        isthere = False
        for domain in existingDomains:
                if domain.name == 'DOM_YES_NO_UNK_NPS2016':
                        isthere = True
                        break
                if isthere == False:
                                arcpy.CreateDomain_management(gdb_name, "DOM_ISEXTANT_NPS2016", "Indicates feature or features that are no longer in existence or partially in existence.", "TEXT", "CODED")
                                ISEXTANTDict = {"Unknown":"Unknown", "True": "True", "False": "False", "Partial": "Partial", "Other": "Other"}
                                for code in ISEXTANTDict:        
                                        arcpy.AddCodedValueToDomain_management(gdb_name, "DOM_ISEXTANT_NPS2016", code, ISEXTANTDict[code])

                else:
                        arcpy.AddMessage("DOM_ISEXTANT_NPS2016 already exists")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

As best as I can tell, I have an illegal loop condition that keeps trying to create the first domain whether it exists or not, and the failure stems from the domain name already exists. 

0 Kudos
13 Replies
ThomasColson
MVP Frequent Contributor

So with some modification to your sample to make it work in Pro, I get the following regardless if the domain exists or not. 

Start Time: Thursday, June 14, 2018 8:58:08 AM
Running script Test...
Completed script Test...
Succeeded at Thursday, June 14, 2018 8:58:08 AM (Elapsed Time: 0.13 seconds

0 Kudos
RandyBurton
MVP Alum

Try this:

found = False
for domain in existingDomains:
    if domain.name == 'DOM_YES_NO_UNK_NPS2016':
        found = True

if found:
    print "Domain exists"
else:
    print "Domain does not exist"
DanPatterson_Retired
MVP Emeritus

Randy to keep it in the loop...

existingDomains = [0, 1, 1, 0, 1, 1, 1]  # fake domain names
for domain in existingDomains:
    if domain == 1:
        found = True
        print("Domain exists")
    else:
        found = False
        print("Domain does not exist")
        
Domain does not exist
Domain exists
Domain exists
Domain does not exist
Domain exists
Domain exists
Domain exists‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

and with python 3.6+ you can do some really cool stuff with f-strings

d = "\n".join([[f"{domain} exists", f"{domain} Missing"][domain==1] for domain in existingDomains])

print(d)
0 exists
1 Missing
1 Missing
0 exists
1 Missing
1 Missing
1 Missing
ThomasColson
MVP Frequent Contributor

Randy and Dan, you were both on to something with the "list" idea, instead of looping through each domain in the GDB.....just make a list and check for membership in the list! 

Here's what ultimately worked: 

existingDomains = arcpy.da.ListDomains(gdb_name)
domain_names = [i.name for i in existingDomains]

if 'DOM_YES_NO_UNK_NPS2016' in domain_names :
        print("DOM_YES_NO_UNK_NPS2016 already exists")
else:
        print("creating domain")
        arcpy.CreateDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", "Is the feature physically observable", "TEXT", "CODED")
        YESNODict = {"Unknown":"Unknown", "Yes": "Yes", "No": "No"}
        for code in YESNODict:        
                arcpy.AddCodedValueToDomain_management(gdb_name, "DOM_YES_NO_UNK_NPS2016", code, YESNODict
)

which results in: 

== RESTART: C:\temp\Pro_Folder_Structure\EXTENDED_CORE_FIELDS_20180614_A.py ==
creating domain
>>> 
== RESTART: C:\temp\Pro_Folder_Structure\EXTENDED_CORE_FIELDS_20180614_A.py ==
DOM_YES_NO_UNK_NPS2016 already exists