I am trying to set up a drive time and I have classified a few roads in my attribute table as follows:
A5=35MPH
A4=30MPH
A3=20MPH
So the codes A5, A4, A3 are in a field called CFCC2, My next step was to add a new field to the attribute table called speed. Currently all the values in speed are null. My question is, how can i create a python script so that I have something that will calculate the speeds of the roads based on the classification I have given them.
Solved! Go to Solution.
Add a string field, use the field calculator emulating this
"True" if !some_field! == "some_value" else ("other value" if !some_field! == "final_value" else "False")
totally untried
"35MPH" if !CFCC2! == "A5" else ("30MPH" if !CFCC2! == "A4" else ("20MPH" if !CFCC2! == "A3" else "20MPH"))
Add a string field, use the field calculator emulating this
"True" if !some_field! == "some_value" else ("other value" if !some_field! == "final_value" else "False")
totally untried
"35MPH" if !CFCC2! == "A5" else ("30MPH" if !CFCC2! == "A4" else ("20MPH" if !CFCC2! == "A3" else "20MPH"))
Here's a way to do it using the Calculate Field tool, with a Python dictionary -- a little easier to work with if you have more than three categories....
Calculate Field
Expression:
mph(!CFCC2!)
Parser:
PYTHON
Code Block:
speed = {"A3":30, "A4":30, "A5":35} def mph(code): return speed
You could also set up a domain for field CFCC2 if this is in a geodatabase. Then no need for a separate Speed field. This is a more database-centric method, since having two fields essentially duplicates the same information in different display formats.