I have lines representing streams that have z values, unfortunately not all the z values flow downhill, there are points within the line that flow uphill, is there a way in python to find these points and flatten them out?
Do you have access to the original dem from which these stream lines were derived?
Because I think the best solution would be to create a hydrologically correct dem first, then derive the stream lines, using the work flow outlined here :
A python approach would be fraught with difficulties like Z this side and that of a particular point. Which is "correct"?
Do the lines at least go in the correct direction ie down stream? Not saying that you would get a programmatic solution here though.
Would be difficult in my estimation.
Replace "InvalidLine" to your featureclass' name. It's dirty but working.
#For singlepart polylines only!
#Polyline must be digitised downstream! (StartPoint's Z value must be higher than End Point's Z value!)
with arcpy.da.UpdateCursor("InvalidLine", "SHAPE@") as features:
for feature in features:
newarray = arcpy.Array()
lastZ = 9999999.99
for part in feature:
for points in part:
for point in points:
if point.Z <= lastZ:
lastZ = point.Z
newpoly = arcpy.Polyline(newarray, None, True)
This approach will not correct Z values of existing points, but "remove" those points that have a Z value that is not below the previous Z value. This means that your output XY locations of the line will be different (it will loose details).
You may want to use a different approach as outlined here: https://community.esri.com/thread/181945-interpolate-missing-z-values-in-polyline
It interpolates the invalid Z values using the M position of the vertice on the line and using the valid Z values. You will need to adapt it a bit to recognize and mark the invalid z values.
Thank you all for your input, the code scripts are helpful, I had started editing Akos code to address deleting vertices, I'm not fast at python, but when I get it working will post code here.