QGIS Planet

Exploring variables in QGIS 2.12, part 1

It’s been quite some time since I last had a chance to blog and a lot has happened since then. Not least of which is that QGIS 2.12 has now been released with a ton of new features that I’ve neglected to write about! To try and get things moving along here again I’m planning on writing a short series exploring how variables work in QGIS 2.12 and the exciting possibilities they unlock. First, let’s look into how variables can be used with QGIS map composer…

So, let’s get started! A new concept introduced in QGIS 2.12 is the ability to set custom variables for use in QGIS’ expression engine. The easiest way to do this is through the “Project Properties” dialog, under the “Variables” section:

Default project variables

Default project variables

You’ll see in the screenshot above that a blank project includes a number of read-only preset variables, such as @project_path and @project_title. (All variables in QGIS are prefixed with an @ character to differentiate them from fields or functions). You can add your own variables to this list by clicking the + button, as shown below:

Adding new variables to a project

Adding new variables to a project

Here I’ve added some new variables, @project_version and @author. Now, any of these variables can be used anywhere that you can use expressions in QGIS, including the field calculator, data defined symbology, labelling, map composer text, etc. So, you could make a map composer template with a label that includes the @author, @project_version and @project_path variables:

Variables in a composer label

Variables in a composer label

Sure, you *could* also manually enter all these details directly into the label for the same result. But what happens when you have multiple composers in your project, and need to update the version number in all of them? Or you move your project to a new folder and need to make sure the path is updated accordingly? Manually updating multiple composers is a pain – make QGIS do the work for you and instead use variables! This would especially be helpful if you’re saving map composer templates for use across multiple projects or users. Using variables will ensure that the template is automatically updated with the right details for the current project.

Another neat thing about QGIS variables is that they can be inherited and overridden, just like CSS rules. Opening the options dialog will also show a Variables group for setting “Global” variables. These variables are always available for your QGIS installation, regardless of what project you’re working on at the time. If your workplace tends to reorganise a lot and constantly shuffle your department around, you could add a global variable for @work_department, so that changing the global variable value in one place will automatically filter through to any existing and future projects you have.

Global variables

Global variables

And like I mentioned earlier, these variables are inherited through various “contexts” within QGIS. If I reopen the Project Properties dialog, you’ll see that a project has access to all the global variables plus the variables set within that specific project. In addition, by adding a variable with the same name to the Project variables the value of the Global variable will be overridden:

Overridden variables

Overridden variables

There’s also a variable editor within each individual composer’s properties tab, so variables can also be set and overridden on a composer-by-composer basis within a project. It’s a really flexible and powerful approach which both simplifies workflows and also opens up lots of new possibilities.

Stay tuned for more on this topic – this topic has only just scratched the surface of how expression variables have changed QGIS! (You can also read part 2 and part 3)

Exploring QGIS 2.6 – Item panel for map composer

In recent releases QGIS’ map composer has undergone some large usability improvements, such as the ability to select and interact with multiple items, and much improved navigation of compositions. Another massive usability improvement which is included in QGIS 2.6 is the new “Items” panel in the map composer. The panel shows a list of all items currently in the composition, and allows you to individually select, show or hide items, toggle their lock status, and rearrange them via drag and drop. You can also double click the item’s description to modify its ID, which makes managing items in the composition much easier.

QGIS composer’s new items panel

This change has been on my wish list for a long time. The best bit is that implementing the panel has allowed me to fix some of the composer’s other biggest usability issues. For instance, now locked items are no longer selectable in the main composer view. If you’ve ever tried to create fancy compositions with items which are stacked on top of other items, you’ll know that trying to interact with the lower items has been almost impossible in previous QGIS versions. Now, if you lock the higher stacked items you’ll be able to fully interact with all underlying items without the higher items getting in the way. Alternatively you could just temporarily hide them while you work with the lower items.

This feature brings us one more step closer to making QGIS’ map composer a powerful DTP tool in itself. If you’d like to help support further improvements like this in QGIS, please consider sponsoring my development work, or you can contact me directly for a quote on specific development.

And now… colour preview modes in QGIS’ map canvas

As a quick follow-up to my last post on colour preview modes for the print composer in QGIS 2.4, this feature has also been added to the main map canvas window! Now it’s even easier to adjust your symbol colours and immediately see how they’d appear under a range of different circumstances:

Colour previews modes for the map canvas

Colour previews modes for the map canvas

 

Colour blindness and grayscale previews in QGIS 2.4

Since QGIS 2.4 is nearing feature freeze it seems like a good time to start exploring some of the great new features in this release. So, let’s get started with my most recent addition to QGIS’ print composer… preview modes!

As every first year cartography text book will tell you, it’s important to know your target media and audience when creating a usable map. Some important considerations are whether or not your map will be photocopied or printed in black and white, and whether you need to consider colour blind map readers in your audience. In the past, designing maps with these considerations has been a time consuming, tedious process. You’d have to export your map, open it in another graphics editing program, apply some colour transform, work out what issues there are, flip back to QGIS, make your changes and repeat. If you’re working with a tight deadline it can be difficult to justify the time this all takes.

QGIS 2.4 will help to make this whole process a lot simpler. In the print composer there’s now an option to enable a number of different live “preview modes“. These include grayscale, monochrome, and two colour blindness simulations (Protanope and Deuteranope).

Composer preview modes in QGIS 2.4

Composer preview modes in QGIS 2.4

These preview modes are live, so you can continue to edit and tweak the colours in your composition while a preview mode is active! For a quick demonstration, let’s start with this creatively coloured thematic map:

bad_colored_map

While it might not be the most aesthetically pleasing map, at least the thematic colours can be easily matched to their corresponding values in the legend. Let’s see what would happen if we photocopied this map. This is as easy as activating the “Simulate photocopy (grayscale)” preview mode:

greyscalepreview

Hmm… not so usable now. The five thematic colours have been reduced to just three discernible colours. Oh well, at least we haven’t had to export our map to find this out, and it’s nice and easy to adjust the colours and composition to work for photocopies without having to leave QGIS to test the results!

Let’s see how this map would look to someone with colour blindness, by activating the “Simulate colour blindness (Protanope)” mode:

color_blindness

In this case, our map isn’t too bad. The different classes are still discernible and the map can be interpreted by someone with protanopia.

So there we have it – now it’s easy to determine how our map outputs will look under different circumstances and adjust them to suit! Composer preview modes will be a part of the upcoming 2.4 release of QGIS, which is due out at the end of June 2014.

Update:

This feature has also been added to the main map canvas.

Composition styling in QGIS 2.2

Here’s a quick run-down on some new feature in QGIS 2.2 which I never got around to writing about before the release. I feel like I’ve got to give these features their due publicity before moving on to all the exciting new stuff which is being added for 2.4. So, without further ado, let’s take a dive into print composer shape and page styling in QGIS 2.2…

Shape styling

It’s no secret that QGIS has pretty impressive capabilities when it comes to cartographic styling of polygon features. Everything from line and point pattern fills, SVG image fills, gradients and even buffered gradients (new in 2.4 — more on that in a later post) can be used to shade polygons. That’s all in addition to the whole range of line styles which can be used to outline the edges of polygons. In QGIS 2.2 all these fill effects are now available for styling shapes in the print composer. What exactly does this mean?

Well, now you can draw a frame onto your print layout and style it with a gradient fill…

Gradient shape fill in composer

…or a line pattern fill….

Line pattern fill in composer

…or some crazy combination of everything…

Advanced fill in composer

It’s totally up to you how far you take this! Here’s a nice example of a map created in QGIS 2.2’s print composer using these new styling options.

Page styling for compositions

Why is this cool? Well, for a start, if we take a quick look at the QGIS map showcase on Flickr very few of the maps shown there have a white background. In previous versions of QGIS achieving a non-white background would require drawing a giant coloured rectangle over your whole composition, banishing it to the back of the stack, and then continually being annoyed by it getting in the way while you tried to work on the rest of the composition. Now, just like the shape styling described above, you can style the page background using any of the available options in QGIS for polygon fills!

Creating a composition with a black page background

Creating a composition with a black page background

It doesn’t end there though. Since the page background can now be styled like this, it’s also possible to have transparent or semi-transparent page backgrounds. I’ll show the result opened here in GIMP so that you can see the full transparency effect over GIMP’s checkerboard background pattern:

A composition exported with a transparent background

A QGIS 2.2 composition exported with a transparent background

Using a transparent composition background like this also allows for transparency effects in map layers to show through – so, for instance, if your map layer is set to 50% transparent then the resultant export from the composer will also be 50% transparent.

And now for the final stinger…

Have I mentioned yet that you can also use data defined symbology for both shape and page styling? No? Well, this was actually my main motivator in adding styling support to these elements. For a long time I’ve been wanting to create atlases which vary the page background based on attributes in the atlas coverage layer. Think flip-book style maps, where the page border is colour-coded to highlight areas that need attention. For example, areas with high rates showing with red borders, average rates with yellow, and low rates with green borders. Using a combination of page and shape styling, data defined symbology, and QGIS’ atlas features, this is now possible!

…And that (belatedly) wraps up my exploration of new features in QGIS 2.2. Next up I’ll start showcasing all the sweet new features which have landed for 2.4…

Atlas previews in QGIS 2.2

QGIS 2.2 includes some great additions to the map composer’s “Atlas” feature. If you’re not familiar with atlas prints they are QGIS’ equivalent of ArcGIS’s “data driven pages”, or something like a map based version of Microsoft Word’s “mail merge”. In an atlas composition you can select one of your map layers to use as a “coverage layer“, and QGIS will automatically generate multiple pages from the composition with each page highlighting a different feature from this coverage layer.

Atlas Previews

Thanks to funding from SIGE, I’ve added some useful new features to QGIS’ atlas abilities for 2.2. The first of these is the ability to preview atlas compositions before printing them. In QGIS 2.0, atlas generation took a bit of guess work. You’d set up the parameters for the atlas, then export the whole atlas in one shot and just hope you’d got the settings right. If not, you’d have to tweak the settings and export the whole lot again to see the result. But not any more! Now, in QGIS 2.2, you can switch on a live atlas preview mode by clicking “Preview Atlas” in the new atlas preview toolbar:

The new atlas preview toolbar

The new atlas preview toolbar

The composer window will switch to showing you a preview of exactly how the atlas will look when exported. You can tweak the appearance of any layout item, adjust the atlas and map parameters, or experiment with the new options for atlas feature styling to see instantly what the final export will look like.

The composer window in atlas preview mode

The composer window in atlas preview mode (complete with gratuitous use of label rotation and rounded rectangles…) 

When this Atlas Preview mode is enabled, you navigate through all the features in the coverage layer by clicking any of the navigation buttons in the atlas preview toolbar:

Navigating the atlas preview

Navigating the atlas preview

While previewing you can export individual pages from the atlas. So, if just one or two pages in your atlas need to be individually tweaked you can do that as you step through the features. A neat thing with this is that you can make temporary tweaks to the extent and scale of the map items as you go, without affecting how the rest of the atlas maps look.

(Oh, by the way, I should mention that as an added bonus QGIS 2.2 lets you control more than one map with an atlas print!)

Selecting the current atlas feature

The second part of the work funded by SIGE was creation of shortcut actions for selecting the current atlas feature. If your atlas coverage layer has many records it may not be practical to step through the atlas previews one at a time until you find a specific feature. That’s where these new shortcut actions come in handy!

There’s a few ways of jumping directly to a specific atlas feature. The first is to open a browser window for your coverage layer, then right click a row and choose “Set as atlas feature for …“:

Setting the atlas feature from the browser window

Setting the atlas feature from the browser window

Selecting this menu item will cause the composer to immediately jump to the matching atlas row. Another way of selecting the current atlas feature is to use the “Set as atlas feature” map action. You activate this by first selecting your coverage layer in the layers panel, then clicking the “Run Feature Action” tool button and selecting “Set as atlas feature…“:

The set atlas feature map action

The set atlas feature map action

The mouse cursor will change to a cross-hair, and clicking any matching feature in the map window will cause the composer atlas preview to jump straight to that feature. Lastly, you can also activate the “Set as atlas feature” action directly from the identify results window.

That’s just a small taste of some of the new atlas creation features which will be available in QGIS 2.2, coming your way by the end of February 2014!

(One last note – as mentioned, this work was kindly sponsored by SIGE. If there’s a specific composer based feature or bug you’d like me to work on, I’m available for further sponsored work. Just contact me directly for details.)

QGIS – Two neat features in 2.2

Here’s a quick run-down on two nice new styling options which I’ve recently added to QGIS 2.2.

Map styling for compositions

This little feature was suggested by Mathieu Pellerin, who is always pushing the boundaries of QGIS’ cartographic tools and coming up with great ideas for new styling features (you can check out some of his work via Flickr). Mathieu’s idea was for a new ‘$map‘ variable for the expression builder. This variable holds the id of the map item which is drawing the map, and allows for some nice tweaking of maps in the composer.

The $map variable is most useful when you have more than one map in your composition. The example below shows $map being used to change the styling of a single layer from the main map to the smaller inset map:

Using $map to style two maps with different colours

Using $map to style a single layer in two maps with different colours

In this example the composition has two maps, the larger has an id of “main_map” and the smaller has “inset_map“. The boundary layer has been styled using the rule based renderer, with one rule for $map=’main_map’ and one for $map=’inset_map’, as shown below:

Rule based rendering using the $map variable

Rule based rendering using the $map variable

The end result is that the layer will be rendered using the two different styles depending on which composer map item it is being drawn into. This trick can also be used to tweak labelling rules between the maps. In the example above I’ve restricted the labelling to only show in the main map. This is achieved by setting an expression for the data defined “Show label” property. I’ve used the expression “$map=’main_map’” so that labels are only shown in the main map and not the smaller inset map.

Tweaking label settings using the $map variable

Tweaking label settings using the $map variable

This small addition to QGIS 2.2 allows for some rather powerful improvements to multi-map compositions!

Drawing polygon borders only inside the polygon

The second new feature I wanted to highlight is a new option for polygon outlines which causes the outline to be drawn only on the inside of a polygon feature. The usual behaviour is for outlines to be drawn directly over the centre of the feature boundary, so that half of the outline is drawn inside the feature and half on the outside.

Simple Line Fill before

This means that the outline in a simple line symbol layer overlaps into the neighbouring polygons, and the result is that outlines from these features blend together:

Shaded borders pre QGIS 2.2

Shaded borders pre QGIS 2.2 – see how the colours bleed into the neighbouring features and overlap

This looks like a big muddy mess. A feature I’ve wanted for a long time is the ability to restrict these outlines so that they are only drawn inside the feature. This effect is commonly seen in world atlases and National Geographic maps, where each neighbouring country is shaded with it’s own unique outline colour. Now it’s possible to do this in QGIS just by ticking a single box!

The new "Draw line only inside polygon" option

The new “Draw line only inside polygon” option

As you can see in the above image, the simple line outline style has a new checkbox, “Draw line only inside polygon“. Ticking this box will clip the outline so that only the portion of it which falls inside the feature is rendered. Here’s the result:

Shaded borders with "Draw line only inside polygon" checked

Shaded borders with “Draw line only inside polygon” checked

So much nicer then the earlier output – now none of the borders overlap into their neighbouring regions! Ok, so it is possible to achieve a similar result by creating a specially crafted layer consisting of negatively buffered polygons subtracted from the original polygons, but this takes a lot of fiddling around. It also has the major disadvantage in that the result is scale dependant, and zooming in or out of the map will alter the size of the polygon outlines. But using this wonderful new checkbox in QGIS, we get proper scale-independent borders, and zooming in or out of the map keeps a consistent border width!

Zooming in keeps a consistent border width...

Zooming in keeps a consistent border width…

So there we go – two small new features added in QGIS 2.2 which have huge potential for your cartographic outputs! As per usual, if you come up with some fancy way of utilising these, don’t forget to add your maps to the QGIS Showcase on Flickr.

Waiting for QGIS 2.2 – Composer Improvements (part 3)

Following on from parts 1 and 2, here’s some more composer changes which are coming in QGIS 2.2

  • Rotation support for all composer item types. Now anything you draw in a composer can be rotated, including scale bars, legends, attribute tables and html frames! Rotation of text labels has also been improved by making the border and background of labels respect the rotation of the label.
Every composer item can now be rotated...

Every composer item can now be rotated…

  • Resizing of rotated items has been improved. Now it’s possible to easily resize rotated items while keeping their correct shape. (There’s still one missing ingredient for complete support here – shear/perspective transforms. Unfortunately this will probably have to wait till 2.4).
Better resizing of rotated items

Better resizing of rotated items

  • Rulers can be shown or hidden in compositions
  • The ruler appearance has been tweaked, adding smaller divisions and better text placement
The ruler appearance has been tweaked

New tweaked appearance for rulers

  • A zoom to actual size button and short cut (Ctrl + 1) have been added
Zoom to 100%

New Zom to 100% button

  • Lastly, the status bar has a new zoom combo box, which shows the current zoom level and allows for quick zoom to several predefined levels. You can also enter an exact zoom level in the box for precise control.
New zoom levels combo box in the status bar

New zoom levels combo box in the status bar

As you can see, the print composer in QGIS 2.2 just keeps getting better! There’s a few other really exciting new additions which have landed recently too, but they deserve their own blog posts. Stay tuned…

Waiting for QGIS 2.2 – Composer Improvements (part 2)

Following on from Part 1, here’s some more recent usability improvements which have been made to QGIS’ map composer:

  • Multiple items can now be selected by drawing a box around them. Holding Shift while drawing a box adds to the current selection, holding Ctrl will subtract from the current selection. In addition, holding Alt while drawing the selection box changes from an “intersects” style selection mode to a “within” selection mode. This was my major frustration with the composer in earlier versions of QGIS, and combined with the earlier fixes for multi-item drag and resize it substantially improves the design workflow.
Selecting multiple items with a mouse drag

Selecting multiple items with a mouse drag

  • Grid lines are always drawn on top of compositions
  • There’s new menu options for showing the grid and snapping to grid, and for showing/snapping/smart guides. All these have convenient keyboard shortcuts so you can quickly switch on or off display of the guides and grid or temporarily switch on or off snapping while you work. There’s also a new menu item for clearing all guides from a composition.
Grid and guide controls in the view menu

Grid and guide controls in the view menu

  • The composer window now has a status bar which shows the exact cursor position and details about resizing/moving items
The new composer status bar

The new composer status bar

  • A new mouse-based zoom tool has been added to the composer, which allows you to click and drag to specify an area to zoom to. You can also switch to this new tool at any time by holding Ctrl and space. Holding Shift while using the zoom tool switches to zooming out.

That’s it for now, but there’s loads more to come for 2.2!

Waiting for QGIS 2.2 – Composer Improvements (part 1)

In the spirit of depesz’s “Waiting for Postgresql 9.#” series (and BostonGIS’ follow up Waiting for PostGIS 2.#), here’s the first in a new series of “Waiting for QGIS 2.2″ posts…

As I mentioned in my last post, one of my major goals for QGIS 2.2 is to make the print composer behave more like a professional DTP package. A big part of this is making sure QGIS respects all the expected standard behaviour for graphic design programs. While the print composer made huge advances toward this goal in version 2.0, there’s still further we can go.

So, let’s start with a list of all the new usability features the print composer has already gained for QGIS 2.2:

  • Selections of multiple items can all be moved and resized together (previously only one item in the selection would be altered)
  • Mouse handles are always drawn on the top of the composition – in earlier versions mouse handles could be hidden by items sitting higher in the composition
  • Holding shift while resizing items locks their aspect ratio
  • Holding control while resizing items causes them to resize from their centre
  • Holding shift while moving items locks the movement to the horizontal or vertical
  • Holding control while moving items temporarily disables any grid or guide-based snapping
  • Items can be removed from a selection by shift-clicking them
  • Control-clicking an item causes the next item below it to be selected – this allows you to select items which are hidden behind other items
  • Holding shift while moving items using the cursor keys results in a large item movement
  • There’s a new “Edit” menu in the composer, with options like
    • Select All/Select None and Invert Selection
    • Select Next Item Above and Select Next Item Below
    • Lock Selected Items and Unlock All
The new composer "Edit" menu

The new composer “Edit” menu

Additionally, there’s a bunch of pull requests which haven’t yet been merged to master, but are ready to go, including:

  • Adding a dedicated “pan” tool for dragging around compositions
  • Compositions can be panned at any time by holding the space key or middle mouse button
  • Compositions can be zoomed by using the mouse wheel
Composer pan tool

Composer pan tool

Some of these are simple little changes which don’t sound like much, but it’s not until you’re forced to work without something like shift-resizing to lock the aspect ratio that you realise how often you use it and rely on it!

A neat trick in QGIS 2.0 – images in atlas prints

Here’s a cool trick which you can do in QGIS 2.0. It builds on two new features introduced in version 2.0 – atlas prints and html labels.

Atlas prints (previously available as a plugin, now integrated into QGIS core) were developed by Oslandia, and allow you to create data driven map layouts from a specified coverage layer. You can read more about them here.

Another new feature in QGIS 2.0 is the ability to render composer labels as html (courtesy of Olivier Dalang). This allows all kinds of fantastic effects, such as formatting text in the middle of a label (using <b>, <i>, and <font> tags) or creating labels which contain HTML tables. You can even use CSS stylesheets and rules to format the HTML! I’ve been told JavaScript also works inside the labels, but I’ve yet to try this out.

You can combine these two new features for some great tricks. Let’s say we’d like to create a set of maps of local councils, and we want each map to have a watermarked logo of the council on it. For this example I’ve created a basic basemap of Victorian councils, and I’ve downloaded all the council logos (in a variety of formats) to a local folder. Next, I’ll add an extra column to the council layer containing the name of the logo image:

adding_logo_column

Adding a logo column to the table

Then, we’ll throw together a simple composition containing the map and set it up as an atlas print:

setting_up_atlas

Generating an atlas

Now for the fun bit. I’ll add a label item to the composer, set it to “Render as HTML”, and insert some specially-crafted html:

The magic HTML label...

The magic HTML label…

For copy/paste purposes here’s the label contents again:

<style>
* {margin: 0px; padding: 0px}
</style>
<img src="file:///home/nyall/GIS/council_logos/[% "logo" %]"
style="width: auto; height: 100%; display: block; margin: 0px 0px 0px auto;" />

There’s a couple of things to note here. First, the magic happens in the image’s src attribute (“file:///home/nyall/GIS/council_logos/[% "logo" %]“). When the composer is exported, QGIS will replace the [% "logo" %] part with the contents of the logo field for each row in the councils table. This means the image source will then point to the local copy of the council’s logo, eg “file:///home/nyall/GIS/council_logos/glen_eira.jpg” for the first row.

Secondly, I’ve styled the image with the css:

width: auto; height: 100%; display: block;

This allows the image to resize to 100% of the height of the label while maintaining its correct aspect ratio. I’ve also added the rule

margin: 0px 0px 0px auto;

to force the image to right align within the html label. This ensures that all the watermarked logos will appear in a consistent size and position for each map.

Lastly, I’ll remove the label’s frame and background by unchecking these options, then set its transparency to 80% under the new “Rendering” section:

Yet another new feature in QGIS 2.0...

Yet another new feature in QGIS 2.0…

Ok, we’re all done. Now, when I select Composer -> “Export as Image”, we’ll get a lovely set of council maps complete with watermarked council logo!

A watermarked atlas!

A watermarked atlas!

There we go — all ready for print, with no manipulation in external programs required at all!

Bonus post-credits section

Here’s a kicker — the linked images don’t need to be local. That means you can even piggy-back off an existing web service to generate an image on the fly! Let’s say you were asked to add QR codes to your maps to link directly to the council website. All it takes is adding a new column to the table, then modifying the image src to read:

src="http://qrfree.kaywa.com/?l=1&s=8&d=[% "url" %]"

Now when we export the maps we’ll also get a QR image generated on the fly and inserted into the layout!

Complete with dynamically generated QR code!

Complete with dynamically generated QR code!

Combining HTML labels and atlas prints in this way is extremely powerful. This example is just touching the tip of the iceberg – I’m keen to see what the community can do with this when QGIS 2.0 is released!

Back to Top

Sustaining Members