3

Further to my earlier question,I'm trying to develop a script that eventually should contain three parameters, feature layer, domain field and a new text field. How can i write a loop that will update domain descriptions to a new text field ? I understand i need to use arcpy.da.ListDomains but i have some trouble writing the update loop.Thanks

3

You could go two ways.

1) You could use Table To Domain (Data Management) GP tool after you have found out the domain name to export the domain values and descriptions into a geodatabase table and then run Join on the feature class and table and then calculate the field. To find out the domain name for a field in a feature class you need the code:

gdb_domains = arcpy.da.ListDomains(gdb) #gdb - figure out based on fc from user
fc = r"properties" #you get fc from user
fc_fields = arcpy.ListFields(fc)

domain_field = "isOwnedRented" #you get the field from user

for field in fc_fields:
    if field.name == domain_field:
        domain_name = field.domain

After that you can supply all the input parameters for the GP tool (you'll need to add a description field to the feature class first though). Refer to the Help page for details.

2) You could go with pure arcpy and Python. You basically has to obtain the domain object first with the arcpy.da.ListDomains first, and then refer to the description values in the returned dictionary.

gdb_domains = arcpy.da.ListDomains(gdb) #figure out based on fc from user
fc = r"properties" #you get fc from user
fc_fields = arcpy.ListFields(fc)
cursor_fields = ["isOwnedRented","domdesc"]
domain_field = "isOwnedRented" #you get the field from user

for field in fc_fields:
    if field.name == domain_field:
        domain_name = field.domain

for domain in gdb_domains:
    if domain.name == domain_name:        
        if domain.domainType == 'CodedValue':
            coded_values = domain.codedValues
            with arcpy.da.UpdateCursor(fc,cursor_fields) as upd_cursor:
                for row in upd_cursor:
                    for val, desc in coded_values.iteritems():
                        if val == row[0]:
                            row[1] = desc
                            upd_cursor.updateRow(row)

Please extend the code to include the input parameters for feature class, obtaining the geodatabase it is stored in and the input field which has an assigned domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.