Unfortunately you can't directly assign new values to the existing geometry of a feature - rather, you have to create a new geometry object, and update the shape field of the feature with that new object. Fortunately, the array objects have a
replace method. So rather than trying to directly modify the X coord of the point inside the array, you need to:
- Create a new
arcpy.Point object with the correct coordinates (looks like you might have done this already)
- Get a copy of the array object stored in the row's Shape field
- Use the
replace method to set the desired point in your array with your modified point
- Make a new Polyline object with that array
- Use the row object's
setValue method to update the Shape field with your new, correct Polyline
- Use the cursor object's
updateRow method to insert the changed row into the dataset.
for r in cur:
ary = r.getValue("SHAPE").getPart(0)
ary.replace(0,correct_point_object) # first arg 0 replaces the first point in the line
newLine = arcpy.Polyline(ary)
replace method takes an index and a value. Unfortunately it doesn't accept e.g. -1 as an index to the last point in the array. However you can say
It looks like you're precomputing the X-coordinates somewhere else and retrieving them later. If this is the case, I'd probably go the whole hog and create new Polyline objects with the correct points for each line while you're computing the correct coordinates. This will likely be easier & cleaner. That way your code could be more like
row_num = 0
for r in cur:
row_num += 1
Which, at least for me, is a bit more clear... but that's stylistic!
Edit to add:
I couldn't fit this in a comment. Without seeing your code it's hard to tell where it might be falling over. Here's a complete tested script which works for me. Hopefully it will serve as a reference. Note that here I'm calculating the new geometry directly from the old one, rather than doing two passes; that may or may not be possible depending on how you're doing your snap position calculations. Also this time I'm constructing a brand new array based on the old one rather than using the
replace method, in case that's necessary.
"""Trivial function to offset a point - replace with what you're
new_point = arcpy.Point(old_point.X+X_distance,
"""Takes a Polyline geometry object and returns a new Polyline with
the first point of the first part offset by the distance given."""
array = line_geom.getPart(0)
first_point = array
new_point = offsetPoint(first_point,X_distance,Y_distance)
# Build a new array with your new point in the 0th position, and
# the rest of the points from the old array.
new_array = arcpy.Array([new_point]+
[array.getObject(x) for x in range(1,array.count)])
# Then make a new Polyline object with that array.
new_line = arcpy.Polyline(new_array)
fc = r"C:\Users\student\Documents\ArcGIS\Default.gdb\SomeStorms"
cur = arcpy.UpdateCursor(fc)
for r in cur:
geom = r.getValue("SHAPE")
Hopefully that helps clear it up.