Select to view content in your preferred language

Extracting X, Y, Z from line features vertices and create a point feature class

3811
3
Jump to solution
07-30-2012 07:01 AM
RickardHansen
Occasional Contributor
I'm trying to create a point feature class using vertices from a line feature class that will add the X, Y, and Z-values. I can't figure out how to get the Z-values to the point feature. I manage to get the X and Y but the Z-values always results in 0. The ptList have Z-values from the vertices but I can't get them to the point feature. I am interested in a solution using python, not using a tool like Vertices To Points.

import arcpy  # Set the outputZFlag environment to Enabled arcpy.env.outputZFlag = "Enabled"   # Input feature class inFC = arcpy.GetParameterAsText(0) outFC = arcpy.GetParameterAsText(1)   # Create describe desc = arcpy.Describe(inFC) shapefieldname = desc.ShapeFieldName inFCDataType = desc.DataType   # Create point list ptList = []   # Create cursor and iterate through objects in feature class rows = arcpy.SearchCursor(inFC) for row in rows:     # Create the geometry object "feat"     feat = row.getValue(shapefieldname)     partnum = 0     # Step through each part of the feature and append vertice coordinates to point list     for part in feat:         part_list = []         for pnt in feat.getPart(partnum):           ptList.append([pnt.X, pnt.Y, pnt.Z])         partnum += 1   # Create point object, point geometry list and iterate through list # and append every coordinate pair to geometry list.  pt = arcpy.Point() ptGeoms = [] for p in ptList:   pt.X = p[0]   pt.Y = p[1]   pt.Z = p[2]   ptGeoms.append(arcpy.PointGeometry(pt))   # Create point feature class arcpy.management.CopyFeatures(ptGeoms, outFC)   # Add XY to point feature class arcpy.management.AddXY(outFC)


Any help would be greatly appreciated!
Thanks,
Rickard
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RickardHansen
Occasional Contributor
I have found a solution that take a different perspective on the problem. In my first try I created a point object, point geometry list and iterated through list and appended every coordinate pair to geometry list. That didn't pass through the z values though.

Now I create a table and add appropiate fields, use an insertCursor, iterate through the point list and pass X, Y and Z values in to the correct fields. From there I create an XYEvent layer and finally copy to a point feature layer. Works great!

# Import arcpy import arcpy  # Set the outputZFlag environment to Enabled arcpy.env.outputZFlag = "Enabled"  # Input feature class inFC = arcpy.GetParameterAsText(0) outFolder = arcpy.GetParameterAsText(1) outFC = arcpy.GetParameterAsText(2)  # Set workspace arcpy.env.workspace = outFolder  # Create describe desc = arcpy.Describe(inFC) shapefieldname = desc.ShapeFieldName inFCCoordinateSystem = desc.SpatialReference inFCDataType = desc.DataType  # Create point list ptList = []  # Create cursor and iterate through objects in feature class rows = arcpy.SearchCursor(inFC) for row in rows:     # Create the geometry object     feat = row.getValue(shapefieldname)     partnum = 0     # Step through each part of the feature and append vertice coordinates to point list     for part in feat:         part_list = []         for pnt in feat.getPart(partnum):           ptList.append([pnt.X, pnt.Y, pnt.Z])         partnum += 1  # Create table and add fields x, y, z arcpy.management.CreateTable(outFolder, "XY_Table") arcpy.management.AddField("XY_Table", "X", "DOUBLE") arcpy.management.AddField("XY_Table", "Y", "DOUBLE") arcpy.management.AddField("XY_Table", "Z", "DOUBLE")  # Create cursor object cur = arcpy.InsertCursor("XY_Table")  # Iterate through ptList and insert a new row in table for every coordinate pair in ptList for pt in ptList:     row = cur.newRow()     row.X = pt[0]     row.Y = pt[1]     row.Z = pt[2]     cur.insertRow(row)  # Create XY event layer arcpy.management.MakeXYEventLayer("XY_Table", "X", "Y", "XY_Table_Event") inTableEvent = "XY_Table_Event"  # Create point feature class arcpy.management.CopyFeatures(inTableEvent, outFC)  # Delete event layer and table arcpy.management.Delete(inTableEvent) arcpy.management.Delete("XY_Table")  # Create describe on out data desc2 = arcpy.Describe(outFC) outFCSpatialRef = desc2.SpatialReference outFCSpatialRefName = outFCSpatialRef.name  # Check if out data has spatial reference, # if not, project in data spatial reference on out data if outFCSpatialRefName == "Unknown":     arcpy.management.DefineProjection(outFC, inFCCoordinateSystem) else:     pass

View solution in original post

0 Kudos
3 Replies
FabianBlau
Deactivated User
Maybe you have to set hasZ:

  ...
  pt.Z = p[2]
  ptGeom = arcpy.PointGeometry(pt)
  ptGeom.hasZ = True
  ptGeoms.append(ptGeom)


I did not try it.
0 Kudos
RickardHansen
Occasional Contributor
Unfortunately it doesn't work. The feature class created already have hasZ = True. The output feature class includes X and Y coordinates as well as a column for Z-values, but that only has 0 (zero) as opposed to the correct values. When I check ptList in Python all the z-values are there, but I don't manage to get them to the feature class.

Rickard
0 Kudos
RickardHansen
Occasional Contributor
I have found a solution that take a different perspective on the problem. In my first try I created a point object, point geometry list and iterated through list and appended every coordinate pair to geometry list. That didn't pass through the z values though.

Now I create a table and add appropiate fields, use an insertCursor, iterate through the point list and pass X, Y and Z values in to the correct fields. From there I create an XYEvent layer and finally copy to a point feature layer. Works great!

# Import arcpy import arcpy  # Set the outputZFlag environment to Enabled arcpy.env.outputZFlag = "Enabled"  # Input feature class inFC = arcpy.GetParameterAsText(0) outFolder = arcpy.GetParameterAsText(1) outFC = arcpy.GetParameterAsText(2)  # Set workspace arcpy.env.workspace = outFolder  # Create describe desc = arcpy.Describe(inFC) shapefieldname = desc.ShapeFieldName inFCCoordinateSystem = desc.SpatialReference inFCDataType = desc.DataType  # Create point list ptList = []  # Create cursor and iterate through objects in feature class rows = arcpy.SearchCursor(inFC) for row in rows:     # Create the geometry object     feat = row.getValue(shapefieldname)     partnum = 0     # Step through each part of the feature and append vertice coordinates to point list     for part in feat:         part_list = []         for pnt in feat.getPart(partnum):           ptList.append([pnt.X, pnt.Y, pnt.Z])         partnum += 1  # Create table and add fields x, y, z arcpy.management.CreateTable(outFolder, "XY_Table") arcpy.management.AddField("XY_Table", "X", "DOUBLE") arcpy.management.AddField("XY_Table", "Y", "DOUBLE") arcpy.management.AddField("XY_Table", "Z", "DOUBLE")  # Create cursor object cur = arcpy.InsertCursor("XY_Table")  # Iterate through ptList and insert a new row in table for every coordinate pair in ptList for pt in ptList:     row = cur.newRow()     row.X = pt[0]     row.Y = pt[1]     row.Z = pt[2]     cur.insertRow(row)  # Create XY event layer arcpy.management.MakeXYEventLayer("XY_Table", "X", "Y", "XY_Table_Event") inTableEvent = "XY_Table_Event"  # Create point feature class arcpy.management.CopyFeatures(inTableEvent, outFC)  # Delete event layer and table arcpy.management.Delete(inTableEvent) arcpy.management.Delete("XY_Table")  # Create describe on out data desc2 = arcpy.Describe(outFC) outFCSpatialRef = desc2.SpatialReference outFCSpatialRefName = outFCSpatialRef.name  # Check if out data has spatial reference, # if not, project in data spatial reference on out data if outFCSpatialRefName == "Unknown":     arcpy.management.DefineProjection(outFC, inFCCoordinateSystem) else:     pass
0 Kudos