looking for a python transform

979
6
02-03-2011 04:06 AM
FrankVignati
Occasional Contributor II
is there a python equivalent to the ITransform2D.Move method?
i am trying to move a polygon from one place to another based on a known XY coordinate
something like:

!Shape.centroid.x! = new_coordx
!Shape.centroid.x! = new_coordy
move
Tags (2)
0 Kudos
6 Replies
DanielSmith
Occasional Contributor III
Hello,


I am working on the same problem. did you by chance resolve this? if so, could you post your solution?...thanks for your time.

--Daniel
0 Kudos
FrankVignati
Occasional Contributor II
Hello,


I am working on the same problem. did you by chance resolve this? if so, could you post your solution?...thanks for your time.

--Daniel


not yet
but there is a clunky work around that can be done with model builder if your polygon starts at 0,0 xy
create a point from the polygon, add xy attributes, and two new fields x1, y1, calculate 1x and y1 to the values of point_x or point_y + values for the x,y of the location to move to
create points from the vertices of the polygon and do the same steps
then create xy event layers of the centroid point and vertices and create points from that
then create lines from the vertices points using the points to line python script esri provided
THEN create a polygon from the lines and moved centroid point

very clunky and it only works if your polygon starts at 0,0 xy

if anyone knows a better way please post it up
0 Kudos
LoganPugh
Occasional Contributor III
It's not too difficult to set the coordinates of a polygon feature in Python. Here is an example: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Writing_geometries/002z0000001v000000/

Instead of setting hard-coded coordinates, just apply a translation (add or subtract a distance) to each of the X and Y components of each of the existing coordinates in each polygon.

Here's another example for multi-part polygons: http://forums.arcgis.com/threads/20158-Creating-a-multipolygon-polygon
0 Kudos
ChrisSnyder
Regular Contributor III
How about building a custom spatial reference that is the same as your standard one but just with different FalseEasting and FalseNorthing values?

Then you could use this custom SR in a cursor (or just the project command for that matter)...
0 Kudos
LoganPugh
Occasional Contributor III
Here's a function you can try within an UpdateCursor. Set the shape of the row to the return value of this function and call updateRow. Written for arcgisscripting so it should work with both 9.3 and 10.

def moveGeomToPoint(geometry, x, y):
    """Returns a geometry whose coordinates are shifted such that the new centroid is located at the coordinates x and y"""
    # Get the current centroid
    oldCentroid = geometry.TrueCentroid

    # Get the delta x and y factors
    dX = x - oldCentroid.x
    dY = y - oldCentroid.y

    # Print details of translation
    print "Current Centroid: (%.12F, %.12F)" % (oldCentroid.x, oldCentroid.y)
    print "New Centroid: (%.12F, %.12F)" % (x, y)
    print "dX, dY: (%.12F, %.12F)" % (dX, dY)

    # Check if geometry is point first
    if geometry.type == "point":
        # Get the existing point object
        pnt = geometry.GetPart()

        # Create a new point object and assign to it the new translated coordinates
        newPnt = gp.CreateObject("Point")
        newPnt.x = pnt.x + dX
        newPnt.y = pnt.y + dY

        # Return the new point instead of going through all the parts
        return newPnt

    # Keep track of where we are in the geometry array
    partnum = 0
    partcount = geometry.PartCount

    # Create a new geometry array -- this is what we are ultimately going to return
    newGeom = gp.CreateObject("Array")

    # Loop through all of the parts in the geometry array
    while partnum < partcount:
        print "Part " + str(partnum) + ":"
        part = geometry.GetPart(partnum)

        # Create a new array just for the part we are currently on
        newPart = gp.CreateObject("Array")

        # Get the first vertex of the current part
        pnt = part.Next()
        pntcount = 0

        # Enter while loop for each vertex
        while pnt:
            # Print x,y coordinates of current point and new point
            oldX = pnt.x
            oldY = pnt.y
            newX = oldX + dX
            newY = oldY + dY
            print "%.12F, %.12F --> %.12F, %.12F" % (oldX, oldY, newX, newY)

            # Create a new point object and assign to it the new translated coordinates
            newPnt = gp.CreateObject("Point")
            newPnt.x = newX
            newPnt.y = newY

            # Add the new point object to the new part array
            newPart.add(newPnt)

            pnt = part.Next()
            pntcount += 1

            # If pnt is null, either the part is finished or there is an interior ring
            if not pnt:
                pnt = part.Next()
                if pnt:
                    print "Interior Ring:"
        # Add the new part to the new geometry array
        newGeom.add(newPart)
        partnum += 1
    return newGeom


Example usage:
import arcgisscripting
global gp
gp = arcgisscripting.create(9.3)
fc = "c:/temp/geomTest3.shp"
rows = gp.UpdateCursor(fc)
row = rows.Next()
while row:
    row.shape = moveGeomToPoint(row.shape, 8, -8)
    rows.updateRow(row)
    row = rows.Next()
del row
del rows
0 Kudos
AdarshMadhavan
Occasional Contributor
Seems to be working.

However , If the file to be processed is non-georeferenced, will this work ?
0 Kudos