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
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "C:\Student\Script_fieldname.py", line 13, in <module>
shift_features(in_features)
File "C:\Student\Script_fieldname.py", line 9, in shift_features
cursor.updateRow([[row[0][0] + (row[1] or 0), row[0][1] + (row[2] or 0)]])
TypeError: sequence size must match size of the row
>>>
I'm working on a project that, in the past, I ran a move calculation [SHAPE].Move([fieldname],0) in arc3.3. I work in ArcGIS 10, but use the oldie for a couple of tasks. We have upgraded and now don't have the use of arc3.3 anymore. I need to find a (modern) way to shift polygon features way out of their original spatial placement and then back again. Arcpy Café "shifting features" coding looked promising, but way out of my wheelhouse. So I am here begging for help. I did some Python tutorials, but am just not getting it.
Wendy
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.