Tuesday, 2 August 2016

Writing Python script in Field Calculator in ArcGIS Desktop?


I'm a programmer, I have programmed in C#, Java, but not with Python. And I'm not a geographic worker. How can I write a python script into Field Calculator to be able to check the other column value. If that other column value is "a certain string", the value in this column should be 0, otherwise it should be computed with an equation. The value I want to look is a Text type, and I want to count in a Double type column.



Answer



You don't 'Dim' variables in python, you just declare and assign them. That said, your basic route is to set the parser to python and check Show Codeblock. In the Pre-Logic Script Code box, enter your function like this:


def DoThis(fld):
val = 0
if fld <> 'a certain string':
val = # do your calculation here
return val


In your 'field =' box, enter:


DoThis(!field!)

In python , # means a comment. Indentation counts. If your calculation depends on other fields, pass those in as well. That should be straightforward with your background. In calling the function, the field name is bracketed by exclaimation (!) marks.


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...