4

I'm trying to assign speed values to street types using TIGER street shapefiles. So I created the new field for the speed, and I was going to use the field calculator to take the MTFCC classifications and fill the speed field from that, using a simple replace script.

Attribute table for shapefile

I have this simple script that I found from another question that someone asked. I plugged my fields and values in and repeated the replace.

def Calc(value):
if "S110" in value:
 return value.replace('S1100','65')
if "S1200" in value:
 return value.replace('S1200','45')
if "1400" in value:
 return value.replace('S1400','30')
if "S1500" in value:
 return value.replace('S1500','15')
if "S1630" in value:
 return value.replace('S1630','25')
if "S1640" in value:
 return value.replace('S1640','25')

enter image description here

However, when I run it, it seems to run fine, but my fields are still Null. I really have no experience with Python, so I'm not even sure where I'm going wrong with the script.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 24, 2016 at 19:40

1 Answer 1

8

You are almost there. What you are trying to do is:

Codeblock:

def calc_speed(field_value):
 if field_value == 'S1100':
 return 65
 if field_value == 'S1200':
 return 45

Speed field

Speed = 
calc_speed(!MTFCC!)

You have to evaluate the value of MTFCC field and then make decision based on that.

Imho a more elegant solution is to use a Python dictionary. Less repetitive code and also easy to expand in case you get more class values.

Codeblock:

d = {'S1100':65,'S1200':45,'S1400':30}
def calc_speed(field_value):
 if field_value in d.keys():
 return d[field_value]

Speed field

Speed = 
calc_speed(!MTFCC!)
answered Apr 24, 2016 at 20:06
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.