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?

  • 3
    did you try if routeHierarchy in ['A Road','A Road Primary']? – Taras Apr 9 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. – user140198 Apr 9 at 12:58
  • Having tried Ian's second code, your suggestion would have worked with a ( ) bracket instead of [ ]. – user140198 Apr 9 at 13:34
  • that is true...sorry for misleading – Taras Apr 9 at 13:38
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 Apr 9 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.