Filtering records in composer (QGIS 2.18)

While producing a map for a colleague I found a way to filter data in a table to specific records using a SQL function.  Having a single view of the table wasn’t an option as the space it took up on the page obscured  part of the map.

Some of you may be familiar with the filter facility in composer but when I’ve used it before it is usually filtering on a specific value in the table i.e. a street name, so the view results in the table view only showing the street I specified. This was different in that I had to have the multiple table views showing specific records.

Each record has a unique ID so it was this that was used in the filter and I remembered that SQL has a “IN” function and decided to try to see if it worked.

It was simple: “ID” IN(1,5,34,67,89) and the view of the table produced only those records with the id of 1 or 5 or 34 or 67 or 89 – who knew?

Below is an image showing the same table twice with different parcel ID’s in each using the IN function (shown in the “filter with” box).


You may not need this very often but just knowing you can do this might just give you another option for map production.


Design better forms in QGIS

Difficulty = if I can do it, you can (based on the: ‘can I do it scale’!)

I’ve been experimenting with dialogue box design just lately for two reasons:

  • I wanted a more intuitive box for the others in my team to be able to enter data based on a workflow rather than the standard straight down approach and to provide check and drop-down boxes for some of the fields.
  • the second reason was for the need to enter data quickly in just two fields, on a table that had loads of fields, the two fields would have been at the top and near the bottom respectively on a normal dialogue and so was a pain tabbing all the way down.

A little research (mainly reading Nathan Woodrow’s blog posts on the subject – don’t be put off by the Python stuff, use the link in his post to the original – you’ll get the idea).  This shows how to link (called binding) the bits (text boxes etc.) on a form to the fields in a table (this bit is the most important ’cause it don’t work if you don’t get it right!).

What you need.

To design a form you will need QT.  If you download and update your installation (MS Windows – I don’t know how this works on a MAC or Linux) using OSGeo4W network installer, it is an option in the ‘advanced’ section – if it isn’t installed, go through the setup process and select the options to install QT.

I don’t propose to go through QT in depth but show the main bits – on starting you will see the start page. You will invariably select the default or open an existing form – if you already design forms then don’t go any further unless you want to feedback (welcomed) on my instructions.


Designing your first form

Selecting the default gives you this:


On the left you will see all the ‘widgets’ that can be added to a form (drag them onto a form) – here I have added ‘Label’ and ‘Line Edit’ widgets. The panels on the right are all the properties associated with the selected widget (or form, if you haven’t got a widget selected) so the content changes depending on what you have selected.


You will add as many widgets as you want and arrange them how you want, you can make the dialogue box bigger or smaller etc. etc. – have a play.  Once you have done, or if you want to come back to it, save the file – it creates a ‘.UI’ file which can be opened next time you start QT.

The important bit – linking to a layer!

To use the form with your layer (I’m using a single table that I created for this example) in QGIS you have to link the ‘Line Edit’ widget to a field in your table.

If you look in the ‘Object Inspector’ panel, on the right, select the widget, it will be highlighted in the panel, you will see it is called ‘lineEdit’, double click this and it highlights blue (on mine), you change the ‘lineEdit’ text to the name of the field in your table – it must be spelt correctly and be the same case as your field name.

To change the label text, you need to select the label on the form and look in the ‘Property Editor’ on the right.  Scroll down to the ‘QLabel’ part and look for the ‘text’ property (the first on that part of the panel). Click into the property and change the value – it changes on the form.

That’s it! You’ve created a custom form.  Save your edits.

Testing it in QGIS

Go into QGIS and open your layer.  Open the layer properties and go to ‘Fields’.


You should be familiar with this if you use QGIS.  For a ‘lineEdit’ widget you don’t need to change anything – in the next episode I will go over using drop-downs and check boxes, for now though you can leave things as they are.

So the field I am linking to is the ‘maptext’ field so I call my ‘lineEdit’ widget ‘maptext’.QTlineEdit

I’ve changed the label so that users will understand what the input is…you get the picture.

Linking the form to the layer.

We now need to tell QGIS to use the ‘.UI’ form when editing or displaying the information in a form.

Use the ‘Attribute editor layout’ drop down and specify ‘Provide UI-file‘.


Another box will appear asking for the path to the ‘UI’ file.


Select the ‘…’ button and navigate to where you saved the ‘UI’ file and select it -you’ve now told QGIS to use this form for this layer.

How does it work?

In a map, add a feature to the layer or, if it is an existing layer, use the ‘identify’ button and click on an existing feature, up will come the dialogue you created – here I’m adding a feature.

Note: the dialogue will open at the size of the last dialogue you opened in QGIS so if it is not big enough, expand it by selecting the bottom right corner – I find this a particularly annoying trait as I would expect the dialogue to open at the size I designed!

So I’ve added a feature, the dialogue appeared, I enter the text as shown, clicked OK.


For this example, I’ve labelled the features with the field into which this text is inserted and also show the table – the text I typed into the dialogue is in the field in the correct record – this way, even if you have 100 fields in your table, you can design a quick data capture form and include only those fields for which you want to enter data.


Trouble Shooting

Dialogue doesn’t appear:

  • check you have told QGIS to use a custom dialogue
  • check you have specified the correct path to the correct ‘UI’ file

Data not going into the table:

  • check that the ‘lineEdit’ widget is called and spelt exactly the same as the field name into which you want the information – this is the most likely cause of data not going into the table.
  • check you have the correct layer selected in the ‘Layers’ panel
  • I don’t know – double-check everything!

BTW: you can keep QT open with the form showing whilst you test it in QGIS, just make changes to the form or add new stuff, save the form, overwriting the previous, and it will appear with the changes as soon as you inspect or add a feature in QGIS.

I hope you found this useful. Next time (soon) I will go into using other widgets but for now, getting these basics sorted will help you get used to the process…have fun!

Creating a vector profile in QGIS


This follows a presentation I gave in 2016 at the QGIS Southwest user group at Hertford University. Of course I was meant to write it up then but with moving jobs I forgot  until a fellow user was having trouble with profile outputs so here is the process I use.

There are a number of plugins required for this:

QProf (for creating the profile and output – you could also use the Profile plugin)

Points2One (to create the vector profiles)

I also had a DSM and DTM and in the process I explain their use.

The area for the example is the Humber Bridge (shown above) and the DSM and DTM are open data from the Environment Agency 1m LIDAR.


Load the DSM and DTM. Locate the area for your profile and either draw a line (I used a scratch layer) or use an existing line layer. I digitised a line along the centre of the bridge from one side to the other being careful not to stray either side – the white line in the figure below. The figure also shows the DSM layer with the DTM below it.


I don’t intend to go through the use of the QProf tool I figure you can do that yourselves but it is easy to use and has many options. Below is an image of the profiles from the line layer. You can see the bridge deck and piers quite clearly also the banks and water surface:


QProf allows you to export in many formats, I choose csv and I create a separate csv for both the DTM and DSM.

Once you have saved the csv files, open it in QGIS using the ‘add csv layer’. You should see 7 columns of figures:

  • ID
  • X
  • Y (I’m doing this in BNG (EPSG:27700) I have not tried it in any other)
  • cds2d (this will be our ‘X’ axis)
  • z
  • cds3d
  • dirslop.

Now here is the bit that you have to get right: we don’t want to use the ID, X or Y as this will put out profile over the map, we want to have a two dimensional view but in the map window so forget about these.

We use the ‘cds2d’ values as our X axis (coordinates) – specify these in the open dialogue.

The ‘z’ column becomes our ‘Y’ axis (coordinates) – specify these in the open dialogue.

The csv will be added to QGIS – you won’t see it in the window because we have transformed the coordinates so that it is in the very lower left corner of the BNG BBox.

Select the layer in Layer Panel and zoom to it, you should see a series of points. Do the same process for both the DTM profile and DSM profile.

Creating the profile lines.

When you are zoomed to the point layer you can transform them into lines or polygons using the Points2One plugin which creates shp files, this process is self-explanatory. Keep the point files as the height data is not retained in the shp files and you can play about with the points to make them vertical lines based on the height value – have a go and see what you can do.

Below are the finished profiles for the Humber Bridge and water/banks, obviously I’ve coloured the water line to look a little more authentic.


Update. Some time after doing this I’ve finally got round to mentioning why this is so good: try adding the results to a composer – because it is a map you can scale it, create grids etc.  You can do a lot more than the usual raster sections you normally get.

Using QGIS Atlas to show single items

Don’t go away – this is not as boring as it seems!

I was asked the other day if I could provide a small overview map for each page of a document containing 30+ individual photos. But, the rub was, that each map had to have only the location symbol relating to that individual photo showing where it was taken from.  The answer was of course, yes.  Then I thought about it and, yes it could be done, with the Atlas feature of QGIS, but I wasn’t sure if I could get only the individual feature on the map, so like all these things the best place to start was a Google search and after a lot of digging I found a number of ideas.  It turned out to be very simple in the end!

Below is the process for achieving the desired result:

I’m using QGIS 2.12 Lyon but it should work in 2.10 Pisa not sure about previous versions – try it.

The map is a basic OS raster and I’ve just created 6 points, each has an ID and Direction attribute,  the direction is to the focal point – it doesn’t really matter for this exercise as the objective is to show how we can produce a map with individual objects, even when they are close together.

I want to produce each map at 1:10,000 scale but this means that all the points will show.

First I create the map in the Composer, add a table (just because I want the information for each point, it isn’t needed for the process though).

Fig1The first setting in the composer is for the map – make sure the ‘Controlled by Atlas’ is checked in the map Item Properties.

If you are using a table to sync data for each object then you need to set a filter ($id = $atlasfeatureid) on the table (see below)

Fig2This results in the appropriate record being shown for each map object.  You can click the ‘Attributes…’ button to restrict which fields are shown, change the header text or set the columns to a fixed size.

In the map, open the layer properties of the layer you are using for the Atlas and set the style to ‘Rule Based’ and set the rule to the same as the filter ($id = $atlasfeatureid).

Back to the composer and select the ‘Preview Atlas’ button on the toolbar – you should now have an individual symbol (works for any map object in fact) in the centre of your map.







QGIS Legend trick – polygon shapes

Whilst doing some work on a plan which involved many layers of information, actually I was trying to replicate the plan created in another mapping system, and part of this process involved making it look exactly like the original as it was part of the planning process documents already accepted and therefore, couldn’t be changed (I would have liked to do it differently from the original but this wasn’t an option).

Getting all the mp layers looking like they should was no problem because using the styling capabilities of QGIS, being so extensive, this was a breeze!  Many layers were brought in from MapInfo (opening the native TAB format) and then styled by attributes using the categorized classification method.

A number of layers were polygons either squares, circles or triangles and although they looked OK on the map the shapes are not replicated in the legend in composer, they appear as a normal (usually) rectangle symbols.  This was seen to be confusing but I didn’t know how to sort it.  Below is a picture of three basic shapes classified by their attribute:

As you can see, it doesn’t matter what the shape is, the legend shows rectangles – this is not helpful especially if there are other map items with the same colour.


Now look at the legend below: it is using the same map items and colours but the legend shows the shape also.  To do this you need to style the polygons as ‘centroid fill’ and select your marker symbol size accordingly This will show your polygon as the selected shape and reflect this in the legend.  The down-side is that the symbol size in the legend can be very much larger than other, normal rectangle symbols (if you’ve set the symbol size in the map to be quite large) so you have to adjust the marker size in both the map and composer legend to get things looking right.


Below are the settings as an example:


I hope you find this usful.

Styling with QGIS

This post is written in response to a question from a QGIS beginner (+Jamal Almuallem) about how I styled some freedata (, he was looking for a tutorial but I didn’t know of one so said I would blog about it.  The picture below is the output and as you can see I’ve applied a style to make some of the features appear as if they are in 3D.



The start of this process is to get some data, I used Ordnance Survey Open Data, Vector Map District, load into QGIS, select the layer and either double-click or right click and select ‘properties’, go to ‘Styles’.  The Settlement layer are all polygons but whether it is line work, points or polygons, the process is the same.

The Layer Properties dialog looks like this:



In the ‘Symbol layers’ section there will be a ‘simple fill’ representing the default colour applied to the layer on loading. You can change this to whatever you like.  The trick to making the buildings look 3D is to add another fill – select the ‘Fill’ at the top of the tree and use the + button to add another fill.  Set the colour, type of fill and an offset see the dialog below for extra options when one of the fills on the tree is chosen:



You can see on the right of the dialog that I have offset the lower fill by 0.5 on both axis. This has the effect of moving the lower fill down and to the left of the top fill.  As a rule I always have the lower fill a darker shade than the top as this imitates shadow.

Click OK and you will see the effect on the map – play around until you find something that you like.  You can save this style to a file for reusing in other projects by using the ‘Save Style’ button.

QGIS Time Manager 2

So I’ve been promising an update to my Timemap series so here is the next stage of tinkering.

This is based on a live demo I did at the first UK QGIS User Group a few weeks ago and is a result of looking at multi-level data within the same time period.  Again, I’ve used railways as the basis, simply because I love railways and loving a subject spurs you on when you’re completing what can be, repetitive tasks.

So the first thing is to set up your basic route [or series], mine is the line between Bletchley and Bedford St. Johns.


©Crown Copyright & Database rights 2012

First thing to mention is that this is created in QGIS 1.8 as the plugin for version 2.0 wasn’t ready.

The line was split into 32 sections (see picture), as it takes 32 minutes to travel between the stations, but you will determine the frequency based on your own time requirements.

  • It is important to make sure you create your records in a sequential manner as the plugin goes through record by record.
  • Each record is given a start and end time (end time not required) – the format of the date/time is important, look at the plugin to determine how you want to format this but make sure you change it in the settings within the plugin else it just won’t work!  I tend to keep to the default setting yyyy-mm-dd hh:mm:ss (you can see how the table is made up from my previous post).
  • I also created a table of points for the stations, with the same structure as the route but a column added which holds the name of the station.  This column is used to label the station.  The point symbols were made to be 100% transparent so they don’t show but were set to be labelled with their name – you don’t have to do this but I only wanted the names to show as the train went by (as it were).
  • Set the start time for each station to correspond with the line segment just preceding it and the end time to however long you want the label to appear (no end time will result in the label staying visible until the end of the whole animation).
  • In the plugin add all the tables you require and run it!

That’s it really, simple to do but attention to the detail and format are key to making it work.

You can see the finished video here:

You are not bound by lines, any object can be made to appear/disappear if it has a date/time – I showed an example of random circles with labels containing information so as long as there’s a temporal aspect to data it will work but better still, it will impress in terms of communicating information.

Have fun!