UpdateCursor Script Tool

1720
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
JohnHauck
Occasional Contributor II
I don't see anything wrong with the code. Try running the following from a Python IDE to see if you can notice anything else.

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

FC = r"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\Atlanta.gdb\streets"
LEV2 = "JoinId"
UpdateField = "CITYL"

try:
    cur = gp.UpdateCursor(FC)
    row = cur.Next()
    while row:
        if row.GetValue(LEV2) == 1:
            row.SetValue(UpdateField, "Urban & Built-Up")
            cur.UpdateRow(row)
        row = cur.Next()
except:
    print gp.getmessages(0)
    gp.addmessage(gp.getmessages(0))

del cur, row
0 Kudos
Corey_C_Denninger
New Contributor
Thanks for the assistance.  I did as you suggested.  I closed ArcCatalog/Map, ran from IDLE, completes successfully with no Errors messages.  Open feature class, no Updates made to the field even though the parameters are met.  The LEV2 field is a Short Interger field and the CATEGORY field is a String/Text field.  Here is the script you posted, modified for my file locations/names.
***********************
import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1


FC = "L:\\GIS\Layers\\Geodatabases\\Land_Use_Cover\\Districtwide_LULC_Analysis.mdb\\District_LULC_Counties_Cities_99_Id"
LEV2 = "LEV2"
UpdateField = "CATEGORY"

try:
    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()
except:
    print gp.getmessages(0)
    gp.addmessage(gp.getmessages(0))

del cur, row
0 Kudos
RDHarles
Occasional Contributor
Just for fun, I ran the code (only changing the 3 variables to fit my test file) and it worked perfectly.
No idea why it wouldn't work for you???
p.s. I assume your path doesn't really have those spaces in it.
p.s.s.  When you copy and past code for a post, highlight just the code and hit the "#" button.  That will keep the formatting.

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

FC = "C:/junk/db1.mdb/table1"
# number
LEV2 = "nm_area"
# text
UpdateField = "TEST"

try:
    cur = gp.UpdateCursor(FC)
    row = cur.Next()
    while row:
        if row.GetValue(LEV2) == 1:
            row.SetValue(UpdateField, "Urban & Built-Up")
            cur.UpdateRow(row)
        row = cur.Next()
except:
    print gp.getmessages(0)
    gp.addmessage(gp.getmessages(0))

del cur, row
0 Kudos
Corey_C_Denninger
New Contributor
I got it to work.  It wasn't the code (as you stated), but my personal geodatabase had reached its size limits and the tool wouldn't function because, I assume, by adding those text strings to my feature classes it pushed the whole gdb over the 2GB limit.  I placed into a file gdb and we're good to go.  I modified the script to function as a script tool.  I am sure there is a more efficient way to write this script (lists, tuples, etc).  If anyone has suggestions, great...if not, it works fine.  My scripting is getting better, but is very basic at this point.

Also, does anyone know how I credit someone with successfully assisting me (like in the old Forum) so they get their points, etc?

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

FC = gp.GetParameterAsText(0)
LEV2 = sys.argv[2]
LEV1 = sys.argv[3]
UpdateField = sys.argv[4]

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

#A = [11,12,13,14,17,18,19]
#B = [15,16]
#C = [21,22,23,24,25,26,27,28,29]

while row:
      if row.GetValue(LEV2) == 11:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 12:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 13:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 14:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 17:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 18:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 19:
            row.SetValue(UpdateField,"Urban & Built-Up")
      elif row.GetValue(LEV2) == 15:
            row.SetValue(UpdateField,"Industrial & Mining")
      elif row.GetValue(LEV2) == 16:
            row.SetValue(UpdateField,"Industrial & Mining")
      elif row.GetValue(LEV1) == 2:
            row.SetValue(UpdateField,"Agriculture")
      elif row.GetValue(LEV1) == 3:
            row.SetValue(UpdateField,"Rangeland")
      elif row.GetValue(LEV1) == 4:
            row.SetValue(UpdateField,"Upland Forest")
      elif row.GetValue(LEV1) == 5:
            row.SetValue(UpdateField,"Water")
      elif row.GetValue(LEV1) == 6:
            row.SetValue(UpdateField,"Wetlands")
      elif row.GetValue(LEV1) == 7:
            row.SetValue(UpdateField,"Barren Land")
      elif row.GetValue(LEV1) == 8:
            row.SetValue(UpdateField,"Trans, Comm, & Util") 
      cur.UpdateRow(row)
      row = cur.Next()
del cur
0 Kudos
RDHarles
Occasional Contributor
To avoid duplicating code, you could do something like this using your lists and the 'in' operator:

A = [11,12,13,14,17,18,19]
B = [15,16]

while row:
      if row.GetValue(LEV2) in A:
            row.SetValue(UpdateField,"Urban & Built-Up")
      if row.GetValue(LEV2) in B:
            row.SetValue(UpdateField,"Industrial & Mining")
0 Kudos
Corey_C_Denninger
New Contributor
That did it.  Thanks for all the help R.D.  I'm surprised though, that not 'elif' statements are needed and the 'if' alone works just fine.  How do I credit you with assisting me on this post and mark 'completed', etc....like in the old Forum?  Are there no longer points awarded?

Here is the script as I have it now.  Saved a few rows and characters from the final script...thanks again:

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

FC = gp.GetParameterAsText(0)
LEV2 = sys.argv[2]
LEV1 = sys.argv[3]
UpdateField = sys.argv[4]

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

A = [11,12,13,14,17,18,19]
B = [15,16]
C = [2]
D = [3]
E = [4]
F = [5]
G = [6]
H = [7]
I = [8]


while row:
      if row.GetValue(LEV2) in A:
            row.SetValue(UpdateField,"Urban & Built-Up")
      if row.GetValue(LEV2) in B:
            row.SetValue(UpdateField,"Industrial & Mining")
      if row.GetValue(LEV1) in C:
            row.SetValue(UpdateField,"Agriculture")
      if row.GetValue(LEV1) in D:
            row.SetValue(UpdateField,"Rangeland")
      if row.GetValue(LEV1) in E:
            row.SetValue(UpdateField,"Upland Forest")
      if row.GetValue(LEV1) in F:
            row.SetValue(UpdateField,"Water")
      if row.GetValue(LEV1) in G:
            row.SetValue(UpdateField,"Wetlands")
      if row.GetValue(LEV1) in H:
            row.SetValue(UpdateField,"Barren Land")
      if row.GetValue(LEV1) in I:
            row.SetValue(UpdateField,"Trans, Comm, & Util")
      cur.UpdateRow(row)
      row = cur.Next()
del cur
0 Kudos
RickyLindley
New Contributor
Along the same type of updatecursor script, I am needing help on a script that will look at the first record in a table and update a field in a featureclass. This would be a one to many field update.
The table is called ProjectList, its has fields Category, Subno and ProjectName. Each record in the table is unique.
The featureclass is called Water and it has the same 3 fields as the table, but multiple records with the same Category and Subno.....the ProjectName is blank or incorrect.
I am trying to write a script that will go to the first record in the ProjectList table and update each ProjectName in the Water featureclass with ProjectName from the ProjectList table. Then continue to the next record in the table and repeat until it reaches the end of the table. Thanks.
0 Kudos
RDHarles
Occasional Contributor
Corey,

Glad I could help.

I prefer using only "if" statements.  That way each statement is evaluated individually instead of the whole thing being tied together in an if/elif/else. 

Not sure if they are bringing back the credit/completed/points system.  I thought I read somewhere that they were but it appears it is not working now.
0 Kudos
RDHarles
Occasional Contributor
rickyl15,
Here's an example of reading from a table (dbf file) and writing to fc (shapefile).
The SearchCursor reads the dbf, the UpdateCursor writes to the fc.
This should give you a pretty good start.

## Read the dbf and populate the shp.

import arcgisscripting, sys, os
gp = arcgisscripting.create()

gp.workspace = os.getcwd()

dbf = "Apr.dbf"
shp = "Apr2009.shp"

# dbf loop
rows = gp.SearchCursor(dbf)
row = rows.Next()
while row:
    
    # Get the value from the field(dbf)
    value = row.GetValue("ID_NUM")
    
    print "Processing ID_NUM = "+str(value)
        
    # shp loop    
    rows2 = gp.UpdateCursor(shp)
    row2 = rows2.Next()
    while row2:
        # Get the value from the field (shp)
        value2 = row2.GetValue("ID_NUM")

        # Populate field in shp based on a match        
        if value2 < 1740:            
            row2.city_legal = "Lower"      
        if value2 > 1739:            
            row2.city_legal = "Higher"

        # Execute the new value to the shp
        rows2.UpdateRow(row2)            
        # shp next
        row2 = rows2.Next()         
    
    # dbf next
    row = rows.Next()

del rows2
del rows

print "\nDone.\n"
0 Kudos