ArcMap 10.4 Field Calculator Manhole Invert Lelels

04-25-2017 04:03 PM
New Contributor

Hi all,

This one should be an easy.

I have a table from Collector with recorded manhole Invert depths.

When I perform simple subtraction [LID_LEVEL] - [INV1_DEPTH] I get Invert Levels replicating Lid Levels if Invert Depth is zero. I want to return zero value in those cases.

0 Kudos
5 Replies
MVP Esteemed Contributor

The easiest way to fix this (after the fact) is to take advantage of the fact that the Calculate Field tool only operates on selected records. Open the attribute table, select for all records where your invert depth is zero, and calculate your results field to zero.

You can also get fancier with Calculate Field with a code block (see the help), but it is easier to use selections.

New Contributor

thanks, it did the job! I might need to automate this process with some code if I start getting more data.

if anyone can assist with that, it would be great!

0 Kudos
MVP Esteemed Contributor

You can still use the field calculator if you just have one condition to test.

I have substituted your field names with 'a' and 'b' in the example, then wrapped it up with them

I am sure you can follow along.  The condition is on the right, the False, True slice is on the left

... [do this if False, do this if True][condition to check] .... is the syntax

a = 10
b = 11
[a - b, 0][a - b <=0.0]
0 # returns zero

# try a positive
a = 10
b = 9
[a - b, 0][a-b <=0.0]
1  # returns the difference

# now it looks horrible with your field names, but so what
# NOTE  use the Python parser... field names enclosed in !'s
[!LID_LEVEL! - !INV1_DEPTH!, 0][!LID_LEVEL! - !INV1_DEPTH! <=0.0]
0 Kudos
MVP Esteemed Contributor

Oh I get it. You are casting boolean expressions to integer in the context of a slice index, where True and False will cast to 1 and 0.

>>> int(True)
>>> int(False)
>>> a - b >= 0
>>> int(a - b >= 0)

I guess I'd rather create the code block. Readability counts.

Calculate FIeld Expression


Calculate Field Code block ("pre-logic script code")

def f(lev, dep):
    if lev > dep:
        val = lev - dep
        val = 0
    return val

0 Kudos
MVP Esteemed Contributor

If it is simple like a two case condition check for the field calculator, I like the direct solution. 

With a code block you can make a mistake with the expression or the code block. like

f(!LID_LEVEL! - !INV1_DEPTH!) should be 


0 Kudos