6

I have gotten stuck on seemingly simple ArcGIS field calculator python expression.

I obtained a great answer to a another similar question a few years ago: Using Range in Python expression of ArcGIS Field Calculator?. Now I need to populate a new field based on 6 ranges of values (bedrock depth).

My attempt at writing this expression is below:

enter image description here

I just need to populate the empty field "bedrockdp" using the ranges in the toc called brockdepmin.

screen shot

Pre-Logic Script Code:

def !GRIDCODE! (value):
    if value >0 and value < 50:
        return "0 - 50 cm from the surface"
    else:
        return "n/a"

bedrockdep =

GRIDCODE (!bedrockdep!)
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
sirgeo
  • 2,029
  • 5
  • 33
  • 43

1 Answers1

14

I think the problem lies with your statement:

def !gridcode! (value):

is incorrect, see http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000

def something(value):

It's hard to read what you've got there but I think this is it: Python expression

Full expression:

def TextValue(FromCode):
    if FromCode > 0 and FromCode < 50:
        return "0 - 50 cm from the surface"
    elif FromCode < 150:
        return "101 - 150 cm"
    elif FromCode < 200:
        return "151 - 200 cm"
    elif FromCode < 250:
        return "201 - 250 cm"
    elif FromCode < 1000:
        return "251 - 1000 cm"
    else:
        return "N/A"

This will populate the text that is in the legend using the GRIDCODE as the numeric source.

Michael Stimson
  • 25,566
  • 2
  • 35
  • 74
  • http://i.stack.imgur.com/JEmEH.png is link to the screenshot – sirgeo Jun 02 '14 at 03:56
  • Fair enough, the problem is that you're using !gridcode! as the function name. The field bedrockdepmin is the one that gets the exclamation marks. It has to do with the scope of the variable.. only the lower box allows substitution, the pre-logic code deals only with variables and the return value is what is populated into the selected field. I can't see the field that you are using to calculate from brockdepmin in the screenshot, is it there? or is it the GRIDCODE? – Michael Stimson Jun 02 '14 at 04:15
  • GRIDCODE is the only value from the attribute table being used. The toc has the 6 classes that need to be added to the bedrockdep field based on the GRIDCODE field. I will give your code a shot in the morning and post the results here. THANKS – sirgeo Jun 02 '14 at 05:05
  • 1
    good answer, but I recommend using the parentheses in the condition: if (FromCode > 0) and (FromCode < 50): – radouxju Jun 02 '14 at 08:54
  • okay, it's running but only populating bedrockdep field with "N/A". Not sure how to post code in threads <cntl + k> { } ?? Here's the paste of the Pre-Logic: def GRIDCODE(FromCode): if (FromCode > 0) and (FromCode < 50): return "0 - 50 cm from the surface" elif (FromCode < 150): return "101 - 150 cm" elif (FromCode < 200): return "151 - 200 cm" elif (FromCode < 250): return "201 - 250 cm" elif (FromCode < 1000): return "251 - 1000 cm" else: return "N/A" – sirgeo Jun 02 '14 at 13:51
  • screen shot here: http://i.imgur.com/8UQD8l9.png – sirgeo Jun 02 '14 at 13:56
  • S O L U T I O N http://i.imgur.com/I7ijD9F.png I forgot to replace FromCode with GRIDCODE. It is working! Just one last thing how do you write greater OR EQUAL ? – sirgeo Jun 02 '14 at 14:05
  • 2

    = is the operator for greater or equal.

    – Michael Stimson Jun 02 '14 at 21:52
  • @radouxju I used to put the parenthesis around the conditions, sort of 'C' style but got pinged because they were unnecessary. I like them, and use them in my own code, they help to isolate the if statement and contribute to the readability. – Michael Stimson Jun 02 '14 at 22:05
  • @>= is the operator for greater or equal. – Michael Miles-Stimson Thanks a bunch! Do you know of list for commonly needed python field calculator expressions?... Something not like the Esri help web pages would be ideal; just concise easy to follow list using examples like the code you provided above. – sirgeo Jun 02 '14 at 22:18
  • The link http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004s000000 provided in the answer is a basic tutorial, about half way down is a nice picture on how to use the field calculator. Unlike the VB field calculator expressions the python function list is just python, though I don't know about importing specific modules like sys, os, math, if you go here: https://docs.python.org/2/tutorial/ to get a tutorial of the basics of python (not arcpy). – Michael Stimson Jun 02 '14 at 22:29