Shifting features

Shifting (or moving) features is a snap using the 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, back up your data if you may potentially want to reverse the updates.

import arcpy

def shift_features(in_features, x_shift=None, y_shift=None):
    Shifts features by an x and/or y value. The shift values are in
    the units of the in_features coordinate system.

    in_features: string
        An existing feature class or feature layer.  If using a
        feature layer with a selection, only the selected features
        will be modified.

    x_shift: float
        The distance the x coordinates will be shifted.

    y_shift: float
        The distance the y coordinates will be shifted.

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


Zonal Peak

Here is a short Python recipe for locating the highest point (peak) in an elevation grid or raster for a set of zones:

import arcpy
from import *

def main(zones, zone_field, depth_raster, output_points):
    """Find the highest points for each raster zone."""
    arcpy.env.overwriteOutput = True

    # Get highest (max) value for each zone.
    arcpy.env.cellSize = depth_raster
    arcpy.env.snapRaster = depth_raster
    zonal_raster = ZonalStatistics(zones, zone_field, depth_raster, "MAXIMUM")

    # Retrieve all cells in depth raster equal to zonal raster values (max values).
    con_raster = Con(Raster(depth_raster) == zonal_raster, depth_raster)

    # Convert the raster cells to point features - output the highest locations.
# End main function

if __name__ == '__main__':
    # Retrieve input parameter values and run main.
    in_zones = arcpy.GetParameterAsText(0)
    zone_id_field = arcpy.GetParameterAsText(1)
    elev_raster = arcpy.GetParameterAsText(2)
    out_points = arcpy.GetParameterAsText(3)
    main(in_zones, zone_id_field, elev_raster, out_points)

To find the lowest (deepest) point in each zone, change the keyword “MAXIMUM” to “MINIMUM”.