QGIS:Becoming a GIS Power User
上QQ阅读APP看书,第一时间看更新

Advanced vector styling

This section introduces more advanced vector styling features, building on the basics that we covered in Chapter 2, Viewing Spatial Data. We will cover how to create detailed custom visualizations using the following features:

  • Graduated styles
  • Categorized styles
  • Rule-based styles
  • Data-defined styles
  • Heatmap styles
  • 2.5D styles
  • Layer effects

Creating a graduated style

Graduated styles are great for visualizing distributions of numerical values in choropleth or similar maps. The graduated renderer supports two methods:

  • Color: This method changes the color of the feature according to the configured attribute
  • Size: This method changes the symbol size for the feature according to the configured attribute (this option is only available for point and line layers)

In our sample data, there is a climate.shp file that contains locations and mean temperature values. We can visualize this data using a graduated style by simply selecting the T_F_MEAN value for the Column field and clicking on Classify. Using the Color method, as shown in the following screenshot, we can pick a Color ramp from the corresponding drop-down list. Additionally, we can reverse the order of the colors within the color ramp using the Invert option:

Graduated styles are available in different classification modes, as follows:

  • Equal Interval: This mode creates classes by splitting at equal intervals between the maximum and minimum values found in the specified column.
  • Quantile (Equal Count): This mode creates classes so that each class contains an equal number of features.
  • Natural Breaks (Jenks): This mode uses the Jenks natural breaks algorithm to create classes by reducing variance within classes and maximizing variance between classes.
  • Standard Deviation: This mode uses the column values' standard deviation to create classes.
  • Pretty Breaks: This mode is the only classification that doesn't strictly create the specified number of classes. Instead, its main goal is to create class boundaries that are round numbers.

We can also manually edit the class values by double-clicking on the values in the list and changing the class bounds. A more convenient way to edit the classes is the Histogram view, as shown in the next screenshot. Switch to the Histogram tab and click on the Load values button in the bottom-right corner to enable the histogram. You can now edit the class bounds by moving the vertical lines with your mouse. You can also add new classes by adding a new vertical line, which you can do by clicking on empty space in the histogram:

Besides the symbols that are drawn on the map, another important aspect of the styling is the legend that goes with it. To customize the legend, we can define Legend Format as well as the Precision (that is, the number of decimal places) that should be displayed. In the Legend Format string, %1 will be replaced by the lower limit of the class and %2 by the upper limit. You can change this string to suit your needs, for example, to this: from %1 to %2. If you activate the Trim option, excess trailing zeros will be removed as well.

When we use the Size method, as shown in the following screenshot, the dialog changes a little, and we can now configure the desired symbol sizes:

The next screenshot shows the results of using a Graduated renderer option with five classes using the Equal Interval classification mode. The left-hand side shows the results of the Color method (symbol color changes according to the T_F_MEAN value), while the right-hand side shows the results of the Size method (symbol size changes according to the T_F_MEAN value).

Note

Note the checkboxes besides each symbol. They can be used to selectively hide or show the features belonging to the corresponding class.

Creating and using color ramps

In the previous example, we used an existing color ramp to style our layer. Of course, we can also create our own color ramps. To create a new color ramp, we can scroll down the color ramp list to the New color ramp… entry. There are four different color ramp types, which we can chose from:

  • Gradient: With this type, we can create color maps with two or more colors. The resulting color maps can be smooth gradients (using the Continuous type option) or distinct colors (using the Discrete type option), as shown in the following screenshot:
  • Random: This type allows us to create a gradient with a certain number of random colors
  • ColorBrewer: This type provides access to the ColorBrewer color schemes
  • cpt-city: This type provides access to a wide variety of preconfigured color schemes, including schemes for typography and bathymetry, as shown in this screenshot:

To manage all our color ramps and symbols, we can go to Settings | Style Manager. Here, we can add, delete, edit, export, or import color ramps and styles using the corresponding buttons on the right-hand side of the dialog, as shown in the following screenshot:

Using categorized styles for nominal data

Just as graduated styles are very useful for visualizing numeric values, categorized styles are great for text values or—more generally speaking—all kinds of values on a nominal scale. A good example for this kind of data can be found in the trees.shp file in our sample data. For each area, there is a VEGDESC value that describes the type of forest found there. Using a categorized style, we can easily generate a style with one symbol for every unique value in the VEGDESC column, as shown in the following screenshot. Once we click on OK, the style is applied to our trees layer in order to visualize the distribution of different tree types in the area:

Of course, every symbol is editable and can be customized. Just double-click on the symbol preview to open the Symbol selector dialog, which allows you to select and combine different symbols.

Creating a rule-based style for road layers

With rule-based styles, we can create a layer style with a hierarchy of rules. Rules can take into account anything from attribute values to scale and geometry properties such as area or length. In this example, we will create a rule-based renderer for the ne_10m_roads.shp file from Natural Earth (you can download it from http://www.naturalearthdata.com/downloads/10m-cultural-vectors/roads/). As you can see here, our style will contain different road styles for major and secondary highways as well as scale-dependent styles:

As you can see in the preceding screenshot, on the first level of rules, we distinguish between roads of "type" = 'Major Highway' and those of "type" = 'Secondary Highway'. The next level of rules handles scale-dependence. To add this second layer of rules, we can use the Refine selected rules button and select Add scales to rule. We simply input one or more scale values at which we want the rule to be split.

Note

Note that there are no symbols specified on the first rule level. If we had symbols specified on the first level as well, the renderer would draw two symbols over each other. While this can be useful in certain cases, we don't want this effect right now. Symbols can be deactivated in Rule properties, which is accessible by double-clicking on the rule or clicking on the edit button below the rule's tree view (the button between the plus and minus buttons).

In the following screenshot, we can see the rule-based renderer and the scale rules in action. While the left-hand side shows wider white roads with grey outlines for secondary highways, the right-hand side shows the simpler symbology with thin grey lines:

Tip

You can download the symbols used in this style by going to Settings | Style Manager, clicking on the sharing button in the bottom-right corner of the dialog, and selecting Import. The URL is https://raw.githubusercontent.com/anitagraser/QGIS-resources/master/qgis1.8/symbols/osm_symbols.xml. Paste the URL in the Location textbox, click on Fetch Symbols, then click on Select all, and finally click on Import. The dialog will look like what is shown in the following screenshot:

Creating data-defined symbology

In previous examples, we created categories or rules to define how features are drawn on a map. An alternative approach is to use values from the layer attribute table to define the styling. This can be achieved using a QGIS feature called Data defined override. These overrides can be configured using the corresponding buttons next to each symbol property, as described in the following example.

In this example, we will again use the ne_10m_roads.shp file from Natural Earth. The next screenshot shows a configuration that creates a style where the line's Pen width depends on the feature's scalerank and the line Color depends on the toll attribute. To set a data-defined override for a symbol property, you need to click on the corresponding button, which is located right next to the property, and choose Edit. The following two expressions are used:

  • CASE WHEN toll = 1 THEN 'red' ELSE 'lightgray' END: This expression evaluates the toll value. If it is 1, the line is drawn in red; otherwise, it is drawn in gray.
  • 2.5 / scalerank: This expression computes Pen width. Since a low scale rank should be represented by a wider line, we use a pision operation instead of multiplication.

When data-defined overrides are active, the corresponding buttons are highlighted in yellow with an ε sign on them, as shown in the following screenshot:

In this example, you have seen that you can specify colors using color names such as 'red', 'gold', and 'deepskyblue'. Another especially useful group of functions for data-defined styles is the Color functions. There are functions for the following color models:

  • RGB: color_rgb(red, green, blue)
  • HSL: color_hsl(hue, saturation, lightness)
  • HSV: color_hsv(hue, saturation, value)
  • CMYK: color_cmyk(cyan, magenta, yellow, black)

There are also functions for accessing the color ramps. Here are two examples of how to use these functions:

  • ramp_color('Reds', T_F_MEAN / 46): This expression returns a color from the Reds color ramp depending on the T_F_MEAN value. Since the second parameter has to be a value between 0 and 1, we pide the T_F_MEAN value by the maximum value, 46.

    Tip

    Since users can add new color ramps or change existing ones, the color ramps can vary between different QGIS installations. Therefore, the ramp_color function may return different results if the style or project file is used on a different computer.

  • color_rgba(0, 0, 180, scale_linear(T_F_JUL - T_F_JAN, 20, 70, 0, 255)): This expression computes the color depending on the difference between the July and January temperatures, T_F_JUL - T_F_JAN. The difference value is transformed into a value between 0 and 255 by the scale_linear function according to the following rule: any value up to 20 will be translated to 0, any value of 70 and above will be translated to 255, and anything in between will be interpolated linearly. Bigger difference values result in darker colors because of the higher alpha parameter value.

    Tip

    The alpha component in RGBA, HSLA, HSVA, and CMYKA controls the transparency of the color. It can take on an integer value from 0 (completely transparent) to 255 (opaque).

Creating a dynamic heatmap style

In Chapter 4, Spatial Analysis, you learned how to create a heatmap raster. However, there is a faster, more convenient way to achieve this look if you want a heatmap only for displaying purposes (and not for further spatial analysis)—the Heatmap renderer option.

The following screenshot shows a Heatmap renderer set up for our populated places dataset, popp.shp. We can specify a color ramp that will be applied to the resulting heatmap values between 0 and the defined Maximum value. If Maximum value is set to Automatic, QGIS automatically computes the highest value in the heatmap. As in the previously discussed heatmap tool, we can define point weights as well as the kernel Radius (for an explanation of this term, check out Creating a heatmap from points in Chapter 4, Spatial Analysis). The final Rendering quality option controls the quality of the rendered output with coarse, big raster cells for the Fastest option and a fine-grained look when set to Best:

Creating a 2.5D style

If you want to create a pseudo-3D look, for example, to style building blocks or to create a thematic map, try the 2.5D renderer. The next screenshot shows the current configuration options that include controls for the feature's Height (in layer units), the viewing Angle, and colors. Since this renderer is still being improved at the time of writing this book, you might find additional options in this dialog when you see it for yourself.

Once you have configured the 2.5D renderer to your liking, you can switch to another renderer to, for example, create classified or graduated versions of symbols.

Adding live layer effects

With layer effects, we can change the way our symbols look even further. Effects can be added by enabling the Draw effects checkbox at the bottom of the symbol dialog, as shown in the following screenshot. To configure the effects, click on the Star button in the bottom-right corner of the dialog. The Effect Properties dialog offers access to a wide range of Effect types:

  • Blur: This effect creates a blurred, fuzzy version of the symbol.
  • Colorise: This effect changes the color of the symbol.
  • Source: This is the original unchanged symbol.
  • Drop Shadow: This effect creates a shadow.
  • Inner Glow: This effect creates a glow-like gradient that extends inwards, starting from the symbol border.
  • Inner Shadow: This effect creates a shadow that is restricted to the inside of the symbol.
  • Outer Glow: This effect creates a glow that radiates from the symbol outwards.
  • Transform: This effect can be used to transform the symbol. The available transformations include reflect, shear, scale, rotate, and translate:

As you can see in the previous screenshot, we can combine multiple layer effects and they are organized in effect layers in the list in the bottom-left corner of the Effect Properties dialog.

Working with different styles

When we create elaborate styles, we might want to save them so that we can reuse them in other projects or share them with other users. To save a style, click on the Style button in the bottom-left corner of the style dialog and go to Save Style | QGIS Layer Style File…, as shown in the following screenshot. This will create a .qml file, which you can save anywhere, copy, and share with others. Similarly, to use the .qml file, click on the Style button and select Load Style:

We can also save multiple different styles for one layer. For example, for our airports layer, we might want one style that displays airports using plane symbols and another style that renders a heatmap. To achieve this, we can do the following:

  1. Configure the plane style.
  2. Click on the Style button and select Add to add the current style to the list of styles for this layer.
  3. In the pop-up dialog, enter a name for the new style, for example, planes.
  4. Add another style by clicking on Style and Add and call it heatmap.
  5. Now, you can change the renderer to Heatmap and configure it. Click on the Apply button when ready.
  6. In the Style button menu, you can now see both styles, as shown in the next screenshot. Changing from one style to the other is now as simple as selecting one of the two entries from the list at the bottom of this menu:

Finally, we can also access these layer styles through the layer context menu Styles entry in the Layers Panel, as shown in the following screenshot. This context menu also provides a way to copy and paste styles between layers using the Copy Style and Paste Style entries, respectively. Furthermore, this context menu provides a shortcut to quickly change the symbol color using a color wheel or by picking a color from the Recent colors section: