0

I am attempting to set a newly created field based on the strings in another field 'TNMFRC'. I am pretty sure that I have the code write, and yet everything is set to a value of '6'. Here's my code:

 def myCalc(road):
  if road == "Local Road":
    return 1
  elif road == "Controlled-access Highway":
    return 2
  elif road == "Secondary Highway or Major Connecting Road":
    return 3
  elif road == "Ramp":
    return 4
  elif road == "4WD":
    return 5
  else:
    return 6

and in the other box:

myCalc(!TNMFRC!)

Here are screenshots of the fields and the field calculator: enter image description here

enter image description here

Can anyone see what I might be doing wrong? I can't figure it out.

EDIT: So even though the field appears to be as string based field, it seems in the properties to be listed as a long integer numeric field?

enter image description here

  • Do you have leading spaces in the field values of TNMFRC? If so, something like ' Ramp' is not the same as 'Ramp' etc – PolyGeo Oct 14 '15 at 6:41
  • Do I simply check for that by copying a field? And pasting it somewhere? – traggatmot Oct 14 '15 at 6:41
  • 1
    Coded value domains are described in the help but there have also been many Q&As here concerning them. I think @Berend is right - what you have stored is an integer and not the string that is being looked up and displayed by the domain. – PolyGeo Oct 14 '15 at 6:49
  • 1
    Try exporting your data to a new shapefile. Then you should be able to see the actual values that are stored in the field. – Martin Oct 14 '15 at 6:52
  • 1
    @traggatmot A coded value domain can be set on a field, and basically defines a set of display values for it. The actual field values may be just a code or a number, the domain contains human-readable values. For instance LR: Local Road; HW: Highway, etc. To check if a field has a domain, open the layer properties dialog, select the Fields tab, and then the field you're interested in. The Field Details on the right will show the domain, if set. The fact that you have a dropdown list for that field also suggest that a domain is set. – Berend Oct 14 '15 at 6:53
3

It looks like you are using a coded value domain.

So that you can see what is stored rather than what is displayed:

  1. Open your attribute table
  2. Use Table Options to access Appearance
  3. Untick the checkbox for Display coded value domain and subtype descriptions
  4. click OK

What your field calculator code is doing is evaluations like:

if 1 == "Local Road":
    return 1

finding that the two values are not equal and falling through to the else statement that returns 6.

  • Thanks for point out what it is doing. I did understand that, but couldn't figure out what was going on behind the scenes. – traggatmot Oct 14 '15 at 7:03
  • can you edit my title to reflect the fact that the real problem lies with coded value domains? I think that would be a great help to people experiencing the same problem. – traggatmot Oct 14 '15 at 7:05
  • Done! I came across the same thing recently, and again earlier today, so when Berend noticed the domain the cause became apparent. – PolyGeo Oct 14 '15 at 7:13
1

If you want to avoid that lengthy if-elif-else block, you can try dictionary method as below-

def coder(val):
    cod_val = {'Divisional Boundary':'DIV','District Boundary':'DIST','Thana Boundary':'THAN','Upazilla Boundary':'UPAZ','International Boundary':'INTL','Union Boundary':'UN','Ward Boundary':'WRD'}
    return cod_val.get(val,"Not Found")

for tables like-

table

Description of functions: This function takes a text parameter and get the corresponding text value from the dictionary as defined inside the function. If the input parameter value is not found in that dictionary then default value ("Not Found") is returned.

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.