Select to view content in your preferred language

nesting an arithmetic function within the reclass function!

4591
6
09-16-2013 12:38 PM
DanielAmrine
Frequent Contributor
I have used the reclass function several times, but what I would like to know is how to add a line after the "return" that prints the value of field A + 90 instead of just "1" into the cell.

"Parser:
Python

Expression:
Reclass(!WELL_YIELD!)

Code Block:
def Reclass(WellYield):
    if (WellYield >= 0 and WellYield <= 10):
      return 1 Instead of "1" a Calculation of (Field A +90 or Field B -180) as an example.
    elif (WellYield > 10 and WellYield <= 20):
      return 2
    elif (WellYield > 20 and WellYield <= 30):
      return 3
    elif (WellYield > 30):
      return 4
"

Any help is much appreciated! i'm hoping it's something simple and i just don't know!
Tags (2)
0 Kudos
6 Replies
Luke_Pinner
MVP Regular Contributor
Firstly, read this: http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code

Then do something like this:
Expression:
Reclass(!WELL_YIELD!,!Field_A!)


Code Block:
def Reclass(WellYield, Field_A):
    if (WellYield >= 0 and WellYield <= 10):
        return Field_A +90
    elif (WellYield > 10 and WellYield <= 20):
        return 2
    elif (WellYield > 20 and WellYield <= 30):
        return 3
    elif (WellYield > 30):
        return 4
0 Kudos
DanielAmrine
Frequent Contributor
This is great i will test it ASAP,

Thank you for your response!

Dan
0 Kudos
DanielAmrine
Frequent Contributor

Unfortunately using return !Field A! does not return the value from Field A.

I tried this as well and it didn't work. Keeps telling me there is a syntax error at line 3.

expression

DirectionCalc( !TestNeg!, !Angle! )

Code:

def DirectionCalc(TestNeg,Angle):

    if (TestNeg == 1):

        return float(!Angle!)

    elif (TestNeg >1):

        return 0

This did work

def DirectionCalc(TestNeg,Angle):

    if (TestNeg == 1):

        return Angle

    elif (TestNeg >1):

        return 0

Im using chrome and i selected advanced editor but there is no option for "code wrap"

How do you return the value from a different field? There is no "#" button to click in this new geonet.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It isn't called "code wrap" but Insert > Syntax Highlighting

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

You are getting the syntax error on line 3 because there is no variable !Angle! in the Python function you defined.  This also explains why the second block of code doesn't give you an error.

Are you saying the second code block above, the one that works, doesn't return the correct result?

0 Kudos
DanielAmrine
Frequent Contributor

Joshua,

the second code block did work in terms of puling in the value from the Angle field and returning into the Field I am calculating.

So angle + 1 would add the value from the angle field +1 into the target field.

This is the final code I came up with to convert the angle from a previous calculation into quadrant bearings.

def DirectionCalc(TestNeg,Angle):
    if (TestNeg == 1):
        return 90-Angle
    elif (TestNeg == 2):
        return (Angle*-1)+90
    elif (TestNeg == 3):
        return 270 - Angle
    elif (TestNeg == 4):
        return (Angle*-1)+270
    elif (TestNeg == 5):
        return 0
    elif (TestNeg == 6):
        return 90

Lower Box:

DirectionCalc( !TestNeg!, !Angle! )

I started with a polyline layer added 4 fields... start(X,Y) and end(X,Y). Then I calculated a DeltaX and DeltaY, And used these to calculate the angle of the line from the start point. I then created TestNEG to classify which quadrant each angle fell in based on which Delta was negative...so

def TestNegative(DeltaX,DeltaY):
    if (float(DeltaX) > 0 and float(DeltaY) > 0):
       return 1
    elif (float(DeltaX) < 0 and float(DeltaY) > 0):
       return 4
    elif (float(DeltaX) > 0 and float(DeltaY) < 0):
       return 2
    elif (float(DeltaX) < 0 and float(DeltaY) < 0):
       return 3
    elif (float(DeltaX) == 0):
       return 5
    elif (float(DeltaY) == 0):
       return 6

Lower Box:

TestNegative(!DeltaX! , !DeltaY!)

Then apply the first box of code based on this calculation.

I could probably now combine the two codes by replacing the classification numbers with the Angle field and the calculations from the first block and that would save having to classify the values.

So your initial response helped a lot Joshua and i feel like a made some major headway in using python today!

Dan

0 Kudos