Home‎ > ‎Publication‎ > ‎

Shifting features using Python

In real situation, we need to use special function for shifting object to another position.
( mistaken on use of references ), right now shifting features is a available in arcpy.da module’s UpdateCursor.
By modifying the SHAPE@XY token, it modifies the centroid of the feature and shifts the rest of the feature to match.
This approach will hold for point, polyline or polygon features. To modify only a single or subset of features in a feature layer,
apply a selection to that layer and pass the layer in as the input to shift_features.
Word of caution, this is using UpdateCursor, so features will be permanently modified.
So as caution you need to back up your data if you may want to reverse the updates.

import arcpy

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)]])


Just try to copy and paste definition statement code function shift_features above in python window, and excecute shifting feature shift_features(“<feature name>”,.1,.1),

And feature shifted as you refresh map display.

After shifted
After shifted