ArcMap 10.4 Field Calculator Manhole Invert Lelels

1324
5
04-25-2017 04:03 PM
MaksymKhovalko
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
curtvprice
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.

MaksymKhovalko
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
DanPatterson_Retired
MVP Emeritus

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
curtvprice
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)
1
>>> int(False)
0
>>> a - b >= 0
False
>>> int(a - b >= 0)
0‍‍‍‍‍‍‍‍‍

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

Calculate FIeld Expression

f(!LID_LEVEL! - !INV1_DEPTH!)

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

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

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 

f(!LID_LEVEL!, !INV1_DEPTH!)

0 Kudos