3

I have a table with a set of original XY values and a set of new XY values. I want to create new points of the new xy's using the insert cursor. My code updates the ID and Sp row but not the XY row.

for fc in arcpy.ListFeatureClasses('*Clip_1'):

  X = 'X1'
  Y = 'Y1'
  ID ='Sp'
  xy = []

  with arcpy.da.SearchCursor(fc, [X, Y, ID]) as Cursor:
    for row in Cursor:
        xy = [(row[2],(row[0],row[1]))]
        cursor = arcpy.da.InsertCursor(fc,['Sp','SHAPE@XY'])
        for new in xy:
            cursor.insertRow(new)
            print str(new)
  del row, xy

my output table looks like:

ID   Sp    X    Y    X1   Y1
1    21    25   25   21   19
2    22    12   29   12   18
3    23    14   18   22   10
4    21    
5    22    
6    23    

What I want is

ID   Sp    X    Y    X1   Y1
1    21    25   25   21   19
2    22    12   29   12   18
3    23    14   18   22   10
4    21    21   19
5    22    12   18
6    23    22   10

Is there a better way to do this?

  • 1
    You use the variable name row twice. – Vince Jul 9 at 15:42
  • Right whoops... that still did not affect the outcome – harry.p Jul 9 at 15:54
5

The token "SHAPE@XY" populates the geometries of the feature. You won't see the results in your output table. If you have fields for X and Y use their names instead of "SHAPE@XY".

with arcpy.da.InsertCursor(fc,['Sp','X', 'Y']) as iCurs:
    with arcpy.da.SearchCursor(fc, [X, Y, ID]) as sCurs:
        for row in sCurs:
            row = (row[2],row[0],row[1])
            iCurs.insertRow (row)

If you want the geometries updated as well also include "SHAPE@XY" in your insert cursor.

with arcpy.da.InsertCursor(fc,['Sp','X', 'Y', "SHAPE@XY"]) as iCurs:
    with arcpy.da.SearchCursor(fc, [X, Y, ID]) as sCurs:
        for row in sCurs:
            row = (row[2],row[0],row[1], (row[0],row[1]))
            iCurs.insertRow (row)

For your RuntimeError it sounds like you may have instances of X coordinates but not Y's or something along those lines, which I can see causing issues. Make sure that for each XY in your input table there are no null values.

  • worked great! except the RuntimeError, only occurs when it goes to the next file – harry.p Jul 9 at 16:42

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.