Wednesday, November 21, 2012

Shift Those Polygons at 10.1

The arcpy team at Esri has a great blog post that shows how to shift polygons using the SHAPE@XY tag.
Here is the sample:

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

Here we have the shape column (column 0) and to access the X and Y property, you just edit the first and second objects in the X,Y pair.  The x_shift and y_shift are float values that will move the code in one direction or another.  

It's very simple sample, but it shows how you can easily move polygons.  In the past you had to adjust each vertex, but now this is done internally thanks to the new arcpy.da module at 10.1.