2

I want to calculate some values with field calculator using Python in ArcGIS table. I have used this link: http://support.esri.com/technical-article/000012478 but with different values of angles. I used this:

if (angle >= 67.5 and angle <=90):

    return "N"
 if (angle > 90 and angle <=112.5):

    return "N"
 if angle >= 22.5 and angle <67.5):

    return "NE"

 if (angle >=0 and angle <22.5):

    return "E"

 if (angle >= -22.5 and angle <0):

    return "E"
 if (angle >= -67.5 and angle <-22.5):

    return "SE"
 if (angle >= -90 and angle <-67.5):

    return "S"
 if (angle >= -112.5 and angle <-90):

    return "S"
 if (angle >= -157.5 and angle <-112.5):

    return "SW"
 if (angle > -157.5):

    return "W"
 if (angle >112.5 and angle <=157.5):

    return "NW"
 if (angle >157.5):

    return "W"

but that doesnt work.I have set all parameters like it is shown in http://support.esri.com/technical-article/000012478 The error message is: ,,There was a failure during processing, check the Geoprocessing Results window for details". What is the problem?

3
  • 1
    So what is the error? Just saying it does not work is not very helpful in describing the problem! Do you get an error message, does it run to completion but do nothing, are you trying to write a string into a numeric field, we can't tell as you have not shown how you are running this code. Suggest you amend you question with more info.
    – Hornbydd
    Dec 7, 2016 at 12:42
  • I have updated my question now.
    – user87349
    Dec 7, 2016 at 12:48
  • 1
    Please use Code formatting ({} button) to make your question legible. Providing an error message that says to check elsewhere without providing that information as well is less than useful. Best coding practice is to use the "else-if" construct in this context, initializing a variable to None and updating the variable inside the if cascade, the returning the variable. It's quite possible you failed to test all conditions.
    – Vince
    Dec 7, 2016 at 13:00

2 Answers 2

8

In one of your first lines you are missing a bracket (. This is your code with the bracket added:

def Direction(angle):
    if (angle >= 67.5 and angle <=90):
        return "N"
    if (angle > 90 and angle <=112.5):
        return "N"
    if (angle >= 22.5 and angle <67.5):
        return "NE"
    if (angle >=0 and angle <22.5):
        return "E"
    if (angle >= -22.5 and angle <0):
        return "E"
    if (angle >= -67.5 and angle <-22.5):
        return "SE"
    if (angle >= -90 and angle <-67.5):
        return "S"
    if (angle >= -112.5 and angle <-90):
        return "S"
    if (angle >= -157.5 and angle <-112.5):
        return "SW"
    if (angle > -157.5):
        return "W"
    if (angle >112.5 and angle <=157.5):
        return "NW"
    if (angle >157.5):
        return "W"

This worked for me, so maybe it'll work for you too. However, with the small amount of information here it might be possible there's still something wrong. Here are some tips:

  • It's easier to read a question that's well written, which makes getting answers faster as well. It's really hard to see the missing bracket in your current layout, whereas it's easily spotted if it's entirely written in a code block like the one above.
  • Your error said "There was a failure during processing, check the Geoprocessing Results window for details". So what do you do? You go to the geoprocessing results window. There, the error is clearly listed: SyntaxError on line #, that's how I found the missing bracket. You can open this by going to Geoprocessing -> Results -> Current Session, and clicking on your calculate field tool you just used.
2

OK if you have set up the tool as shown in the link and your code in the question is exactly as you inputted it then I am not seeing the function header:

def Direction(angle):

Note python is a very unforgiving language and a single space out of place will cause the code to fail. You need to check that you have all the indentations correct. I often find it helpful when copying code from websites to paste it into IDLE as you can then go to Run > Check module to ensure syntax is correct. IDLE is installed as part of python so it will be on your computer.

1
  • I have the def Direction(angle): function too, but I cant find the mistake. Can you find that?
    – user87349
    Dec 7, 2016 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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