I want to add additional speed values to my OSM data. There is a maxspeed value for some of the roads, but a lot of values are 'null'. Therefore, I would like to add additional values based on the OSM road type categorisation (e.g primary roads have a maximum speed of 90 in case the maxspeed value is 'null'). That way, I can make isochrone analysis based on more accurate maxspeed inputs.

So I tried the following in a virtual field of the OSM data using the expression builder:

If("maxspeed"is null) AND ("highway" =   'primary' ),90, if("maxspeed"is null) AND ("highway" =  'secondary'),50,  "maxspeed")

This should then be done for every category, but the formula is not working. Could anyone help me out?

  • 1
    Your brackets don't make sense, otherwise the expression is solid. Still, it would be easier to use CASE WHEN instead of if - less nesting.
    – Erik
    Aug 31, 2021 at 10:34

2 Answers 2


As Erik suggests, a CASE WHEN statement works fine.

      WHEN "maxspeed" IS null AND "highway" = 'primary' THEN 90 
      WHEN "maxspeed" IS null AND "highway" = 'secondary' THEN 50 
      ELSE "maxspeed" 

Looks like your brackets are no good.

You can try:

IF("maxspeed" is null AND "highway" = 'primary',   90,
IF("maxspeed" is null AND "highway" = 'secondary', 50, "maxspeed") )

But the CASE WHEN answer is more clear.

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.