Select to view content in your preferred language

Calculate fields using logic with Python - standalone script

4895
13
Jump to solution
04-22-2021 05:01 AM
KK2
by
Occasional Contributor

Hallo, I would like to ask how to populate a new attribute field using logic with python as standalone script without opening Raster Calculator and pasting the script attached below. How to assign with python the information from which attribute(s) and from which dataset the new attribute will be populated?

Expression:
Reclass(!WELL_YIELD!)

Code Block:
def Reclass(WellYield):
    if (WellYield >= 0 and WellYield <= 10):
        return 1
    elif (WellYield > 10 and WellYield <= 20):
        return 2
    elif (WellYield > 20 and WellYield <= 30):
        return 3
    elif (WellYield > 30):
        return 4

 

0 Kudos
3 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
DanPatterson
MVP Esteemed Contributor

you don't need Shape

float(!SHAPE.CLC1!)

should be 

!CLC1!

since that is the field you are using as the source information


... sort of retired...

View solution in original post

DanPatterson
MVP Esteemed Contributor

Calculate Field (Data Management)—ArcGIS Pro | Documentation

arcpy.management.CalculateField(in_table, field, expression, {expression_type}, {code_block}, {field_type})

arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3",
codeblock)

"getClass(short(!CLC1!))"  # wrong

leave out short

"getClass(!CLC1!)"

AND the error says....

    .... not a member of VB | PYTHON | PYTHON_9.3

and you put .... "PYTHON3",

try  "PYTHON" or  "PYTHON_9.3"


... sort of retired...

View solution in original post

13 Replies
DanPatterson
MVP Esteemed Contributor

Use the spatial analyst's Con function. 

It this example W is your WellYield raster.

 

OutRas = Con(W <= 10, 1, Con(W <= 20, 2, Con(W <= 30, 3, 4)))

or try

OutRas = RoundUp(Divide(W, 10.))

 

 


... sort of retired...
0 Kudos
KK2
by
Occasional Contributor

Thank you for the answer, but I am not working on raster but on polygon shapefile and I would like to run it as standalone python script, so I think the Con function will not help me much. I am looking for a script that indicates me the input data and input attributes in the attribute table from which the logic statement is evaluated to populate a new attribute in my polygon shapefile.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Ahh field calculator

Calculate Field (Data Management)—ArcGIS Pro | Documentation

 


... sort of retired...
KK2
by
Occasional Contributor

Thank you for the answer, it is what I was looking for.

However, I received an error while running the script, which was as follow: 

Runtime error  Traceback (most recent call last):   File "<string>", line 3, in <module>   File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 3183, in CalculateField     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000800: The value is not a member of VB | PYTHON | PYTHON_9.3. Failed to execute (CalculateField).  

Do you know maybe what might be wrong in my code? I think it might be because of the line attached below, because the example code was taking area and I want to reclassify my input data to another classes and I am not sure if my implementation is correct. In the line attached below  I wanted to take the attribute value to calculate the new attribute.

# Set local variables
inTable = "CLC"
expression = "getClass(float(!SHAPE.CLC1!))"

 

The entire code:

# Import system modules
import arcpy
# Set environment settings
arcpy.env.workspace = "E:/CLC_data/CLC.shp"

# Set local variables
inFeatures = "CLC"
fieldName = "NEW_code"
#fieldPrecision = 9
#fieldAlias = "refcode"
fieldLength = 50
 
# Execute AddField for new fields
arcpy.AddField_management(inFeatures, fieldName, "TEXT", field_length=fieldLength)

# Set local variables
inTable = "CLC"
expression = "getClass(float(!SHAPE.CLC1!))"

#Create NEW_code for CLC1
codeblock = """
def getClass(CLC1):  
  if (CLC1 == 1):  
    return 'A_1'  
  elif (CLC1 == 2):  
    return 'A_2'
  elif (CLC1 == 3):  
    return 'A_3'   	
  else:
	return 'error'"""
 	
# Execute CalculateField 
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3", 
                                codeblock)
0 Kudos
DanPatterson
MVP Esteemed Contributor

you don't need Shape

float(!SHAPE.CLC1!)

should be 

!CLC1!

since that is the field you are using as the source information


... sort of retired...
KK2
by
Occasional Contributor

Thank you for your informative answer. I corrected the script accordingly, but unfortunately, I still have the same error. Do you see maybe some other error in my script that may cause this error? I think that I implemented everything like in the example on esri website, but it does not work.

0 Kudos
DanPatterson
MVP Esteemed Contributor

arcpy.env.workspace = "E:/CLC_data

the folder would be the workspace

What you have with the shapefile appended to it is the featureclass name


... sort of retired...
KK2
by
Occasional Contributor

Thank you for your reply. I have  amended the script according to your suggestion, but unfortunately I still have the same error. It has to be something different, but I don't know what it is.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Post a clean version of your script so we can see what changes were made.  And your error message again too


... sort of retired...
0 Kudos