Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have been replacing field calculations with python update cursors in many of my work flows and today I ran into a problem. The fields being updated have an attribute domain. so now, when I "calculate" the field based on the domain value, the field is not populated with the desired description. Instead it is populated with the value used in the calculation. Please see the portion of code for the update cursor.

# Get linear units
(path, name) = os.path.split(featureClass)
spatial_ref = arcpy.Describe(path).spatialReference
units = spatial_ref.linearUnitName
if units == "Meter":
    lengthUOM = "Metre"

# Calculate feature class fields
rows = arcpy.UpdateCursor(featureClass)
for row in rows:
    length = row.getValue("Shape_Length")
    row.setValue("featureLength", length)
    row.setValue("UnitOfMeasure", lengthUOM) #this field has a domain
    rows.updateRow(row)
del row, rows

When I use field calculator (in an edit session) and set the value to "Metre" the field will populate with the description from the domain (what I would like). The above code, however, sets values in the field to "Metre" instead of the description. I have read this question, but it does not address update cursors.

Has anyone had any success with update cursors on fields with domains? If so, could you give me some insight into where lies my mistake? Thanks in advance for any advice.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It sounds like you need to get the coded value for Metre and calculate that coded value into the field, is that correct?

I've handled this with a dictionary before, but I created the coded-value domain myself with a dictionary (timezones below), so I already know the key: value pairs. So the trick is to, for a given value in the dict, get the key:

def get_timezone_key(timezone_val):
    """Get key value for a given timezone text value"""
    timezones = {1: "US/Alaska", 2: "US/Central", 3: "US/Eastern",
                 4: "US/Hawaii", 5: "US/Pacific"}
    # Get the subtype code from dict based on input def timezone user selected
    timezone_key = timezones.keys()[timezones.values().index(timezone_val)]
    return timezone_key

print get_timezone_key("US/Central") # gives 2

Please clarify if this is not what you are needing to accomplish.

share|improve this answer
    
thank you for the response. I was actually trying something similar to your response. However, I just found an error in my code. A key of "Metre" does not return the right value because the key value is a non-capitalized "metre" string. Other than that, using the correct key, as you suggest, in an update cursor works perfectly. –  Barbarossa Jul 12 '13 at 17:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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