4

The solution in VB for the base process is at If/then VBScript/Python code equivalent to SQL IN ('x','y',z') expression in ArcGIS Field Calculator? works great but I was trying to nest 'if' statements and can't.

So pre-logic code of

if [grid_code] = 1 then 
f2= 2
end if
if [grid_code] = 2 then
f2=3.5
end if

...etc. but this doesn't work.

What am I doing wrong?

I tried the second 'if' before the 'end if' and also tried 'elif' but these give script errors.

Also I am seeing a warning message about "The calculated value is invalid for the row with objectID=1" and when you click click 'yes' to continue it works fine.

2 Answers 2

4

For VB, something like this should work, I think:

Pre-logic script code:

Dim f2 As Double
If [grid_code] = 1 Then
  f2 = 2
ElseIf [grid_code] = 2 Then
  f2 = 3.5
End If

asp_code =

f2

In Python, I think it would be something like this (though I don't have ArcGIS 10 so I can't check):

Pre-logic script code:

def my_func(my_field):
    if my_field == 1:
        f2 = 2
    elif my_field == 2:
        f2 = 3.5
    return f2

asp_code =

my_func(!grid_code!)

The error message saying that the "calculated value is invalid" is a bit strange. Perhaps double-check that your asp_code field is of float or double data type (not integer), and that you have all of the options in the grid_code field covered by your if statements? Sorry if that's too obvious, but it's all I can think of at the moment. Good luck!

2
  • +1 great answers. I'd lean towards using the Python solution, since it has more of a future than VBA.
    – Mike T
    Commented Oct 27, 2011 at 19:32
  • Thanks James - the python code was almost perfect. I had to include f2 in the asp_code = re_score(!VEGTYPE_NU!,f2) and f2=99 def re_score(my_field,f2): in the pre-logic ... anyway it Runs in less that a minute with 45 my_field values in a dataset with 31,000 polygons.
    – GeorgeC
    Commented Nov 4, 2011 at 5:42
3

For Python syntax, I would suggest that you would want something like this with nested if ... elif statements. As for the [grid_code] does it need to be enclosed in brackets? or is it a field? if it is a field, then I suspect that replacing [] with double quotes would do the trick

...you need a def (aka function call name passing a field name to the following
if [grid_code] == 1: 
  f2= 2
elif [grid_code] == 2:
  f2=3.5
return f2

Update... Of course boolean slicing is far easier in the field calculator, a demo, just insert ! marks around the field names (Gridcode, in this example, so it would be !Gridcode! in the field calculator)

>>> Gridcode = 1
>>> [3.5, 2.0][Gridcode == 1]
2.0
2
  • sorry I should have added the first line is "dim f2"...in VB. Using python is fine as well. Basically 'grid_code' is a field in the table and the f2 values get placed in another field called 'asp_code'. Maybe what I missed is the 'return f2'. Will try and see...
    – GeorgeC
    Commented Oct 26, 2011 at 23:13
  • also what do you mean by needing a def? is it just def (f2, grid_code) as the first line? I can't try this now as I am away from my main desktop. best,
    – GeorgeC
    Commented Oct 27, 2011 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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