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.
- 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.
- 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.
- 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 startpt = row.firstPoint endpt = row.lastPoint row = round(startpt.Z, 2) row = round(endpt.Z, 2) cursor.updateRow(row)
- The result is populated in the two new fields created in Step 2.
- ArcGIS Desktop: Interpolate Shape
- ArcGIS Desktop: Add Surface Information
- ArcGIS Desktop: Using the Python window
- How To: Enable, attribute, and edit features with z-values