2

Help! I think I am missing something that must be so obvious but I cannot figure out what I am doing wrong. Here's some background:

I have one short integer field (MajAspNum) that has values 1-10. I have another text field (Direction) that needs to be filled. I am trying to use the Field Calculator to calculate it but am not succeeding in any way. I should know what I'm doing but I just can't seem to make it work.

Pre-logic Script Code:
def des(num):
if num = 1:
 return "Flat"
elif num = 2:
 return "North"
elif num = 3:
 return "Northeast"
elif num = 4:
 return "East"
elif num = 5:
 return "Southeast"
elif num = 6:
 return "South"
elif num = 7:
 return "Southwest"
elif num = 8:
 return "West"
elif num = 9:
 return "Northwest"
elif num = 10:
 return "North"
else:
 return "N/A"
Direction = 
des (!MajAspNum!)

It gives me a GeoProcessing error of invalid syntax on line 2. I have tried just selecting by attributes in the attributes table and then using field calculator more simply on just those but that gives me a very weird error that literally makes no sense. I'm hoping my error is a really obvious fix to someone out there!

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jan 19, 2016 at 19:51
1
  • The answers are correct; however the issue for which you were getting the error message is that tests for equality require two equals signs: if num ==1: Commented Jan 20, 2016 at 3:41

3 Answers 3

4
def des(num):
 aList=['Flat', 'N', 'NE', 'E','SE','S','SW','W','NW','N']
 if num in range(1,11):return aList[num-1]
 return 'N/A'

Alternatively select valid records and use:

['Flat', 'N', 'NE', 'E','SE','S','SW','W','NW','N'][ !MajAspNum! -1]
answered Jan 19, 2016 at 20:14
0
3

A switch statement is ideal for what you want to do. Since Python doesn't support switch statements, you can use dictionary mapping to accomplish the same thing.

def des(num):
 the_dict = {1: 'Flat',
 2: 'North',
 3: 'Northeast',
 4: 'East',
 5: 'Southeast',
 6: 'South',
 7: 'Southwest',
 8: 'West',
 9: 'Northwest',
 10: 'North'
 }
 return the_dict.get(num, 'N/A')
answered Jan 19, 2016 at 22:01
1
  • I think this is the best solution. Switch statement (or in this case, a dictionary), is the right call. Commented Jan 19, 2016 at 22:06
1

here it is in vbs, i can do it in python later when i have the time.

dim result1
if [MajAspNum] = "1" then
 result1 = "flat"
Elseif [MajAspNum] = "2" then
 result1 = "north"
Elseif [MajAspNum] = "3" then
 result1 = "northeast"
...
Else result1 = "N/A"
end if

^^ codeblock

Direction = result1

I found this searching the site, Writing conditional (if/then) statements into Field Calculator of ArcGIS for Desktop using Python parser? it has the correct syntax for python.

answered Jan 19, 2016 at 19:59
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.