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
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?
2 Answers 2
It looks like you are using a coded value domain.
So that you can see what is stored rather than what is displayed:
- Open your attribute table
- Use Table Options to access Appearance
- Untick the checkbox for Display coded value domain and subtype descriptions
- 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– traggatmot2015年10月14日 07:03:01 +00:00Commented Oct 14, 2015 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– traggatmot2015年10月14日 07:05:40 +00:00Commented Oct 14, 2015 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.2015年10月14日 07:13:28 +00:00Commented Oct 14, 2015 at 7:13
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-
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.
Explore related questions
See similar questions with these tags.
' Ramp'
is not the same as'Ramp'
etc