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.
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.
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?
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
That totally did the trick. Thanks for helping the newbie.
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 = 100y_shift = 100shift_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
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.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.