10 min read

In this article by Alexander Bruy and Daria Svidzinska, authors of the book QGIS By Example, you will learn how to work with styles, including saving and loading them, using different styles, and working with the Style Manager.

(For more resources related to this topic, see here.)

Working with styles

In QGIS, a style is a way of cartographic visualization that takes into account a layer’s individual and thematic features. It encompasses basic characteristics of symbology, such as the color and presence of fill, outline parameters, the use of markers, scale-dependent rendering, layer transparency, and interactions with other layers. Style incorporates not only rendering appearance, but also other things, such as labeling settings.

A well-chosen style greatly simplifies data perception and readability, so it is important to learn how to work with styles to be able to represent your data the best way.

Styling is an important part of data preparation, and QGIS provides many handy features that make this process much more productive and easier. Let’s look at some of them!

Saving and loading styles

Creating good-looking styles can be a time-consuming task, but the good thing is that once developed styles don’t go to nowhere. You can save them for further use in other projects.

When you have finished polishing your style, it is wise to save it. Usually, this is done from the Layer Properties dialog, which can be opened from the layer’s context menu.

QGIS By Example

There is a Style button at the bottom of this dialog. It provides access to almost all actions that can be performed with the layer’s style, including saving, loading, making the style default, and so on.

The style can be saved to the file on the disk (this works for any layer type), or stored in the database (possible only for database-based layers). To save style in the file, perform these steps:

  1. Open the Layer Properties dialog.
  2. Click on the Style button at the bottom of the Properties dialog and go to the Save Style submenu:

    QGIS By Example

  3. Choose one of the available formats. A standard file dialog will open. Navigate to the desired location in your filesystem and save the style.

    QGIS By Example

Currently QGIS provides support for the following formats of saving styles:

  • QGIS style file: The style is saved as a .qml file, which is a native QGIS format used to store symbology definition and other layer properties.
  • Style Layer Descriptor (SLD) file: The style is exported to a .sld file. SLD format is widely used in web cartography, for example, by applications such as GeoServer. It is necessary to mention that currently, SLD support in QGIS is a bit limited. Also, you should remember that while you can save any style (or renderer type) in SLD, during import, you will get either a rule-based or a single-symbol renderer.

If you work with the spatial database, you may want to save layer styles in the same database, together with layers. Such a feature is very useful in corporate environments, as it allows you to assign multiple styles for a single layer and easily keep the styles in sync. Saving styles in the database currently works only for PostgreSQL and SpatiaLite.

To save style in the database, follow these steps:

  1. Open the Layer Properties dialog.
  2. Click on the Style button at the bottom of the Properties dialog and go to the Save Style submenu.
  3. Select the Save in database (format) item, where format can be spatialite or postgres, depending on the database type:

    QGIS By Example

  4. The Save style in database dialog opens. Enter the style name and (optional) description in the corresponding fields, and click on the OK button to save the style:

    QGIS By Example

The saved style can be loaded and applied to the layer. To load a style from the file, use these steps:

  1. Open the Layer Properties dialog from the context menu.
  2. Click on the Style button at the bottom of the Properties dialog and select the Load Style item.
  3. Choose the style file to load.

Loading a style from the database is a bit different:

  1. Open the Layer Properties dialog from the context menu.
  2. Click on the Style button at the bottom of the Properties dialog and go to Load Style | From database.
  3. The Load style from database dialog opens. Select the style you want to load and click on the Load Style button.

    QGIS By Example

With all of these options, we can easily save styles in the format that meets our requirements and tasks.

Copy and paste styles

Very often, you need to apply mostly the same style with really minor differences to multiple layers. There are several ways of doing this.

First, you can save the style (as described in the previous section) in one of the supported formats, and then apply this saved style to another layer and edit it. But there is simpler way.

Starting from QGIS 1.8, you can easily copy and paste styles between layers. To copy a style from one layer to another, perform these steps:

  1. In the QGIS layer tree, select the source layer from which you want to copy the style.
  2. Right-click to open the context menu.
  3. Go to Styles | Copy Style to copy the style of the source layer to the clipboard.
  4. Now, in the QGIS layer tree, select the target layer.
  5. Right-click to open its context menu.
  6. Go to Styles | Paster Style to paste the previously copied style from the clipboard and apply it to the target layer.

It is important to note that QGIS allows you to copy, for example, a polygonal style and apply it to a point or line layer. This may lead to incorrect layer rendering, or the layer can even disappear from the map even though it still present in the layer tree.

Instead of using the layer context menu to copy and paste styles, you can use the QGIS main menu. Both of these actions (Copy Style and Paste Style) can be found in the Layer menu.

The copied style can be pasted in a text editor. Just copy the style using the context menu or QGIS main menu, open the text editor, and press Ctrl + V (or another shortcut used in your system to paste data from the clipboard) to paste the style. Now you can study it.

Also, with this feature, you can apply the same style to multiple layers at once. Copy the style as previously described. Then select in the QGIS layer tree all the layers that you want to style (use the Ctrl key and/or the Shift key to select multiple layers). When all the desired layers are selected, go to Layer | Paste Style. Voilà! Now the style is applied to all selected layers.

Using multiple styles per layer

Sometimes, you may need to show the same data with different styles on the map. The most common and well-known solution to do this is to duplicate the layer in the QGIS layer tree and change the symbology.

QGIS 2.8 allows us to achieve the same result in a simpler and more elegant way. Now we can define multiple styles for a single layer and easily switch between them when necessary. This functionality is available from the layer context menu and the layer Properties dialog.

By default, all layers have only one style, called default. To create an additional style, use these steps:

  1. Select the layer in the layer tree.
  2. Right-click to open the context menu.
  3. Go to Styles | Add.
  4. The New style dialog opens. Enter the name of the new style and click on OK:

    QGIS By Example

  5. A new style will be added and become active.

    It is worth mentioning that after adding a new style, the layer’s appearance will remain the same, as the new style inherits all the properties of the active style.

  6. Adjust the symbology and other settings according to your needs. These changes will affect only the current style; previously created styles will remain unchanged.

You can add as many styles as you want.

All available styles are listed in the layer context menu, at the bottom of the Styles submenu. The current (or active) style is marked with a checkbox. To switch to another style, just select its name in the menu.

QGIS By Example

If necessary, you can rename the active style (go to Styles | Rename Current) or remove it (go to Styles | Remove Current). Also, the current style can be saved and copied as previously described.

Moreover, it is worth mentioning that multiple styles are supported by the QGIS server. The available layer styles are displayed via the GetCapabilities response, and the user can request them in, for example, the GetMap request.

This handy feature also works in the Print Composer.

Using Style manager

Style manager provides extended capabilities for symbology management, allowing the user to save developed symbols; tag and merge them into thematic groups; and edit, delete, import, or export ready-to-use predefined symbology sets.

If you created a symbol and want it to be available for further use and management, you should first save it to the Symbol library by following these steps:

  1. In the Style section of the layer Properties window, click on the Save button underneath the symbol preview window.
  2. In the Symbol name window, type a name for the new symbol and click on OK:

    QGIS By Example

  3. After that, the symbol will appear and become available from the symbol presets in the right part of the window. It will also become available for the Style Manager.

The Style Manger window can be opened by:

  • Clicking on the Open library button after going to Properties | Style
  • Going to Settings | Style Manager

    QGIS By Example

The window consists of three sections:

  1. In the left section, you can see a tree view of the available thematic symbology groups (which, by default, don’t contain any user-specified groups)
  2. In the right part, there are symbols grouped on these tabs: Marker (for point symbols), Line (for line symbols), Fill (for polygon symbols), and Color ramp (for gradient symbols). If you double-click on any symbol on these tabs, the Symbol selector window will be opened, where you can change any available symbol properties (Symbol layer type, Size, Fill and Outline colors, and so on). Similarly, you can use the Edit button to change the appearance of the symbol.
  3. The bottom section of the window contains symbol management buttons—Add, Remove, Edit, and Share—for groups and their items.

Let’s create a thematic group called Hydrology. It will include symbology for hydrological objects, whether they are linear (river, canal, and so on) or polygonal (lake, water area, and so on). For this, perform the following steps:

  1. Highlight the groups item in the left section of the Style manager window and click on the very first + button. When the New group appears, type the Hydrology name.
  2. Now you need to add some symbols to the newly emerged group. There are two approaches to doing this:
  3. Right-click on any symbol (or several by holding down the Ctrl key) you want to add, and select from its contextual shortcut Apply Group | Hydrology.

    QGIS By Example

  4. Alternatively, highlight the Hydrology group in the groups tree, and from the button below, select Group Symbols, as shown in this screenshot:

    QGIS By Example

    As a result, checkboxes will appear beside the symbols, and you can toggle them to add the necessary symbol (or symbols) to the group. After you have clicked on the Close button, the symbols will be added to the group.

  5. Once the group is created, you can use it for quick access to the necessary symbology by going to Properties | Style | Symbols in group, as shown in the following screenshot:

    QGIS By Example

Note that you can combine the symbology for different symbology types within a single group (Marker, Line, Fill, and Color ramp), but when you upload symbols in this group for a specific layer, the symbols will be filtered according to the layer geometry type (for example, Fill for the polygon layer type).

Another available option is to create a so-called Smart Group, where you can flexibly combine various conditions to merge symbols into meaningful groups. As an example, the following screenshot shows how we can create a wider Water group that includes symbols that are not only present in Hydrology already, but are also tagged as blue:

QGIS By Example

Use the Share button to Export or Import selected symbols from external sources.

Summary

This article introduced the different aspects of style management in QGIS: saving and loading styles, copying and pasting styles, and using multiple styles per layer.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here