How do I test if a domain exists?

1506
7
10-26-2010 08:46 AM
PaulDziemiela
New Contributor
Hi folks,

Simple question, I cannot figure out how to gracefully check if a domain exists in a file geodatabase using Python with ArcGIS 9.3.1 SP2.  I can try to create the domain afresh and then catch the error but seems like something I should be able to test for.   

Thanks,

Paul
0 Kudos
7 Replies
DaleHoneycutt
Occasional Contributor III
Describe the workspace - the returned describe object has a list of domains.

See http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Workspace_properties
0 Kudos
PaulDziemiela
New Contributor
Thanks!

I can wrap that up into an exists function.

Cheers,

Paul
0 Kudos
AdamCox1
Occasional Contributor II
Long after the fact, but this thread very nearly answers my question...

I want to make a conditional statement happen if a domain doesn't yet exist.  In other words, if the domain "GOES_TO" does not exist, I don't want it be (unsuccessfully) created in the next step.

desc = arcpy.Describe(gdb_path)
    domains = desc.domains

    for domain in domains:
        if domain == "GOES_TO":
            isitthere = "yes"
        else:
            isitthere = "no"

if isitthere == "no":

    #create the new domain, etc.

This code doesn't work because as the list goes through each domain, all of them but one are not called "GOES_TO", which means that isitthere will always equal "no".  But at least you can see what I'm trying to do...  I feel like there's something obvious that I'm missing...
0 Kudos
DaleHoneycutt
Occasional Contributor III
isthere = False
for domain in domains:
  if domain == "GOES_TO":
    isthere = True
    break

if not isthere:
  # do something
0 Kudos
AdamCox1
Occasional Contributor II
yes, that is what I was missing.  I appreciate the quick answer.  Thanks!
0 Kudos
DaleHoneycutt
Occasional Contributor III
FYI:
In 10.1, there is a new data access Python module that has a ListDomains function that returns Domain objects. Domain objects have all the properties of the domain, not just the name. ListDomains is far superior to describing a workspace object.
0 Kudos
AdamCox1
Occasional Contributor II
Yeah, I found that out in another thread.  I'm using 10.0 though, so I'll have to wait for an upgrade to use ListDomain.
0 Kudos