Hello,
I am attempting to use update search cursor to calculate the slope of a line. I am using SHAPE@Z to get the elevation of a point along a line. I am trying to get specific point information using queryPointAndDistance OR positionAlongLine, but I don't know which to use.
For my purposes I need the first point, middle point and end point to calculate the side slopes of a stream. I am using the following script using ArcPro to get the slope for just the left side for now.
def get_slope(lines,id_field):
"""Get the slope of the transects using the first middle and last point of the line"""
with arcpy.da.UpdateCursor(lines,[id_field,'SHAPE@Z','slope']) as cursor:
for row in cursor:
# difference in 1st point and mid-point. 100 represents the length between points
row[2]=((row[1].queryPointAndDistance(0.0,TRUE))-(row[1].queryPointAndDistance(0.5,TRUE)))/100
cusor.updateRow(row)
But I keep getting errors that 'float' object has no attribute 'positionAlongLine'. I am thinking I am missing something big...
Any help would be great.
Thanks!
queryPointAndDistance (in_point, {as_percentage})
Finds the point on the polyline nearest to the in_point and the distance between those points. Also returns information about the side of the line the in_point is on as well as the distance along the line where the nearest point occurs.
You provided, hence the error... it requires a point
((row[1].queryPointAndDistance(0.0,TRUE))
row[1] is a polyline, you need to get the point on the polyline
Okay, so I have got to the point that I can access the Z values of the line and print them. How would I just select the first point and the middle point using this script?
def get_slope(lines,id_field):
"""Get the slope of the transects using the first middle and last point of the line"""
with arcpy.da.UpdateCursor(lines,[id_field,'SHAPE@','SHAPE@Z','slope']) as cursor:
for row in cursor:
geom=row[1]
for part in geom:
for pnt in part:
print(pnt.Z)
If you read the help file for the update cursor what does it say about SHAPE@Z?
A double of the feature's z-coordinate.
A double is a float, so you are asking a geometry processing function queryPointAndDistance on a number not a geometry object. If you want to query a geometry object you need to return it using SHAPE@.
Understood. So you are saying that this same equation is possible using the SHAPE@ or does my code need additional lines? Running the code with the SHAPE@ returns an error that TRUE is not defined?