The problem is that I think you need to rebuild the entire line, because you are using the Shape field itself. That is because the shape field itself is referring to an actual geometry (arcpy.Polyline in this case). Also, I would recommend that you use the arcpy.da Cursors since you have version 10.2.
Here is how I would tackle this problem. Looks like you want to snap the first and last points of all lines to a single geometry (or maybe use a where clause to work on a subset of features)? First, I would start with a function that will do that:
def moveFirstAndLastPoints(in_lines, featToSnapTo, where_clause=''):
"""snaps line endpoints to a feature's first and last points
in_lines -- target lines to change
featToSnapTo -- feature to snap lines to first and last point
(should be line)
where_clause -- optional where clause to target specific features
"""
with arcpy.da.UpdateCursor(in_lines, ['SHAPE@'], where_clause=where_clause) as rows:
for r in rows:
# create array for all parts
allParts = arcpy.Array()
# iterate through each part (may or may not want to do this by part)
for part in r[0]:
# create new array to store arcpy.Polyline()
array = arcpy.Array([p for p in part])
array.replace(0, featToSnapTo.firstPoint)
array.replace(array.count-1, featToSnapTo.lastPoint)
allParts.append(array)
# now make into new line to replace geometry
rows.updateRow((arcpy.Polyline(allParts),))
return
Next, you need to get the feature you want to snap to as a Geometry. So I would do that like this:
# get a feature (first and only feature in this shapefile)
with arcpy.da.SearchCursor("testForSnapping", ['SHAPE@']) as rows:
feat = [r[0] for r in rows][0]
Then I would call the function to move the first and last points:
moveFirstAndLastPoints("lines_copy", feat)
And if you're in ArcMap, you may need to call arcpy.RefreshActiveView()
(or hit the refresh button) to see your changes. Here are screenshots of before and after in ArcMap (lines_copy
was a copy of lines_original
):
Before running the code:
And After (this is a horrible example of test geometry to use for this, but should demonstrate the point):
And here is the full code I used in ArcMap (I did not include the where clause parameter in here, but if you copy and paste the function by itself you can implement this):