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.

SOLVED: although the field was not nullable (no null values), a glitch created one in the heading field, so the variable was stored as a null value and thus the NoneType.

I am using updateCursor to subtract a previous value (last direction) from a current value (direction) to populate a new field (change in direction). I borrowed and modified code from the forums (thanks anonymous contributors!) so while I understand the conditionals and what they all do, I did not write this myself.

Code:

import arcpy
fc = r'...'
rows = arcpy.UpdateCursor(fc)
firstTime = True
for row in rows:
    if firstTime:
        previous = row.heading
        firstTime = False
    else:
        row.angle = row.heading - previous
        rows.updateRow(row)
        previous = row.heading
del row, rows

The error I get is:

Runtime error <type 'exceptions.TypeError'>: unsupported operand type(s) for -: 'int' and 'NoneType'

From what I understand because I have no return function after I define my variable previous, it gets erased and the variable is read as NoneType. However with multiple tries I've figured out that the return command only works in def function() codeblocks.
Is there an equivalent to return for updateCursor?

UPDATE: this similar Codeblock produces the same results.

import arcpy
fc = r'...'
rows = arcpy.UpdateCursor(fc)
count = 0
for row in rows:
    if count != 0:
        count = count + 1
        row.angle = row.heading - previous
        rows.updateRow(row)
        previous = row.heading
    else:
        count = 1
        previous = row.heading
del row, rows
share|improve this question
    
I'd have to see an example of your data, for example, what's stored in row.heading? –  Roy Jun 14 '12 at 19:35
    
heading is a numeric long field with values between 0 and 360 (compass directions with 0 being no movement). Although it seems backwards, previous is being defined because for the first row, count = 0 which means the if statement is not valid and the else statement is used, which triggers the if statement because now count = 1. –  AlmaThom Jun 14 '12 at 19:37
    
Have you tried print statements (specifically on previous)? One of your variables is doing something fishy, you need to see what's stored in it to debug. –  Roy Jun 14 '12 at 19:39
    
When I print previous I get "None". –  AlmaThom Jun 14 '12 at 19:44
    
Aha! although the heading attribute was not nullable when I created it, there was a glitch and a <null> value was recorded. When I fixed that, the script ran normally. Unintentionally storing a null value in my variable made it none. Thanks Roy. –  AlmaThom Jun 14 '12 at 19:49

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.