2

I would like to calculate the slope value in %, but I don’t want to have any negative values. If there are negative values in the slope field, I would like to multiple them by -1 and if not, multiple them by +1 (or do nothing).

My input is a line shapefile. First I added a field called "slope", then I calculate the slope based on other fields.

enter image description here

arcpy.AddField_management(ShapeFileLine, "Slope", "DOUBLE", 10, 3)
arcpy.CalculateField_management(ShapeFileLine, "Slope", '((((!END_Z! - !START_Z!)/!LENGTH_3D!))*100)!',"PYTHON")

What I should do if I want I only have positive values? In this case they are all negatives, but sometimes the positive and negative values are mixed

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Oct 19, 2018 at 14:19
0

3 Answers 3

5

I agree with @smiller answer about how to do an If Else in a code block.

However, in this particular case, you could also just use the absolute value function which will always return the positive version of a number, regardless if the input is positive or negative https://docs.python.org/2/library/functions.html#abs

Which means it would look something like:

arcpy.CalculateField_management(ShapeFileLine, "Slope", 'abs((((!END_Z! - !START_Z!)/!LENGTH_3D!))*100)',"PYTHON")

(FYI: It looked like you had an extra ! at the end of your code block originally, so I removed that because it looked like a miss-type.)

answered Oct 19, 2018 at 14:49
1
  • yes. you are right! I have tested both of the suggestions. they worked.@smiller @John Commented Oct 22, 2018 at 7:25
3

Include a codeblock to use if/else.

expression = "posSlope(!Slope!)"
codeblock = """def posSlope(slope):
 if slope < 0:
 return slope * -1
 else:
 return slope""" 
arcpy.CalculateField_management(ShapeFileLine, "Slope", expression, "PYTHON_9.3", codeblock)

Or if you wanted to do it all at once (I assume startz/endz are both float):

expression = "calcSlope(!END_Z!, !START_Z!, !LENGTH_3D!)" 
codeblock = """def calcSlope(endz, startz, length3d):
 slope = ((endz - startz)/length3d) * 100 
 if slope < 0:
 return slope * -1
 else:
 return slope""" 
arcpy.CalculateField_management(ShapeFileLine, "Slope", expression, "PYTHON_9.3", codeblock)

Another example: https://community.esri.com/thread/87299

answered Oct 19, 2018 at 14:31
1
  • it works. but the quotations needs to be edited. Commented Oct 22, 2018 at 7:24
2

Just take the absolute value. For example, using an UpdateCursor in arcpy:

import arcpy
fc = r'C:/path/to/your.gdb/featureclass'
with arcpy.da.UpdateCursor(fc, "Slope") as cursor:
 for row in cursor
 row[0] = abs(row[0])
 cursor.updateRow(row)
answered Oct 22, 2018 at 12:20

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.