Select by Location + Field Calculator

275
3
09-26-2011 05:32 AM
NDC
by
New Contributor III
Hello all.  I'm trying to improve on my automation skills but have hit a road block.  I'm trying to update features in a specific area and replace all SLOPE values of 999 with 0.

I am using model builder and can successfully run select by location to identify the drain features within the defined boundary polygon.  However, I am running into problems using Field Calculator to replace the attributes of those specific drains.

For VB I was using

replace([SLOPE]"999","0")

and I receive a VBA Error code saying "Expected ')'" End of statement as I understand?

I've also tried running it through a python script I had found and tried:

!SLOPE!.replace("999","0")
That is returning a syntax error.

I am new to running code and am just jumping in but this would help tremendously to other field classes where I have been stuck using Find&Replace.

Thank you
0 Kudos
3 Replies
MarcNakleh
New Contributor III
Hi Nick,

I think the problem here is that your values might be stored as numbers.
!SLOPE!.replace('999', '0') 

would work perfectly well with strings. The thing is, replace is a string method and so needs an input that is such.

Your easiest (though sloppiest IMO) option would be to do something like:
int(str(!TNODE_!).replace('10','00'))

which converts your number to a string, executes the replace method, and then converts the result back into an integer (or float, or whatever you need.)

Something more structured that would allow more flexibility in your logic would involve using the Codeblock in the Field Calculator, where the Code Block would be:
def quick_check(val):
    if val == 999:
        return 0
    return val


and your main expression would be
SLOPE =
quick_check(!SLOPE!)

In this way, you're passing the value in SLOPE to the function above, which returns 0 if it's 999, or the regular value if it's not.

Hope this helps!
Marc
0 Kudos
NDC
by
New Contributor III
What would be the VB script to select and replace the integer values?
0 Kudos
NDC
by
New Contributor III
I'm asking because I keep getting an improper use of Null error when trying to populate the field
0 Kudos