[GIS] Setting the Centroid of a Set of Polygons

arcmaparcpypython

I'm trying to shift a set of rectangle polygons over an area of interest (AOI). I thought it would be as easy as setting the SHAPE@TRUECENTROID of the polygon area of interest to the shapefile/featureclass of the rectangles. When multiple polygons are selected in Arcmap, the centroid of the collection of polygons appears.

When attempting to pass the shapefile in to an update cursor, each polygon overlaps over the AOI coordinates instead. Is there a simple, Arcpy, way of sifting the entire set of polygons in a shapefile to be centered over said area of interest? Or put another way, maintain the spatial relationship each rectangle has with one another while shifting the entire set over the AOI.

I should add that I am first rotating the polygons with Arcpy. When running the script it doesn't keep the rotation expected. I would expect setting the centroid would merely shift, not rotate.

EDIT.
Here is the rotate code (this was adapted from another post on here dealing with rotating polygons)

def RotateAxis(AnchorX,AnchorY,inputx,inputy, rAngle):
        x = inputx - AnchorX
        y = inputy - AnchorY
        resultx = ((x * math.cos(rAngle)) + (y * math.sin(rAngle))) + AnchorX
        resulty = ((x * math.sin(rAngle)) - (y * math.cos(rAngle))) + AnchorY
        return (resultx,resulty)

I update the geometry from this then finally after each polygon has been rotated I attempt to perform the shift.

XC and YC are the anchor points which are the SHAPE@XY from my AOI.

Updated Code to reflect bottom picture.

memFC = "in_memory\\dissolvedPoly"
arcpy.Dissolve_management(inFC, memFC,"PageNumber")

with arcpy.da.SearchCursor(memFC, ["SHAPE@XY"]) as oFeature:
        for row in oFeature:
            memX, memY = row[0]

xShift = float(XC) - float(memX)
yShift = float(YC) - float(memY)
print "Shift x " + str(xShift) + " ; Shift y " + str(yShift)

with arcpy.da.UpdateCursor(inFC, ["SHAPE@XY"]) as cfeature:    
        for row in cfeature:
            print "Shifting polygons..."
            print ""
            print "x = " + str(x), " y = " + str(y)

            cfeature.updateRow([[row[0][0] + (xShift), row[0][1] + (yShift)]])

Before Shifting Polygons(What I start with)
Before Shift

After Shifting Polygons (What I Want)

After Shift

What I actually get.

What I Get

EDIT 2:

What I get with the shift x, shift y code.

With Shift Code

Best Answer

If you want to shift the entire feature using python, and more specifically arcpy.da.updateCursor(), you should apply the shift to the SHAPE@XY token and not to the SHAPE@TRUECENTROID token. This would need you to update each polygon indepently in a loop, but shouldn't destroy your topology if you apply exactly the same shift.

EDIT : thanks for the additional information

from what I see, you should merge your two polygons (e.g. using arcpy.dissolve_management), then compute the shift needed to place the merged polygon at the right place (X of AOI - X of merged polygon, Y of AOI - Y of merge polygon) and finally apply this shift to each individual polygon.

cursor.updateRow([[row[0][0] + (x_shift), row[0][1] + (y_shift)]])

Related Question