5

Trying to update Z values onto some point and line features from the level attributes on each feature. My script works fine on points - the previous Z values of 0 now have the same values as the level field. It doesn't work on my line features though.

I guess I can't write to the firstPoint.Z and lastPoint.Z on each line, however it is giving no errors at all (the script exits successfully).

pointFields = ['SHAPE@Z', 'Level']
lineFields = ['SHAPE@', 'FromLevel', 'ToLevel']

with arcpy.da.UpdateCursor(pointFC, pointFields) as cursor:
    for row in cursor:
        row[0] = row[1]
        cursor.updateRow(row)

with arcpy.da.UpdateCursor(lineFC, lineFields) as cursor:
    for row in cursor:
        row[0].firstPoint.Z = row[1]
        row[0].lastPoint.Z = row[2]
        cursor.updateRow(row)

Not all of my lines are 2-point lines, but I only want to update the first and last vertex of each line with the FromLevel and ToLevel values from the attribute table. I'm trying to modify the code from How to update the Z value using an UpdateCursor? however I am trying to figure out how to update only the first and last points of my line.

Here is what I've attempted based on one of the answers from the other Q&A

with arcpy.da.UpdateCursor(lineFC, lineFields) as cursor:
    for row in cursor:
        newGeom = arcpy.Array()
        for part in row[0].getPart():
            newPart = arcpy.Array()
            for pnt in part:
                if pnt != None:
                    if pnt == row[0].firstPoint:
                        newPnt = arcpy.Point(pnt.X, pnt.Y, row[1])
                    elif pnt == row[0].lastPoint:
                        newPnt = arcpy.Point(pnt.X, pnt.Y, row[2])
                    else:
                        newPnt = arcpy.Point(pnt.X, pnt.Y, 0)
                    newPart.add(newPnt)
            newGeom.add(newPart)
        newShape = arcpy.Polyline(newGeom)
        row[0] = newShape
        cursor.updateRow(row)

But still it completes without error yet no Z values are present.

How can I update only the first and last vertices of my line with the Z values from attributes?


It does appear that if pnt == row[0].firstPoint: isn't working. I did a print row[0].firstPoint and a print pnt and they return exactly the same values, yet it's not going into that if

print "First Point = {}".format(row[0].firstPoint)
print "Pnt = {}".format(pnt)

returns

First Point = 1875883.117 5826666.937 0 NaN
Pnt = 1875883.117 5826666.937 0 NaN

Yet it still hits the else.

1

1 Answer 1

6

I think you need to fix your new shape line to contain z values.

newShape = arcpy.Polyline(newGeom, None,  True, False)

The default is false

http://pro.arcgis.com/en/pro-app/arcpy/classes/polyline.htm

I also had this as my if statement for the first point

if (row[0].firstPoint.X, row[0].firstPoint.Y) == (pnt.X, pnt.Y):

and last point

if (row[0].lastPoint.X, row[0].lastPoint.Y) == (pnt.X, pnt.Y):
1
  • Thanks JamesLeversha, forcing the Z Values into my new shape has done the trick
    – Midavalo
    Jul 6, 2016 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.