UpdateCursor Script Tool

1771
35
05-12-2010 07:47 AM
Corey_C_Denninger
New Contributor
I am attempting to write a simple script tool that looks at one field, and if it meets a specified value(s) then populate another field with a String.  I plan to make the script a bit longer and more complex, as of now, the simple version (below) does not work.  Any help is appreciated.  Thank you.

BTW - I beleive my indentations are correct, but the script is not posting/displaying below the way I indented.

import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1

#gp.workspace = "L:\\GIS\Layers\\Geodatabases\\Land_Use_Cover\\Districtwide_LULC_Analysis.mdb"

FC = sys.argv[1]
LEV2 = sys.argv[2]
UpdateField = sys.argv[3]

cur = gp.UpdateCursor(FC)
row = cur.Next()

while row:
            if row.GetValue(LEV2) == 11:
            row.SetValue(UpdateField,"Urban & Built-Up")
            cur.UpdateRow(row)
      row = cur.Next()

del cur
del row
0 Kudos
35 Replies
deleted-user-rQoEFM5qzbHE
New Contributor II
That doesn't work, I already tried it. I am actually joining a .csv to a shapefile. The String variable does represent a field in the "parent" join table.

In order to make sure there wasn't an issue with trying to calculate a joined field, I took the function and tried to set the value of the row in the original shapefile. It still doesn't work and I am getting an error that says

[INDENT]ERROR 999999: Error executing function[/INDENT]
0 Kudos
ChrisSnyder
Regular Contributor III
I assume your .csv file has the necessary OID field, right?

It would be very useful to look at the field values to make sure the join is happening the way you think it is. A while back I wrote a sort of helper python module that among other things, can help diagnose things like this: http://arcscripts.esri.com/details.asp?dbid=15290

Basically all you have to do is copy the lmpy.py file your C:\Python2x\Lib folder.

Then in PythonWin, IDLE, etc.:

import lmpy
lmpy.listRecords("my_feature_layer")

This will list the field names and field values of the 1st 25 records such as: (note my example doesn't have a joined table).

RECORD #1
----------------------------------------------------------------------------------------------------
OBJECTID: 1
Shape: <geoprocessing describe geometry object object at 0x00C1A188>
Shape_Length: 393.699999988
Shape_Area: 9687.48062441
ACRES: 0.222393953728

RECORD #2
----------------------------------------------------------------------------------------------------
OBJECTID: 2
Shape: <geoprocessing describe geometry object object at 0x00C1A9F8>
Shape_Length: 393.699999988
Shape_Area: 9687.48062441
ACRES: 0.222393953728

RECORD #3
----------------------------------------------------------------------------------------------------
OBJECTID: 3
Shape: <geoprocessing describe geometry object object at 0x00C1A188>
Shape_Length: 393.700000018
Shape_Area: 9687.48062588
ACRES: 0.222393953762

Are the field values in your join table what you expected? Are some of the joined field values NULL? If so, you will need to exclude the NULLs (the "can't update field values to NULL" bug is a long standing issue that was finally fixed with v10).

Also, take a look at the lmpy.listfields() function. Could it be you are trying transfer a text field into a integer field or something to that effect?
0 Kudos
deleted-user-rQoEFM5qzbHE
New Contributor II
The the following line of code was how I tried to trap for NULL values.

if M[0][1] > 0:

Realizing this may not work, I changed it to

if M[0][1] <> "NULL":

However, it still doesn't work.

My join seems to be working fine and the value I am trying to transfer is a string. As I mentioned in my previous post, this function does not seem to work even without a join. I tried to just set the value to "XXXXXX", but got the same error.
0 Kudos
deleted-user-rQoEFM5qzbHE
New Contributor II
Alright, I have made a couple of changes and still no success. But, maybe the results will be enlightening. I was originally using a gp.SearchCursor function instead of an UpdateCursor function (I don't really know what the difference is). Since changing to the UpdateCursor function, the script crashes at that line if I pass in a joined table. Is it possible that UpdateCursor doesn't work with a join? It did not crash when I passed in only the feature class.

Thanks.
0 Kudos
ChrisSnyder
Regular Contributor III
1) Here's how to figure out if a field value is NULL in a searchcursor:
searchRows = gp.searchcursor(fc):
searchRow = searchRows.next()
while searchRow:
   if searchRow.FIELDNAME == None: #or inverse of "... != None:"
      print "It's NULL!"
   searchRow = searchRows.next()
del searchRow
del searchRows


2) A searchcursor is used to simply reterieve (e.g. read) field/record values (read cursor = read access). An updatecursor is used to change existing field/record values in a table. Note that you can't change values in a lookup table (only the join to table) if you are dealing with a joined table situation (updatecursor = write access). An updatecursor has all the functionality of a readcursor, but with the added ability to alter the field/record values. An insertcursor is used to write (aka insert) new records (and field values) into a table.

So here's some things to try:

1. Instead of using the .csv file directly, convert it to a table (.dbf, PGDB, FGDB, etc.) via ArcGIS. That way it will be garannteed to generate an OBJECTID field. I think this is where things are going wrong for you.

2. Just a note: You line of code:

for i in range(0,numSchools):

should probably be

for i in range(0,numSchools + 1):

because:

for i in range (0,5):
   print i
0
1
2
3
4
0 Kudos
deleted-user-rQoEFM5qzbHE
New Contributor II
Well, I can't even get past this line of code:

rows1 = gp.UpdateCursor("PS_lyr")


"PS_lyr" is the joined table. I have since exported the .csv to a .dbf to see if that fixed the problem, but it hasn't. I have imported lmpy and everything seems to be fine. It displays all the fields in the joined table with the proper table precursor.fieldname.

I am starting to think I am going to have to come at this from a different direction.
0 Kudos