Scale-dependent rendering
While they are not specifically for web services, being able to change the styling and presence of data based on the scale of the map can have a huge impact on the speed and readability of web services. Unlike printed maps, web maps are viewed at multiple scales. This variation in scales often requires different cartography to keep the map legible and usable.
You'll need a QGIS project, preferably one with a high data density or differing levels of information. A good example is road data, where you have major, minor, local, and other variants of road classification. caryStreets.shp converted from CAD in a previous chapter is a good example.
- Open QGIS and load
caryStreets.shp. - Now, open the attribute table and look for an attribute to filter in. In
caryStreets.shp, there are several potential columns to use, such as StreetType, Major_Road, and Main_Road.Note
StreetType appears to be classes, whereas the other two columns appear to be True or False flags. Any of these are decent candidates for filtering rules.
- Now, open the Properties section for the layer:
- Switch to the Style tab to edit the symbology.
- Change the top-right dropdown to Rule Based Rendering.
- Create a new rule (green plus sign).
- In the pop up dialog set Label to Major Roads and Filter to "Major_Road" = 't'.
- (Optional) You can use the expression builder to build the filter statement and test it. Click on the … button to open the dialog.
Tip
You could create two copies of Major with different scale ranges so that as you zoom in, the major roads become thicker at the same time that minor roads are enabled.
This is what your layer looks like before and after you create the first rule:
- Now, add another rule for minor roads by filtering for "Major_Road" = 'f'.
- This time, you're going to enable the Scale range option.
- Set Minimum (exclusive) to 1:100,000. For any scale bigger than this, the features will be hidden. For Maximum (inclusive), type in 1:0, which will disable the Max filter.
- Pick a different line type and/or color for the minor roads:
- You should now have two rules, one for major roads and one for minor roads:
Tip
You don't have to open the edit rule dialog; you can directly modify parts of the rules in the Rule Based Rendering page.
- Go back to the map and zoom in to 1:50,000, then zoom out to 1:250,000. The minor roads should appear and disappear as you change past the 1:100,000 scale:
The goal with scale-dependent rendering is generally to make your map readable at many different zoom levels. By setting the Min and Max scales for each layer or subfeatures within a layer, you can declutter a map for readability. The rendering engine just checks the scale against each rule before deciding what to render.
Scale-dependent rendering can be used in several ways. This can be used to change the styling based on zoom or hide or reveal data based on the zoom level. However, it's also not limited to just changing styles or layers. You can also perform scale-dependent labeling, which is part of data-driven labeling described in the Configuring data-defined labels recipe in Chapter 10, Cartography Tips, of this book.
Scale rules also work on raster layers; however, this only allows you to turn a raster on and off. It doesn't allow you to change its appearance.
If you have a QGIS server set up from earlier in this chapter, the scaling rules should apply to your web services (WMS and WFS).
Tip
You probably don't want to use something as complex as a street layer via WFS in a web browser because it's almost guaranteed to crash. Stick to pushing such complex layers as Tiles or WMS.
The Rule Based Rendering has a lot of features crammed into it. However, this is not yet a comprehensive guide to everything that it can do, so you'll need to explore and perform Internet searches for now.