2

I don't know if my mistake is of a general nature. I come from R where it is very easy to store a vector in a matrix column. So now I (new to coding with arcpy) am desperately trying to do the same thing in ArcGIS.

I have a point feature class to which I have added a new field ("new_ID"). I want this new field to be filled with a sequence of numbers from 1 to [number of items in my FeatureClass].

Question: Is it possible to assign a vector to the new field?

The source of my problem is: I need to move and sometimes delete points in a feature class. After deleting them the column OBJECTID obviously doesn't contain a straight sequence anymore. For example: my points feature class contains 10 points, I delete point number 5, so my column OBJECTID goes like: 1,2,3,4 , 6,7,8,9,10 (5 is missing). For specific reasons I need a straight sequence of numbers. In that case after the deletion of point number 5: from 1 to 9.

I couldn't find any help in the search section. So I wonder if this problem ever occured to someone else? Am I just still to much in R? Would you solve this issue in ArcGIS completely differently? Or am I just overlooking something crucial?

I'm also unsure about which tool to use. UpdateCursor? CalculateField? JoinField?

Here's what I tried:

>>> sum = arcpy.GetCount_management("my_featureClass")
sum = int(sum.getOutput(0))
input_field = range(1, sum + 1)

>>> cursor = arcpy.UpdateCursor("my_featureClass")
for i in curs:
    i.new_ID = input_field
    curs.updateRow(i)

ValueError: Row: Invalid input value for setting

>>> cursor = arcpy.UpdateCursor("my_featureClass")
for i in curs:
    i.new_ID = input_field[i]
    curs.updateRow(i)

TypeError: list indices must be integers, not Row

>>> cursor = arcpy.UpdateCursor("my_featureClass")
for i in curs:
    i.new_ID = int(input_field)
    curs.updateRow(i)

TypeError: int() argument must be a string or a number, not 'list'

I understand why all three don't work but I don't see a solution.

  • after you delete a row and save, the OBJECTID should automatically recreate a straight sequence. You can also look for "increment" on this site for examples in field calculator e.g. gis.stackexchange.com/questions/111647/… – radouxju Sep 15 '14 at 14:45
  • do you mean saving the whole .gdb? That doesn't work for me. Also refreshing the table doesn't give me a straight sequence. – chris.en Sep 16 '14 at 9:48
1

You need to give it some indication as to which value from the input list (vector) you want for each row, which will require enumerating the cursor so you have access to an index value. Try:

sum = arcpy.GetCount_management(fc)
sum = int(sum.getOutput(0))
input = range(1, sum + 1)

cursor = arcpy.da.UpdateCursor("my_featureClass", "new_ID")
for n, row in enumerate(cursor):
    row[0] = input[n]
    cursor.updateRow(row)

This linefor n, row in enumerate(cursor) generates an index value n within that loop, which you can use to pull values out of a related list.


An alternative which would let you skip the sum and input variables entirely (since it's "counting" the rows as it goes through the loop):

cursor = arcpy.da.UpdateCursor("my_featureClass", "new_ID")
for n, row in enumerate(cursor):
    row[0] = n + 1
    cursor.updateRow(row)

I also recommend (and coded above) using the da cursors if you've got 10.1+ -- they are faster (since you can worry about only one field at a time).

|improve this answer|||||
  • general question: can I call every cursor only once? I type for example: "for row in cursor: [RETURN] print row" and it works. If I type the same code a second time it doesn't give me any result at all. By now, I actually wonder if my whole set up is buggy as your suggested enumerate-code doesn't work either... strange. – chris.en Sep 16 '14 at 9:41
  • okay, maybe this is known to everybody but me: I can actually not use the same cursor twice in a loop. However, it works when I call the definition of the cursor (cursor = arcpy.da.UpdateCursor("fc","fieldname")) again and then use the cursor in a loop. EDIT: And now the whole code works. Thank you, Erica and guys! – chris.en Sep 16 '14 at 9:52
1

An alternative to Erica's answer:

cursor = arcpy.da.UpdateCursor("my_featurClass", "new_ID")
i = 1
for row in cursor:
  row[0] = i
  cursor.UpdateCursor(row)
  i += 1
del cursor, row
|improve this answer|||||
  • that is probably how I would have solved it in R. Good to see that is also works with ArcGIS – chris.en Sep 16 '14 at 8:54

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.