0

I'm having trouble making a script tool in ArcGIS. I have developed a script that will edit certain values in the attribute table. Without incorporating a script tool this script works fine and edits the relevant attributes adequately. Here it is:

import arcpy

fc = "C:\Users\maureen\Documents\ArcGIS\EDRN\EDRN_LINK.shp"
fields = ["FID", "RIVERNAME", "SOURCE", "DRAINS", "RIVERTYPE", "LCC_USER"]

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == 4402:
            row[1] = "Calder"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == 4402:
            row[2] = "2009 Aerial Photography"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == 4402:
            row[3] = "NO"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == 4402:
            row[4] = "1"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == 4402:
            row[5] = "T0015685"
            cursor.updateRow(row)

When I edit the above and try to create a script tool my script is as follows:

import arcpy

fc = "C:\Users\maureen\Documents\ArcGIS\EDRN\EDRN_LINK.shp"
fields = ["FID", "SOURCE", "RIVERNAME", "RIVERTYPE", "DRAINS", "FRMMAINRIV",   "LCC_USER"]

f_id = arcpy.GetParameterAsText(0)
srce = arcpy.GetParameterAsText(1)
rvrnme = arcpy.GetParameterAsText(2)
rvrtyp = arcpy.GetParameterAsText(3)
drns = arcpy.GetParameterAsText(4)
fr_main_riv = arcpy.GetParameterAsText(5)
lcc_us = arcpy.GetParameterAsText(6)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == "f_id":
            row[1] = "srce"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == "f_id":
            row[2] = "rvrnme"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == "f_id":
            row[3] = "rvrtyp"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == "f_id":
            row[4] = "drns"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == "f_id":
            row[5] = "fr_main_riv"
            cursor.updateRow(row)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == "f_id":
            row[6] = "lcc_us"
            cursor.updateRow(row)

Once I have made the necessary edits and ran the script tool wizard the script doesn't work. The detail box states that the scrip has completed and that it was successful, but non of the specified edits have been occurred and only a blank space remains. The above makes me think that the problem lies in the script tool wizard, specifically the data types and parameter properties I am using.

Can anyone suggest any changes I can make to the the script tool wizard or the amended script?

  • I think you should use arcpy.AddMessage() to check that all the variables you are setting from the tool dialog have the values you think they have. If that does not solve it then can you edit your Question to provide your latest code and what the tool dialog is reporting, please ? – PolyGeo Jun 24 '14 at 19:03
2

As mentioned by Hornbydd, you should put everythin in the same run of the cursor because this code is clearly not optimal. The second problem is that there is a confusion between the name of the variable and the string of the name of the variable.The last problem is that your variable f_id contains a string, but FID is of type integer. So you need to remove the quotes as already mentioned, and use the int() function

with arcpy.da.UpdateCursor(fc, fields) as cursor:
       for row in cursor:
       if  row[0] == int(f_id):
            row[1] = srce
            row[2] = rvrnme
            row[3] = rvrtyp

            cursor.updateRow(row)
  • Thanks alot, it turns out that the last problem you mentioned was the main problem. I made the changes to my original script and it worked! Cheers. – Geord359 Jun 25 '14 at 10:19
3

Instead of hard coding your values in the script, I recommend creating variables as if it was already in a script tool. Also, I would group your assignments into a single cursor.

import arcpy

fc = "C:\Users\maureen\Documents\ArcGIS\EDRN\EDRN_LINK.shp"
fields = ["FID", "SOURCE", "RIVERNAME", "RIVERTYPE", "DRAINS", "FRMMAINRIV",   "LCC_USER"]

f_id = "4402" 
srce = "srce" 
rvrnme = "rvrnme"
rvrtyp = "rvrtyp"
drns = "drns"
fr_main_riv = "fr_main_riv"
lcc_us = "lcc_us"

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == f_id:
            row[1] = srce
            row[2] = rvrnme
            row[3] = rvrtyp
            row[4] = drns
            row[5] = fr_main_riv
            row[6] = lcc_us
            cursor.updateRow(row)

If the script behave as expected after testing, you can change the values of our variables into arcpy.GetParameterAsText() input statements and add those in the script tool's properties under the Parameters tab, in the same order.

import arcpy

fc = "C:\Users\maureen\Documents\ArcGIS\EDRN\EDRN_LINK.shp"
fields = ["FID", "SOURCE", "RIVERNAME", "RIVERTYPE", "DRAINS", "FRMMAINRIV",   "LCC_USER"]

f_id = arcpy.GetParameterAsText(0)
srce = arcpy.GetParameterAsText(1)
rvrnme = arcpy.GetParameterAsText(2)
rvrtyp = arcpy.GetParameterAsText(3)
drns = arcpy.GetParameterAsText(4)
fr_main_riv = arcpy.GetParameterAsText(5)
lcc_us = arcpy.GetParameterAsText(6)

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if  row[0] == f_id:
            row[1] = srce
            row[2] = rvrnme
            row[3] = rvrtyp
            row[4] = drns
            row[5] = fr_main_riv
            row[6] = lcc_us
            cursor.updateRow(row)

Alternatively, you could switch your logic around and use arcpy.MakeFeatureLayer_management(), arcpy.SelectLayerbyAttribute_management() and arcpyCalculateField_management() instead. This might be more efficient than updating features individually.

  • Thanks, for your suggestions on how to condense my script. It was overly long! – Geord359 Jun 25 '14 at 10:22
  • You are welcome @Geord359. Did you solve your problem? I am still unclear why you iterate each feature using a cursor instead of using CalculateField multiple time. I can provide an example if it can kelp. – ericchiasson Jun 25 '14 at 10:32
  • I did solve my problem Eric. It turns out it I was inputting the FID as a string, so had to compensate by changing parts of the script to int(f_id). That seemed to resolve things. The main reason I used a cursor instead of another method is it was the only way that seemed logical when I was thinking it through. I only started learning python a few weeks ago so I'm afraid my scripts probably aren't as concise or efficient as they could be. Just need a bit more practice I think! – Geord359 Jun 25 '14 at 10:52
2

I think all you need to do is remove the quotes from around your row statements. So

 if  row[0] == "f_id":
     row[1] = "srce"
     cursor.updateRow(row)

becomes

 if  row[0] == f_id:
     row[1] = src
     cursor.updateRow(row)

Also the logic is a bit zany. You have a list called fields which you use in your cursor but then you get them as parameters so potentially they can be something quite different and your code would fail. A smarter way would be to get your parameters as you have then build your field list from them.

  • I've tried removing the quotes but I still get the same results. Thanks anyway. – Geord359 Jun 24 '14 at 16:51

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.