Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to calculate the direction the 'generate near table' function gives me into actual directions, N, NW etc. Azimuth would be a start too. I tried this script in the field calculator but it doesn't run.

This was the original we tried:

if([Direction]>0,
    if([Direction]<22.5,"W",
    if([Direction]<67.5,"SW",
    if([Direction]<112.5,"S",
    if([Direction]<157.5,"SE",
    if([Direction]<180,"E",""))))),

if([Direction]>-22.5,"W",
    if([Direction]>-67.5,"NW",
    if([Direction]>-112.5,"N",
    if([Direction]>-157.5,"NE",
    if([Direction]>-180,"E",""))))))

I tried it with and without "then output=" and "Else, output =0". I'm using ArcGIS 10, have tried with with python and vb ticked.

share|improve this question
    
Are you using 9.3 arcmap, qgis, autodesk or other software for field calculator? If arcmap do you have the python type ticked or vba? –  Brad Nesom Nov 2 '11 at 19:39
1  
I doubt either Python or VBA (or any other language) would parse all those commas correctly, @Brad. Julia, please format your code neatly so people can read it! –  whuber Nov 2 '11 at 19:44
    
That is using Excel-style If-Then logic with incorrect syntax. I agree with the previous points. It would help to see the code in a clean format and to know which software is being used. –  user3461 Nov 2 '11 at 20:00
    
Julia, you can edit your orig. question to clean up the code. The commas probably aren't needed unless you're using Excel. Can you please let us know which software you're trying to work in? –  user3461 Nov 2 '11 at 20:02
    
And it's ArcGIS 10. I tried python and vba. –  Julia Nov 2 '11 at 20:04
show 3 more comments

3 Answers 3

Try this. Set Parser to VBScript and tick the Show Codeblock option. Add this to the Pre-Logic Script Code field.

Dim output As String

If [Direction] > -22.5 And [Direction] < 22.5 Then
    output = "W"
ElseIf [Direction] >= 22.5 And [Direction] < 67.5 Then
    output = "SW"
ElseIf [Direction] >= 67.5 And [Direction] < 112.5 Then
    output = "S"
ElseIf [Direction] >= 112.5 And [Direction] < 157.5 Then
    output = "SE"
ElseIf [Direction] >= 157.5 And [Direction] <= 180 Then
    output = "E"
ElseIf [Direction] <= -22.5 And [Direction] > -67.5 Then
    output = "NW"
ElseIf [Direction] <= -67.5 And [Direction] > -112.5 Then
    output = "N"
ElseIf [Direction] <= -112.5 And [Direction] > -157.5 Then
    output = "NE"
ElseIf [Direction] <= -157.5 And [Direction] >= -180 Then
    output = "E"
Else ' Some error
    output = "?"
End If

Add this single line to the [fieldname] = field.

output

Note: I have not tried this in ArcMap. If it doesn't work try turning all instances of "ELSEIF" to "ELSE IF".

share|improve this answer
1  
I tried that but it doesn't work. Not with ELSEIF or ELSE IF –  Julia Nov 2 '11 at 20:37
2  
Holy Cow, this worked! Thanks, Kevin. I tried so many different things, changed variable name etc but I just realized that the last time I ran it I did not change it to VBA. This time it worked!!! @nmpeterson, I bet yours works too this just worked first. Thank you!!!!!!!! –  Julia Nov 2 '11 at 20:58
    
There was a minor issue with your Else part, since it could result in an "invalid use of null" runtime error if you had something like 500. Also, you can prettify VBA code in MS's VBA editor in Excel (Alt+F11). –  Mike T Nov 2 '11 at 22:47
    
Thanks for the edit, Mike. You're welcome Julia. Since this worked I'd appreciate it if you accepted it as the correct answer; thanks. –  user3461 Nov 3 '11 at 2:46
add comment

Kevin beat me to it, but here is how it would be done in Python. I also cleaned up the logic a bit so you only have 8 checks rather than 10. Try checking the Python button and doing it with this as the Codeblock:

def direction(x):
    output = ''
    if ((x >= 157.5 and x <= 180) or (x >= -180 and x < -157.5)):
        output = 'E'
    elif (x >= -157.5 and x < -112.5):
        output = 'NE'
    elif (x >= -112.5 and x < -67.5):
        output = 'N'
    elif (x >= -67.5 and x < -22.5):
        output = 'NW'
    elif (x >= -22.5 and x < 22.5):
        output = 'W'
    elif (x >= 22.5 and x < 67.5):
        output = 'SW'
    elif (x >= 67.5 and x < 112.5):
        output = 'S'
    elif (x >= 112.5 and x < 157.5):
        output = 'SE'
    return output

and this as the actual calculation:

direction(!Direction!)
share|improve this answer
1  
neither worked. I did define the field as text since the output will be a letter. that is correct, right? –  Julia Nov 2 '11 at 20:56
    
I didn't have a chance to test it in ArcMap, but I can't see any reason why it wouldn't work if you had the Python option selected and were doing the calculation on a text field. –  nmpeterson Nov 2 '11 at 22:25
    
For what it's worth, I believe the problem with this was that I hadn't written the code as a function. In this format, it should now work. –  nmpeterson Nov 5 '11 at 18:54
add comment

In python:

["E","NE","N","NW","W","SW","S","SE","E"][int(round((!Direction!/45.0)+4))]
share|improve this answer
2  
This is very slick, albeit less intuitive than the other answers. Just to clarify for everyone how it works: the first set of square brackets contains a list of strings, and the second set acts as an index. The expression evaluates to an integer between 0-8 for any angle between -180 and +180. The output, then, is the list item at the zero-based position specified by the integer expression. –  nmpeterson Nov 5 '11 at 18:52
    
Very clever! :) –  blah238 Nov 5 '11 at 20:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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