With the arrival of ArcGIS 10.0, Esri introduced the ability to use Python within the Field Calculator allowing the user the ability to perform more complex calculations.
How does this help me?
Let’s suppose I need to create a map showing the fields of the central North Sea in both the context of their hydrocarbon production type and their depth. To do this, I will utilise two columns from the fields data; FIELDTYPE and DEPTH_M.
I’ve decided that a simple depth classificaton, based on DEPTH_M, will suffice:
- Shallow
- Medium
- Deep
Setting it up
First I use Add Field to create a new column called FLD_CLASS, which will contain my new classification.
Next I open the Field Calculator by right clicking on FLD_CLASS in the table and choosing Field Calculator.
To prepare the calculator to process Python, I set the parser to Python by clicking the radio button at the top of the Field Calculator.
I activate the codeblock by clicking on the tick box Show Codeblock. This opens a new section called the Pre-Logic Script Code window.
Enter the codeblock
The Python code is written in the Pre-Logic section and called by the expression window.
Below, I have placed the Python code alongside the field calculator. The code consists of the Python function called cProc
The cProc function has two required parameters, called ft and dep, which represent the hydrocarbon field type and the depth.
In essence, cProc is a series of decisions (using if, elif and else statements) on what to name the output values a and d.
In the Field Calculator, I have placed the expression calling the cProc function and sending in the two column names as parameters.
cProc( !FIELDTYPE!, !DEPTH_M! )
At the end of the cProc function, I have a return statement: return ‘{0} {1}’.format(a,d)
This will return a formatted string, which is the result of the conditional statements in cProc.
When I have written the code to the Field Calculator, I usually save the calculation with the Save… button, which saves it to a .cal file. I can load this at a later date should the data change and require reprocessing.
In the table screenshot at the start, the first record has the values FILEDTYPE = GAS and DEPTH_M = 32.4. When the field calculator passes over that row, it will execute the cProc with those values.
For example, cProc(“GAS”, “32.4”) would return “Gas Shallow”
As ft is equal to GAS, then a will equal Gas
As dep is less than 50.0, then d will equal Shallow
The Result
Once the calculation is complete the table should look as follows and allow me to produce the map in this form.
Posted by Ben Holmes, GIS Consultant, Exprodat.