I got a call from a colleague who was having dificulty getting ArcGIS to calculate the value of a field in an attribute table. He was building a model in ModelBuilder and wanted to perform a conditional (If Then) calculatuion to evaluate whether a field’s value was Zero (0) before he tried to divide another value with it.
In response to his question, I quickly provided him the following VBA code for his field calculator tool:
Pre-Logic Script Code:
if ([SUM_weeks] <> 0) then newval = [SUM_deliv] / [SUM_weeks] else newval = .001 end if
[NewField] = newval
While this worked fine, he mentioned that he would rather have Python code, since it is rumored that esri is removing VBA in future releases of ArcGIS. In response, I initially tried to simply convert the Pre-Logic code block to the following Python code:
if (!SUM_weeks! <> 0): newval = !SUM_deliv! / !SUM_weeks! else: newval = .001
Much to my surprise this code generated a parsing error, and try as I did, I could not change it in a way that would work, despit the fact that it appears to be perfectly valid Python code.
After reviewing some portions of the ArcGIS Desktop help file, I learned that in order for me to execute the same statement in Python, I needed to write a Python function and then pass the field values into that function. So I composed the following Python code to send my colleague:
Pre-Logic Script Code:
def DoMath(field1, field2): if (field2 == 0): newval = .001 else: newval = field1 / field2 return newval
This worked fine but I was really surprised that I could not just use the field names within the code block. We’ve always been able to do this in esri projects. We coudl do it in VBA and even Avenue (for those who remember the ArcView 3.x Query Builder), AML allowed this too. But not anymore, why?
As a long time esri user, this kind of stuff concerns me. I’m not worried that they going to lose their customer base or market share. But it seems that as ArcGIS has grown more powerful some portions of it have grown more difficult for casual users. Replacing dialogs and functions that worked well for casual users with Python code blocks works well for programmers and power users, but it leaves casual users to struggle with stuff they used to know how to do.
Users shouldn’t have to write a Python function and then pass an attribute field value into that function to edit a field. This kind of editing used to be easier and I hope someone at esri figures this out and simplifies it again.
P.S. Don’t get me wrong, it’s not that I hate change or dislike Python; I use it often. I should point out that there are some wonderful simplications to field editing in ArcGIS 9.x and 10 that esri programmers should be commended for. I greatly enjoy and make frequent use of the Calculate Geometry functions and appreciate not haveing to write the VBA code blocks that I used to write to add X and Y coordinates to attribute tables or calculate polygon areas. Actually I only wrote the code once and then saved the query expressions for subsequent uses, but still, it’s much easier to use the newer Calculate Geometry functions.