1

I am working on a road dataset and would like to calculate a field(speed) using three conditional statements from two fields:

  1. formOfWay: with value, Dual carriageway etc.
  2. 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?

Bera
81.7k14 gold badges85 silver badges199 bronze badges
asked Apr 9, 2019 at 12:39
4
  • 3
    did you try if routeHierarchy in ['A Road','A Road Primary']? Commented Apr 9, 2019 at 12:42
  • Hi Taras, thank you for your suggestion.I have tried the 'in' statement but it didn't work. Instead I got the value of 30 (as with the else statement) for all the rows in the speed field. Commented Apr 9, 2019 at 12:58
  • Having tried Ian's second code, your suggestion would have worked with a ( ) bracket instead of [ ]. Commented Apr 9, 2019 at 13:34
  • that is true...sorry for misleading Commented Apr 9, 2019 at 13:38

1 Answer 1

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
answered Apr 9, 2019 at 12:52
1
  • Many thank Ian Turton, both code worked. I only changed the one equal to sign before 'A Road Primary' to a double equals. Commented Apr 9, 2019 at 13:16

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.