GEOM7004 Assignment 2 Question 1

"""

    GEOM7004 Assigment 2 Question 1
    Table Classification via Python Script

"""

import arcpy

# Get the layer from the 1st parameter.
layer_name=arcpy.GetParameterAsText(0)
# Get the field storing the texture from the 2nd parameter.
t_name=arcpy.GetParameterAsText(1)
# Get the field storing the humidity value from the 3rd parameter.
h_value=arcpy.GetParameterAsText(2)
# Obtain the field name for storing the result.
r_name=arcpy.GetParameterAsText(3)

# Load the table into a cursor object.
try:
    rows=arcpy.UpdateCursor(layer_name)
except:
    arcpy.AddError("Error: Fail to open the table!")

# Compute the result with iterating all the rows in the table.
matched=False
try:
    for row in rows:
        # To check whether the humidity value is null or not first, if so, ignore all the follwoing operations.
        if row.getValue(h_value)==None:
            continue
        if row.getValue(t_name)=="Sandy":
            matched=True
            if row.getValue(h_value)<5:
                row.setValue(r_name,"Poor")
            elif row.getValue(h_value)<=10:
                row.setValue(r_name,"Poor")
            else:
                row.setValue(r_name,"Medium")
        elif row.getValue(t_name)=="Sandy-Clay":
            matched=True
            if row.getValue(h_value)<5:
                row.setValue(r_name,"Poor")
            elif row.getValue(h_value)<=10:
                row.setValue(r_name,"Medium")
            else:
                row.setValue(r_name,"Rich")
        elif row.getValue(t_name)=="Clay":
            matched=True
            if row.getValue(h_value)<5:
                row.setValue(r_name,"Medium")
            elif row.getValue(h_value)<=10:
                row.setValue(r_name,"Rich")
            else:
                row.setValue(r_name,"Rich")
        else:
            pass
        rows.updateRow(row)
except Exception as e:
    arcpy.AddError("An error occured:\n"+str(e))
# If there is not a record with result computed. 
if matched==False:
    arcpy.AddWarning("Warning: Do not found any appropriate row. No record has been changed!")


你可能感兴趣的:(GEOM7004 Assignment 2 Question 1)