Skip to main content
added pic
Source Link
Paul
  • 11.6k
  • 1
  • 30
  • 47

You don't need to be updating row[1] for every feature, so cursor.updateRow(row) should be under the final else statement. Also, I'd suggest using with statements as closing is better supported:

fc = r"C:\Points\Test.gdb\Points_3d"     
with arcpy.da.UpdateCursor (fc, ["Elevation","Slope"]) as cursor:
    firstRun = True 

    for row in cursor:
       if firstRun:
          oldValue = row[0]
          firstRun = False
       else:
        #We don't need to check less than since we aren't negating it.
          if oldValue > row[0]:
             row[1] *= -1
             cursor.updateRow(row)
          oldValue = row[0]

I checked similar code on a sample dataset (see below) and it correctly negated the features based on the if statement. If this still isn't working, I'd check to see if your if statement ever evaluates to True.

enter image description here

You don't need to be updating row[1] for every feature, so cursor.updateRow(row) should be under the final else statement. Also, I'd suggest using with statements as closing is better supported:

fc = r"C:\Points\Test.gdb\Points_3d"     
with arcpy.da.UpdateCursor (fc, ["Elevation","Slope"]) as cursor:
    firstRun = True 

    for row in cursor:
       if firstRun:
          oldValue = row[0]
          firstRun = False
       else:
        #We don't need to check less than since we aren't negating it.
          if oldValue > row[0]:
             row[1] *= -1
             cursor.updateRow(row)
          oldValue = row[0]

I checked similar code on a sample dataset and it correctly negated the features based on the if statement. If this still isn't working, I'd check to see if your if statement ever evaluates to True.

You don't need to be updating row[1] for every feature, so cursor.updateRow(row) should be under the final else statement. Also, I'd suggest using with statements as closing is better supported:

fc = r"C:\Points\Test.gdb\Points_3d"     
with arcpy.da.UpdateCursor (fc, ["Elevation","Slope"]) as cursor:
    firstRun = True 

    for row in cursor:
       if firstRun:
          oldValue = row[0]
          firstRun = False
       else:
        #We don't need to check less than since we aren't negating it.
          if oldValue > row[0]:
             row[1] *= -1
             cursor.updateRow(row)
          oldValue = row[0]

I checked similar code on a sample dataset (see below) and it correctly negated the features based on the if statement. If this still isn't working, I'd check to see if your if statement ever evaluates to True.

enter image description here

Source Link
Paul
  • 11.6k
  • 1
  • 30
  • 47

You don't need to be updating row[1] for every feature, so cursor.updateRow(row) should be under the final else statement. Also, I'd suggest using with statements as closing is better supported:

fc = r"C:\Points\Test.gdb\Points_3d"     
with arcpy.da.UpdateCursor (fc, ["Elevation","Slope"]) as cursor:
    firstRun = True 

    for row in cursor:
       if firstRun:
          oldValue = row[0]
          firstRun = False
       else:
        #We don't need to check less than since we aren't negating it.
          if oldValue > row[0]:
             row[1] *= -1
             cursor.updateRow(row)
          oldValue = row[0]

I checked similar code on a sample dataset and it correctly negated the features based on the if statement. If this still isn't working, I'd check to see if your if statement ever evaluates to True.