Some Bits: Nelson's log

QGIS: drawing roads with lines in casings

Many maps follow a sort of standard for symbology where roads are drawn as brown-orange-red lines with black casings around them. Like this, from a Wikipedia style guide.

QGIS has almost no support for drawing lines with casings, as common as it is. It’s something of a right of passage for map makers to figure out how to do it. I think I figured it out finally but I’m not an expert; please correct me in comments if I need it. This StackOverflow question is also helpful.

The basic idea is to draw two lines; a thick black line, then the thinner orange line on top of it. That’s simple enough. But it’s subtle and if you do it wrong all sorts of ugly things can happen.

Part of what makes doing this hard is that in most GIS data sets, roads aren’t just one single line. Particularly in OSM data roads are many line objects that are separate, little segments of road, and they just happen to overlap / join up right. Those joints can look terrible if you draw things wrong, see below. Also the intersections between roads need special attention.

I should note up-front that it’s important to test your results in whatever output format you’re really going to use. The on-screen preview in QGIS looks nice. But if you use the layout manager to compose a print run, know that the layout manager renders differently than the preview. Worse you’re probably really rendering to PDF or SVG, so you really have to look at those output files. And if you care about print, well, print it; the printer will change the way it looks once again. Most of the screenshots here are going to be in the main QGIS edit / preview mode.

The simplest thing is to just style the lines for your roads with two overlapping lines , like this in the Symbol Selector. Note the order matters!

In this case I have the black line as 1mm and the orange line as 0.6mm. Something like this is available as a default symbol in QGIS, the “Topo Main Road”.

This mostly works, and you should try it first and see if it’s acceptable because it’s so simple. However if you look closely, you’ll see small errors in the black casing. Particularly for roads made of many line segments. You can fix some of these by fiddling with the cap style; “flat” on the black line and “square” on the orange line works best for me. Here’s what square/square looks like blown up to 4mm to exaggerate the problems:

Another drawback with this simple approach is you don’t have any control over road intersections. They can end up looking terrible.

The more complicated drawing option is to have two layers for roads; one for the casings and one for the orange lines. You don’t actually need two full GIS layers though, you can also just duplicate the line data in the symbology classifier. In this case I’m using an OSM PBF extract with a Rule-Based Layer. (You might also be able to do this with a Categorized layer, but that type is less amenable to having multiple symbols for one type of data.)

If you just try this straight it might or might not work. The problem is that the order of these lines is not consistent in QGIS; they don’t necessarily draw in the order you have the rules in. To fix that you have to enable symbol levels (more info). This lets you specify the z-order of the symbols to control what order they are drawn in. Higher numbered levels are drawn on top of lower levels.

Note I’ve added in some extra complexity here; multiple sizes of roads. The general rule is you want to draw small roads first and then bigger roads on top of them. So the primary road is given the highest level, 15, so it draws on top of secondary and tertiar.

In this case I drew the casings right before the roads; doing things in that order makes it so the primary road has an uninterrupted casing. Here’s a sample of some roads of different sizes blown up to exaggerate the lines. (And the glitches in the casings, sigh. It looks better at normal resolution.)

Another option is to draw all the casings last, after all the roads. (Ie: have secondary’s orange line higher priority than the primary casing.) Doing that results in something that looks like this:

I’m not actually certain which is correct. I like the former better, so I’m doing that. Google Maps and the beautiful hand-rendered maps by Imus Geographics do the latter, so maybe I’m wrong. Honestly at normal map sizes you’d need a magnifying glass to see the difference.

One last note: symbol levels seem to only be relevant within their map layer. A symbol with a high level but in a lower map layer will not overdraw a symbol in a higher map layer.

So there you have it, roads with casings. And imperfections. I don’t know if it’s possible to actually do better in QGIS; I should really ask an expert. I sure wish QGIS just had support for “line with casing” as a symbol style. The “topo road” thing doesn’t cut it.