2

My level is very basic in Python.

This is the python codeblock I have in ArcGIS Field Calculator:

def Reclass(THLB):
  if THLB == 0:
    return 0
  elif THLB == 1:
    return !Shape_Area!

This works fine if I have another value for elif..say 1. The field populates great. But I can't get the elif to return another field (area in this case).

What am I overlooking?

Both the shape area and field I'm populating are floats.

3
  • Note that the !name! syntax is not standard Python. It's some whacko ESRI customization, so don't be surprised if it behaves in remarkably strange ways.
    – jpmc26
    Jun 27, 2017 at 10:06
  • 1
    @jpmc26 the exclamation marks around a field name is definitely not standard python, its a marker used by Esri field calculator to substitute the value of the field for each feature and not a string or variable. The confusion arises when one reads about the field calculator syntax and (in my view) the exclamation bounding is not adequately explained; the scope of the variable token !field! is only on one line, the calculation line, and is out of scope anywhere else. Jun 27, 2017 at 20:59
  • @MichaelStimson Yes, I'm aware. Also, ESRI not explaining something adequately and not making it intuitive enough to figure out on your own? Tell me something new. ;) ESRI must be preparsing the code snippet and replacing it with some actual Python before evaluating it, and whatever they did doesn't work right except in a very limit scope. How or why they would even do that is beyond me; I should think they would have had to do extra work to limit its scope.
    – jpmc26
    Jun 27, 2017 at 21:16

1 Answer 1

3

!Shape_Area! is out of scope for this calculation, if you want to calculate the values of shape_area you need to specify like this:

def Reclass(THLB,Area):
  if THLB == 0:
    return 0
  elif THLB == 1:
    return Area

Then call like:

Reclass(!Field!,!shape_area!)

That is of course provided your data type has a shape_area field. If your data is a shapefile or other data source that doesn't have a shape area field you need to calculate your area first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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