0

I am working with a .tif raster which has an attribute table. I can successfully add a number of extra fields, however I am having problems populating those fields.

When I add the updateCursor part to my code the script runs correctly but the attribute table is empty.

What I want to do is to take the value in the "rv_q20" field and if that is equal to a number write a new given value in "rv_q20s". I tried to pass the values from "rv_q20" between "" (e.g. "5") but encounter the same problem.

I would like to avoid using the arcpy.CalculateField_management


import arcpy

megaraster = r"E:\GIS Data\Raster1.tif"

#add score fields
arcpy.AddField_management(megaraster, "rv_q20s", "SHORT")
arcpy.AddField_management(megaraster, "rv_q50s", "SHORT")
arcpy.AddField_management(megaraster, "rv_q100s", "SHORT")
arcpy.AddField_management(megaraster, "rv_q200s", "SHORT")
arcpy.AddField_management(megaraster, "rv_q500s", "SHORT")
arcpy.AddField_management(megaraster, "rv_q1500s", "SHORT")

arcpy.AddField_management(megaraster, "sw_q20s", "SHORT")
arcpy.AddField_management(megaraster, "sw_q50s", "SHORT")
arcpy.AddField_management(megaraster, "sw_q100s", "SHORT")
arcpy.AddField_management(megaraster, "sw_q200s", "SHORT")
arcpy.AddField_management(megaraster, "sw_q500s", "SHORT")
arcpy.AddField_management(megaraster, "sw_q1500s", "SHORT")

#add field descriptions
arcpy.AddField_management(megaraster, "rv_q20d", "TEXT")
arcpy.AddField_management(megaraster, "rv_q50d", "TEXT")
arcpy.AddField_management(megaraster, "rv_q100d", "TEXT")
arcpy.AddField_management(megaraster, "rv_q200d", "TEXT")
arcpy.AddField_management(megaraster, "rv_q500d", "TEXT")
arcpy.AddField_management(megaraster, "rv_q1500d", "TEXT")

arcpy.AddField_management(megaraster, "sw_q20d", "TEXT")
arcpy.AddField_management(megaraster, "sw_q50d", "TEXT")
arcpy.AddField_management(megaraster, "sw_q100d", "TEXT")
arcpy.AddField_management(megaraster, "sw_q200d", "TEXT")
arcpy.AddField_management(megaraster, "sw_q500d", "TEXT")
arcpy.AddField_management(megaraster, "sw_q1500d", "TEXT")

RFscore = "RFscore"
SWscore = "SWscore"

arcpy.AddField_management(megaraster, RFscore, "SHORT")
arcpy.AddField_management(megaraster, SWscore, "SHORT")

#add field for total score
Totscore = "TotScore"
arcpy.AddField_management(megaraster, Totscore, "SHORT")

#add field for Risk Level
RiskLevel = "RiskLevel"
arcpy.AddField_management(megaraster, RiskLevel, "TEXT")

RFscoreName = "RFRetPer"
SWscoreName = "SWRetPer"

arcpy.AddField_management(megaraster, RFscoreName, "TEXT")
arcpy.AddField_management(megaraster, SWscoreName, "TEXT")

#call the update cursor to iterate through all the rows

rows = arcpy.UpdateCursor(megaraster)

for row in rows:
    fldDepth = row.getValue("rv_q20")
    if fldDepth is 1:
        row.setValue("rv_q20s", 10)
    elif fldDepth is 2:
        row.setValue("rv_q20s", 11)
    elif fldDepth is 3:
        row.setValue("rv_q20s", 12)
    elif fldDepth is 4:
        row.setValue("rv_q20s", 13)
    elif fldDepth is 5:
        row.setValue("rv_q20s", 14)
    elif fldDepth is 6:
        row.setValue("rv_q20s", 15)
    else:
        row.setValue("rv_q20s", 0)
  • I found the solution myself, I was missing the bit that updates the values. I changed the last part of the code to: – Giacomo Dec 5 '17 at 16:09
1

I found the solution myself, I was missing the bit that updates the values. I changed the last part of the code to:

for row in rows:
    fldDepth = row.getValue("rv_q20")
    if fldDepth == 1:
        row.setValue("rv_q20s", 10)
        rows.updateRow(row)
    elif fldDepth == 2:
        row.setValue("rv_q20s", 11)
        rows.updateRow(row)
    elif fldDepth == 3:
        row.setValue("rv_q20s", 12)
        rows.updateRow(row)
    elif fldDepth == 4:
        row.setValue("rv_q20s", 13)
        rows.updateRow(row)
    elif fldDepth == 5:
        row.setValue("rv_q20s", 14)
        rows.updateRow(row)
    elif fldDepth == 6:
        row.setValue("rv_q20s", 15)
        rows.updateRow(row)
    else:
        row.setValue("rv_q20s", 0)
        rows.updateRow(row)

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.