I am totally new to script writing and am trying to get the Shifting Features code to work from ArcPy Café. I can't see what I am doing wrong.
Solved! Go to Solution.
That's my fault. Move the function above the call to the function and it should work. I edited my post above.
Also, just a note about your script, you set a value of 100 for x and y shift, and then set both to None in the function definition. So, I would not expect any shift to be observed as it is.
You need to actually call the shift_features function. As it is, the script executes line 1, then executes line 3, then stops. Everything between 'def' and 'return' is a function just waiting to be called. Your script should look like this:
import arcpy in_features = r"C:/Temp/CP_C06.shp" x_shift = 20 # may as well shift by some number y_shift = 50 # ditto def shift_features(in_features, x_shift, y_shift): with arcpy.da_UpdateCursor(in_features, ['SHAPE@XY']) as cursor: for row in cursor: cursor.updateRow([[row[0][0] + (x_shift or 0), row[0][1] + (y_shift or 0)]]) return shift_features(in_features, x_shift, y_shift)
Also, please read "Posting Code Blocks in the New GeoNet" so we can copy/paste your code to help.
Using PythonWin gives me the following error.
NameError: name 'shift_features' is not defined. Below is the code that I used.
import arcpy
in_features = r"C:/Temp/CP_C06.shp"
x_shift = 100
y_shift = 100
shift_features(in_features, x-shift, y_shift)
def shift_features (in_features, x_shift=None, y_shift=None):
with arcpy.da.UpdateCursor(in_features, ['SHAPE@XY']) as cursor:
for row in cursor:
cursor.updateRow([[row[0][0] + (x_shift or 0),
row[0][1] + (y_shift or 0)]])
return
That's my fault. Move the function above the call to the function and it should work. I edited my post above.
Also, just a note about your script, you set a value of 100 for x and y shift, and then set both to None in the function definition. So, I would not expect any shift to be observed as it is.
That totally did the trick. Thanks for helping the newbie.
nice...Darren...although it would be wise to recommend that users experiment on a temporary file or use copyfeatures_management to clone an existing one prior to doing anything with shape geometry...a bad change in geometry can render a file useless
I figure if you're clever enough to play around with Python, you should be clever enough to make a backup or suffer the consequences.
hahaha ... you have never taught then
Hi Darren. What if I want to pull the x-shift value and y-shift value from a field in my shapefile instead of putting a value into the code sequence?
You can access field values by including them in the UpdateCursor. The list, ['SHAPE@XY','X_SHIFT','Y_SHIFT'] (change X_SHIFT and Y_SHIFT to your field names), translates to row[0], row[1], and row[2], respectively.
import arcpy in_features = r"C:/Temp/CP_C06.shp" def shift_features(in_features): with arcpy.da_UpdateCursor(in_features, ['SHAPE@XY','X_SHIFT','Y_SHIFT']) as cursor: for row in cursor: cursor.updateRow([[row[0][0] + (row[1] or 0), row[0][1] + (row[2] or 0)]]) return shift_features(in_features)