2

I have a large number of polygons with matching points. I wish to move each respective polygon to its corresponding point (see attached image). I can't use the 'Move To' tool in the 'Editor', as I have too many polygons and points for this to be viable. Is there a way to do this with ArcPy or similar?

enter image description here

4

I assume you have some common id to link each polygon to Point. Then you can read each id and point xy coordinates as a Dictionary using da.SearchCursor. Then use the Dictionary with da.UpdateCursor to move polygons:

import arcpy

arcpy.env.workspace = r'C:\Default.gdb'
polys = 'areas'
points = 'areas_FeatureToPoint'
common_id_column = 'idcolumn'

d =  {idval:centroidxy for idval,centroidxy in arcpy.da.SearchCursor(points, [common_id_column,'SHAPE@XY'])} #Store Point id and centroid coordinates in a dictionary

with arcpy.da.UpdateCursor(polys,[common_id_column,'SHAPE@XY']) as cursor:
    for row in cursor:
        if row[0] in d:
            row[1] = d[row[0]]
            cursor.updateRow(row)
        else:
            print "Could not find polygon with point id {0}".format(row[0])

enter image description here

|improve this answer|||||
1

A starting point in arcpy could be this script, it overwrites the centroid of the polygon: https://arcpy.wordpress.com/2012/11/15/shifting-features/

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.

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

    return

If you have a equal ID field in the points and polygons features you could maybe rewrite the updateRow of the centroid like this:

def shiftPoly(inPoints, inFcS, inFcSL):

    arcpy.MakeFeatureLayer_management(inFcS, inFcSL)

    with arcpy.da.SearchCursor (inPoints, ['SHAPE@XY', 'ID']) as sCurs:
        for row in sCurs:
            print("{0}: {1}, {2}".format(row[1], row[0][0], row[0][1]))

            myX = row[0][0]
            myY = row[0][1]

            arcpy.SelectLayerByAttribute_management(inFcSL, "NEW_SELECTION", 
                                        "ID = {}".format(row[1]))

            with arcpy.da.UpdateCursor(inFcSL, ['SHAPE@XY']) as cursor:
                for row in cursor:
                    cursor.updateRow([[myX, myY]])

    return
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.