How To: Add start and end z-values to a line feature using Python script


There is no direct way to insert the start and end elevation of a line feature as attribute values. The Interpolate Shape tool can only add z-values to the feature geometry, and the Add Surface Information tool can only add the mean elevation of a feature by defining the elevation surface. However, it is possible to add the start and end z-values to a line feature as attribute values by combining the Interpolate Shape tool and a Python script.


The instructions provided describe how to add start and end z-values to a line feature.

  1. Run the Interpolate Shape tool to add the z-values vertices to the desired line features. Input the elevation data into the Input Surface field, and select the desired line feature from the Input Feature Class drop-down.

    The image of the Interpolate Shape tool.
  2. Open the attribute table of the newly created line feature, and add two new fields with Float or Double data type for the start and end z-values. For more information on adding fields, refer to the following web help document, ArcGIS Desktop: Adding fields.

    Image of newly created Z fields.
  3. Run the following script through the Python console (Geoprocessing > Python.)
    import arcpy
    #Specify the desired workspace.
    input_fc = r'<workspace>\<line_feature>'
    #Modify this part according to the specified field names created in step 2.
    myfield1 = "Z_Start"
    myfield2 = "Z_End"
    myshape = "SHAPE@"
    #Iterate between rows available in the attribute table of the line feature and input the z-values.
    with arcpy.da.UpdateCursor(input_fc, (myshape, myfield1, myfield2)) as cursor:
        for row in cursor:
            geom = row[0]
            startpt = row[0].firstPoint
            endpt = row[0].lastPoint
            row[1] = round(startpt.Z, 2)
            row[2] = round(endpt.Z, 2)

    The image of the sample script.
  4. The result is populated in the two new fields created in Step 2.

    Image of populated Z fields.

Related Information