Select to view content in your preferred language

Python script to alter XY values for a feature

761
2
09-09-2010 03:26 PM
JosephSheffield
Deactivated User
I have a python script for Arc 10 that is attempting to alter the X and Y values for a feature in a feature class.  I am able to get the value of the points.X and point.Y and alter these values.  But, I can't figure out how to get the change to be saved in the feature class once the script is completed.

Here is the code:

# Import arcpy module
import arcpy
 
arcpy.env.overwriteOutput = True

# Script arguments
Dgb2_A = arcpy.GetParameterAsText(0)
Fault_Reconstruction = arcpy.GetParameterAsText(1)


# Local variables:
A_TIM = "C:\\Alamo_Project\\Alamo_Breccia.mdb\\Measured_Sections\\A_TIM"
numFeatures = 0
numFeatures = arcpy.GetCount_management(Fault_Reconstruction)
# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Fault_Reconstruction, "NEW_SELECTION", "[OBJECTID] = 1")
# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Dgb2_A, "INTERSECT", Fault_Reconstruction, "", "NEW_SELECTION")
# Process: Copy Features
arcpy.CopyFeatures_management(Dgb2_A, A_TIM, "", "0", "0", "0")
inputFieldX = "Fault_Movement_X"
inputFieldY = "Fault_Movement_Y"
cur = arcpy.SearchCursor(Fault_Reconstruction)
MovementX = 0
MovementY = 0
currentRow = 0
for row in cur:
    MovementX = row.getValue(inputFieldX)
    MovementY = row.getValue(inputFieldY)
    arcpy.AddMessage("X: " + str(MovementX))
    arcpy.AddMessage("Y: " + str(MovementY))
    row = cur.next()
curA = arcpy.UpdateCursor(A_TIM)
shapefieldname = arcpy.Describe(A_TIM).shapeFieldName
pnt = arcpy.Point()
newpnt = arcpy.Point()

tempX = 0
tempY = 0


for row in curA:
    feat = row.getValue(shapefieldname)
    pnt = feat.getPart()
    arcpy.AddMessage("X: " + str(pnt.X))
    arcpy.AddMessage("Y: " + str(pnt.Y))
    pnt.X = pnt.X + MovementX
    pnt.Y = pnt.Y + MovementY
    arcpy.AddMessage("X: " + str(pnt.X))
    arcpy.AddMessage("Y: " + str(pnt.Y))
    curA.updateRow(row)
0 Kudos
2 Replies
KimOllivier
Honored Contributor
To update the shape field you have to create an array and assign it to the shape in the buffer before you write the buffer out. You have created a pnt object and updated that, but have not written it back into the row. If the feature is a polyline or polygon then the object needs to be an array of arrays of points to handle multiparts.

row.feat = pnt
curA.updateRow(row)

Also in your search cursor you are mixing the old and new methods, the for loop automatically interates the cursor.

either
row = cur.next()
while row :
.... process
....row = cur.next()

or
for row in cur :
....process
0 Kudos
JosephSheffield
Deactivated User
kimo

Thanks for the input.  When I add the row.feat = pnt I get the following error:  "<type 'exceptions.RuntimeError'>: ERROR 999999: Error executing function.
Failed to execute (FaultRecTest)."


I also tried feat = pnt.  This script runs, but does not alter the location of the feature.


Thanks for pointing out that I was mixing old and new methods for the search cursor.  I've only have begun to play with python and C# code for Arc 10.
0 Kudos