I am working on a road dataset and would like to calculate a field(speed) using three conditional statements from two fields:
- formOfWay: with value, Dual carriageway etc.
- routeHierarchy: with values A Road, A Road Primary etc.
My aim is to return a speed value of 65 where formOfWay is Dual Carriageway AND Route Hierarchy is either A Road or A Road Primary.
Using the code provided in a similar question asked, I have tried the code below in ArcGIS Field Calculator, but haven’t been unsuccessful. The error message returned point to a syntax error in line 2 and I suspect this should be the part of the code having the two ‘routeHierarchy’
values (A Road and A Road Primary).
The code works with one coded value for routeHierarchy but returns an error when the second value is added.
def ifBlock(routeHierarchy, formOfWay):
if routeHierarchy == ['A Road','A Road Primary'] and formOfWay == 'Dual Carriageway':
return 65
else:
return 30
Please, can someone point out the correction I can apply to make this work?
1 Answer 1
You need either:
def ifBlock(routeHierarchy, formOfWay):
if (routeHierarchy == 'A Road' or routeHierarchy == 'A Road Primary') and formOfWay == 'Dual Carriageway':
return 65
else:
return 30
or
def ifBlock(routeHierarchy, formOfWay):
if routeHierarchy in ('A Road','A Road Primary') and formOfWay == 'Dual Carriageway':
return 65
else:
return 30
-
Many thank Ian Turton, both code worked. I only changed the one equal to sign before 'A Road Primary' to a double equals.user140198– user1401982019年04月09日 13:16:51 +00:00Commented Apr 9, 2019 at 13:16
Explore related questions
See similar questions with these tags.
if routeHierarchy in ['A Road','A Road Primary']
?