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.
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 = 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
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
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)
Sorry, I am just not getting it. I have two fields. One is called x_shift and one is called y_shift. Both float fields. Each currently have a value of 200. I keep getting an error.
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)
EDIT
Never mind...need the error message please
Hi Dan,
I am trying to shift points using the function shift_features, but i am getting this error:
Runtime error Traceback (most recent call last): File "<string>", line 10, in <module> File "<string>", line 5, in shift_featuresAttributeError: 'module' object has no attribute 'da_UpdateCursor'
import arcpy in_features =r"B14_20140416_Mornings_UTM32" def shift_features(in_features): with arcpy.da_UpdateCursor(in_features, ['SHAPE@XY','NEAR_X','NEAR_Y']) 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)
I am working on a FileGDB with ArcGIS 10.3
NEAR_X and NEAR_Y are attributes of B14_20140416_Mornings_UTM32"
Thanks for the help
I got a new error:
Runtime error Traceback (most recent call last): File "<string>", line 7, in <module> File "<string>", line 4, in shift_featuresTypeError: sequence size must match size of the row
Angela, it is working from a table in arcmap and not a file on disc and you need the associated other fields in the table besides the shapefield. You would provide the function with the layer's name in the shift_features call,
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.