Friday 3 May 2019

arcpy - updating Z coordinates of points in polylines


I have a polyline feature dataset with Z coordinates. I'd like to change the Z values (e.g.) by adding 10 meters.



I am trying the following code, but no changes happen. It seems the code is able to enter each part of each polyline and print the Z values plus 10, but I don't know what I have to do to update the Z coordinates.


Here is the code:


import arcpy

gdb = r'C:\Umberto\network_analyst\umbeNETWORK\OfficeBuilding.gdb'
feature = gdb + r'\Transportation\FloorLines'
fields = ["Floor_Number", "SHAPE@"]

# start edit session
edit = arcpy.da.Editor(gdb)

edit.startEditing()
edit.startOperation()

# Create field name with the proper delimiters
#
whereclause = """%s = 3""" % arcpy.AddFieldDelimiters(feature, fields[0])

with arcpy.da.UpdateCursor(feature,fields, whereclause) as cursor:
for row in cursor:
polyline = row[1]

for part in polyline:
for pnt in part:
pnt.Z = pnt.Z + 10
print pnt.Z
cursor.updateRow(row)

# stop esit session
edit.stopOperation()
edit.stopEditing(True)

Answer




Using the explode_to_points=True argument into updateCursor as suggested in this link did the work. Thanks to @klewis for the comment.


However, because the updateCursor in my case works with two fields, I had to specify two arguments in the final updateRow call, as row[0] refers to the filed Floor_Number, while row[1] is the Z coordinate I need to change.


The working code is:


import arcpy

gdb = r'\path\to\my.gdb'
feature = gdb + r'\my_polyline'
fields = ["Floor_Number", "SHAPE@Z"]

# start edit session

edit = arcpy.da.Editor(gdb)
edit.startEditing()
edit.startOperation()

# Create field name with the proper delimiters
#
whereclause = """%s = 3""" % arcpy.AddFieldDelimiters(feature, fields[0])

# this is the increment of the Z coordinate (in meters, change as you need)
z_increase = -200


with arcpy.da.UpdateCursor(feature,fields, whereclause, explode_to_points=True) as cursor:
for row in cursor:
print row[1]
cursor.updateRow([row[0], row[1] + z_increase])

# stop edit session
edit.stopOperation()
edit.stopEditing(True)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...