shifting features

4812
18
Jump to solution
01-15-2015 09:05 AM
WendyRolston
New Contributor

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.

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

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.

View solution in original post

18 Replies
DarrenWiens2
MVP Honored Contributor

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.

WendyRolston
New Contributor

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

0 Kudos
DarrenWiens2
MVP Honored Contributor

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.

WendyRolston
New Contributor

That totally did the trick.  Thanks for helping the newbie.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

DarrenWiens2
MVP Honored Contributor

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.

0 Kudos
DanPatterson_Retired
MVP Emeritus

hahaha ... you have never taught then

WendyRolston
New Contributor

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?

0 Kudos
DarrenWiens2
MVP Honored Contributor

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)
0 Kudos