I am somewhat familiar with using the python language in the Calculate Field tool in ArcGIS Pro, but was wondering how this is done in a stand alone Python script with a raster .img (Combine20012004FltPths.img) file.
I am trying to reclassify a new field I have created using integer values from another field. However, when I try to run the code in IDLE, I get a "SyntaxError: invalid syntax".
Here is my python script for the Code Block and the arcpy management Calculate Field tool:
# Code Block for Calculate Field
codeBlock = def reclass(FlightPat1):
if (FlightPat1 == 0):
return "Unclassified"
elif (FlightPat1 == 11):
return "Open Water"
elif (FlightPat1 == 12):
return "Perennial Snow/Ice"
elif (FlightPat1 == 21):
return "Developed, Open Space"
elif (FlightPat1 == 22):
return "Developed, Low Intensity"
elif (FlightPat1 == 23):
return "Developed, Medium Intensity"
elif (FlightPat1 == 24):
return "Developed, High Intensity"
elif (FlightPat1 == 31):
return "Barren Land"
elif (FlightPat1 == 41):
return "Deciduous Forest"
elif (FlightPat1 == 42):
return "Evergreen Forest"
elif (FlightPat1 == 43):
return "Mixed Forest"
elif (FlightPat1 == 52):
return "Shrub/Scrub"
elif (FlightPat1 == 71):
return "Herbaceuous"
elif (FlightPat1 == 81):
return "Hay/Pasture"
elif (FlightPat1 == 82):
return "Cultivated Crops"
elif (FlightPat1 == 90):
return "Woody Wetlands"
elif (FlightPat1 == 95):
return "Emergent Herbaceuous Wetlands"
#Reclassify field based off another field
arcpy.CalculateField_management("Combine20012004FltPths.img", "NLCD01Clss",
"Reclass(!FlightPat1!)", expression_type = "PYTHON3",
code_block = codeBlock, field_type = "TEXT")
Would I need to convert the attribute table of my .img (Combine20012004FltPths.img) file into a dBASE or geodatabase table or some other table format, then run the Code Block and Calculate Field tool for it to work?
return
inside eachelif
, they could all just beif
instead. Personally, I'd use a dictionary lookup, so your function would bereturn lookup[FlightPat1] if FlightPat1 in lookup else None