In QGIS 3 it is possible to create map themes which remember what layers are visible and what layer styles are used. This feature provides possibility for quick switching between different map styles and is particularly useful in cases when different data sets are available for the same area. In this rather short and simple tutorial we will create a map with two themes. The data for this tutorial is available in this zip-archive.
1. Start by opening start.qgs
map in QGIS. You will see that the file consists of three layers:
Assume that we want to have two different maps:
Map | Traffic Accidents | Roads | Land use |
1 | Show | Color according to type | Show in different colors |
2 | Do not show | All of the same color | Show in different colors |
2. We will begin by creating a theme for the first map. All the requirements for the first map are already in place, so we only need to save the current state as a map theme. Click on the icon in the layers window and select Add theme.
3. Give the theme name Map 1
and click OK. The current theme has been saved.
If you now click on the icon again, you will see that the theme named
Map 1
has appeared in the menu and is selected.
4. Now, let's start preparing the second map. We begin by hiding all the traffic accidents. Click on the checkbox on the left to the layer Traffic accidents
.
5. Before we proceed to changing the layer style, let's save the current state. Click again on the icon and select Add theme..., name this theme
Map 2
. Now you have two map themes: Map 1
and Map 2
.
Try to switch between two themes and you will see that it toggles the visibility of the traffic accidents layer. It means that when you created a theme, QGIS remembered the visibility states of all layers.
6. Now we need to make roads layer look be visible but look differently in the two themes. We will do that using Layer styles. Right-click on the roads layer, select Styles ‣ Add... and enter name Single-color
.
Now you have two styles for the roads layer: default
and Single-color
. Let's rename default
into Multi-color
. Switch to default
style by clicking on it and then go into the same menu again and click Rename Current.... Enter the new name and click OK.
7. Switch roads layer back to Single-color
style by using Styles menu. Now use Layer Styling menu to switch from Categorized
to Single symbol
symbology (or, as an alternative, you can adjust colors of all classes). This will make all roads to be drawn using the same symbol, independently on their type. Select the color as you see fit.
When you modify the symbology style, the changes are automatically saved to the current active layer style. So after you switch to Single symbol
, you can switch between two layer styles to see that the roads symbology changes from one to multiple colors (and vice-versa).
8. While layer styles automatically save related changes when they occur, this is not the case for map themes. If you will swtich to Map 2
map theme now, you will see that roads layer will fall back to multiple colors. In order to associate Single-color
style with roads layer in Map 2
theme, we need to overwrite Map 2
theme. In order to do that, activate Single-color
style for roads layer (right-click on roads layer, select Styles > Single-color), click on the icon and select Replace theme > Map 2. This action will overwrite
Map 2
theme and will save the current state of visibility of layers and all applied styles to this theme.
Now, if you switch between two themes Map 1
and Map 2
you will see that they satisfy our goals.
9. Now let's learn how to use themes in layout. Open a print layout (Project > New Print Layout) and add the first map (Add item > Add map and drag in on the canvas).
10. Force the first layer style on the map by selecting Map 1
in Follow map theme dropdown. You will see that the first theme was applied to the map.
11. Using the same sequence of actions, add the second map to the canvas and apply the second theme to it. Now, you will see both maps showing the same data in different styles.
Note
Map themes do not save everything. The exact description of what exactly map themes save you can find in the official documentation.
Labels can be added to a map to show any information about an object. Any vector layer can have labels associated with it. These labels rely on the attribute data of a layer for their content.
Note
The Layer Properties dialog does have a Labels tab, which now offers the same functionality, but for this example we’ll use the Label tool, accessed via a toolbar button.
The goal for this lesson: To apply useful and good-looking labels to a layer.
Proceed with this tutorial using the map from this zip-archive. Do not worry if your map does not look exactly the same as the one in the pictures. If you are interested in how this map was created, you are welcome to look at this and this pages.
Before being able to access the Label tool, you will need to ensure that it has been activated.
This gives you the Layer labeling settings dialog.
You’ll need to choose which field in the attributes will be used for the
labels. In the previous lesson, you decided that the NAME
field was the
most suitable one for this purpose.
The map should now have labels like this:
Depending on the styles you chose for your map in earlier lessons, you’ll might find that the labels are not appropriately formatted and either overlap or are too far away from their point markers.
That’s the font problem solved! Now let’s look at the problem of the labels overlapping the points, but before we do that, let’s take a look at the Buffer option.
You’ll see that this adds a colored buffer or border to the place labels, making them easier to pick out on the map:
Now we can address the positioning of the labels in relation to their point markers.
2mm
and make sure that
Around point is selected:You’ll see that the labels are no longer overlapping their point markers.
In many cases, the location of a point doesn’t need to be very specific. For example, most of the points in the places layer refer to entire towns or suburbs, and the specific point associated with such features is not that specific on a large scale. In fact, giving a point that is too specific is often confusing for someone reading a map.
To name an example: on a map of the world, the point given for the European Union may be somewhere in Poland, for instance. To someone reading the map, seeing a point labeled European Union in Poland, it may seem that the capital of the European Union is therefore in Poland.
So, to prevent this kind of misunderstanding, it’s often useful to deactivate the point symbols and replace them completely with labels.
In QGIS, you can do this by changing the position of the labels to be rendered directly over the points they refer to.
This will reveal the Quadrant options which you can use to set the position of the label in relation to the point marker. In this case, we want the label to be centered on the point, so choose the center quadrant:
0
:If you were to zoom out on the map, you would see that some of the labels disappear at larger scales to avoid overlapping. Sometimes this is what you want when dealing with datasets that have many points, but at other times you will lose useful information this way. There is another possibility for handling cases like this, which we’ll cover in a later exercise in this lesson.
2.00mm
. You may like to adjust the styling of the point marker or
labels at this stage.1:100000
. You can do this by typing it into
the Scale box in the Status Bar.Now that you know how labeling works, there’s an additional problem. Points and polygons are easy to label, but what about lines? If you label them the same way as the points, your results would look like this:
We will now reformat the roads layer labels so that they are easy to understand.
10
so that you can see more labels.You’ll probably find that the text styling has used default values and the
labels are consequently very hard to read. Set the label text format to have a
dark-grey or black Color
and a light-yellow buffer
.
The map will look somewhat like this, depending on scale:
You’ll see that some of the road names appear more than once and that’s not always necessary. To prevent this from happening:
Another useful function is to prevent labels being drawn for features too short to be of notice.
5mm
and note the results when you click Apply.Try out different Placement settings as well. As we’ve seen before, the horizontal option is not a good idea in this case, so let’s try the curved option instead.
Here’s the result:
As you can see, this hides a lot of the labels that were previously visible, because of the difficulty of making some of them follow twisting street lines and still be legible. You can decide which of these options to use, depending on what you think seems more useful or what looks better.
It has one fields which is of interest to us now: place
which defines the
type of urban area for each object. We can use this data to influence the label
styles.
Edit...
to open the
Expression string builder:In the text input, type: "place" = 'town'
and click Ok
twice:
Notice its effects:
Note
Please do not forget to save the resulting map, because it will be used in the next tutorial!
We can’t cover every option in this course, but be aware that the Label tool has many other useful functions. You can set scale-based rendering, alter the rendering priority for labels in a layer, and set every label option using layer attributes. You can even set the rotation, XY position, and other properties of a label (if you have attribute fields allocated for the purpose), then edit these properties using the tools adjacent to the main Label tool:
(These tools will be active if the required attribute fields exist and you are in edit mode.)
Feel free to explore more possibilities of the labeling system.
Labels are a good way to communicate information such as the names of individual places, but they can’t be used for everything. For example, let’s say that someone wants to know what each landuse area is used for. Using labels, you’d get this:
This makes the map’s labeling difficult to read and even overwhelming if there are numerous different landuse areas on the map.
The goal for this lesson: To learn how to classify vector data effectively.
Work with the map that you have obtained after completing Tutorial 2 in this lab.
You’ll see something like this:
Now our landuse polygons are appropriately colored and are classified so that areas with the same land use are the same color. You may wish to remove the black border from the landuse layer:
You’ll see that the landuse polygon outlines have been removed, leaving just our new fill colours for each categorisation.
Notice that there is one category that’s empty:
This empty category is used to color any objects which do not have a landuse value defined or which have a NULL value. It is important to keep this empty category so that areas with a NULL value are still represented on the map. You may like to change the color to more obviously represent a blank or NULL value.
Remember to save your map now so that you don’t lose all your hard-earned changes!
If you’re only following the basic-level content, use the knowledge you gained above to classify the buildings layer. Set the categorisation against the building column and use the Spectral color ramp.
Note
Remember to zoom into an urban area to see the results.
There are four types of classification: nominal, ordinal, interval and ratio.
In nominal classification, the categories that objects are classified into are name-based; they have no order. For example: town names, district codes, etc.
In ordinal classification, the categories are arranged in a certain order. For example, world cities are given a rank depending on their importance for world trade, travel, culture, etc.
In interval classification, the numbers are on a scale with positive, negative and zero values. For example: height above/below sea level, temperature above/below freezing (0 degrees Celsius), etc.
In ratio classification, the numbers are on a scale with only positive and zero values. For example: temperature above absolute zero (0 degrees Kelvin), distance from a point, the average amount of traffic on a given street per month, etc.
In the example above, we used nominal classification to assign each farm to the town that it is administered by. Now we will use ratio classification to classify the farms by area.
We’re going to reclassify the layer, so existing classes will be lost if not saved.
We want to classify the landuse areas by size, but there’s a problem: they don’t have a size field, so we’ll have to make one.
The new field will be added (at the far right of the table; you may need to
scroll horizontally to see it). However, at the moment it is not populated, it
just has a lot of NULL
values.
To solve this problem, we’ll need to calculate the areas.
You’ll get this dialog:
Now your AREA
field is populated with values (you may need to click the
column header to refresh the data). Save the edits and click Ok.
Note
These areas are in degrees. Later, we will compute them in square meters.
You’ll be using this to denote area, with small areas as Color 1 and large areas as Color 2.
In the example, the result looks like this:
Now you’ll have something like this:
Leave everything else as-is.
It’s often useful to combine multiple criteria for a classification, but unfortunately normal classification only takes one attribute into account. That’s where rule-based classification comes in handy.
"landuse" = 'residential' AND "name" <> 'Swellendam'
(or
"landuse" = 'residential' AND "name" != 'Swellendam'
),
click Ok and choose a pale blue-grey for it and
remove the border:"landuse" <> 'residential' AND "AREA" >= 0.00005
and choose a mid-green color."name" = 'Swellendam'
and assign it
a darker grey-blue color in order to indicate the town’s importance in the
region.These filters are exclusive, in that they collectively exclude some areas on the map (i.e. those which are smaller that 0.00005, are not residential and are not ‘Swellendam’). This means that the excluded polygons take the style of the default (no filter) category.
We know that the excluded polygons on our map cannot be residential areas, so give the default category a suitable pale green color.
Your dialog should now look like this:
Your map will look something like this:
Now you have a map with Swellendam the most prominent residential area and other non-residential areas colored according to their size.
Symbology allows us to represent the attributes of a layer in an easy-to-read way. It allows us as well as the map reader to understand the significance of features, using any relevant attributes that we choose. Depending on the problems you face, you’ll apply different classification techniques to solve them.
QGIS has built-in functions and algorithms to calculate various properties based on the geometry of the feature - such as length, area, perimeter etc. This tutorial will show how to use the Add geometry attributess tool to add a column with a value representing length of each feature.
Given a polyline layer of railroads in North America, we will determine the total length of railroads in the United States.
Natural Earth has a public domain railroads dataset.
Download the North America supplement zip file from the portal.
For convenience, you may directly download a copy of the dataset from the link below:
ne_10m_railroads_north_america..zip
Data Source [NATURALEARTH]
ne_10m_railroads_north_america.zip
file in the Browser panel and expand it. Drag the ne_10m_railroads_north_america.shp
file to the canvas.ne_10m_railroads_north_america
loaded in the Layers
panel. You will see that the layer has lines representing railroads for
all of North America. Now, let’s calculate the lengths of each line
feature. Go to .ne_10m_railroads_north_america
as the Input layer. The input layer’s Coordinate Reference System (CRS) is EPSG:4326 WGS84. This is a Geographic
CRS with Latitude and Longitude as coordinates, WGS84 as ellipsoid and
degrees as units. Because latitude and longitude don’t have a standard
length, you can’t measure distances or areas accurately using planar
geometry functions. Fortunately, QGIS provides a better way to compute
distances using ellipsoidal geometry, which is the most accurate method
for layers spanning large areas such as this. Pick Ellipsoidal
as the Calculate using option. Click Run. Once the process finishes, click Close.Note
If your input layer is in a Projected CRS, you may choose Layer CRS
option for calculation. Local or Regional projected coordinate systems
are designed to minimize distortions over their region of interest, so
are more accurate for such computation.
Added geom info
loaded in the Layers panel. This is a copy of the input layer with a new column added for distance. Right-click the Added geom info
layer and select Open Attribute Table.Note
The Add Geometry Attribute tool adds different set of attributes depending on whether the input layer is points, lines or polygons. See QGIS documentation for more details.
Added geom info
layer and select Filter."sov_a3" = 'USA'![]()
Added geom info
layer in the Layers
panel indicating that a filter is applied to the layer. You can also
visually confirm that the layer now contains line segments only for
United States. Now we are ready to calculate the sum. Click the Show statistical summary button on the Attributes Toolbar.Added geom info
layer and length
column.length
column - meters. Let’s change the computation to use kilometers instead. Click the Expression icon next to the fields drop-down menu in the Statistics panel.length / 1000![]()
Start by opening a new document and add the three .shp
layers from this zip-archive to the view. Chose blue colour for the hydrography and pink for the city polygons. The road net comes from the Swedish National Road Administration which manages a road data base. It stores the geometry of all roads and streets together with a lot of attributes about the road conditions. This is a subsection of the road data base that covers Kristianstad commune. The flow of traffic has been measured and stored in the table connected to the layer.
The task is to visualize the traffic in two maps. The first map shall show different road classes together with the layers hydrography and city limits. You can find this information in the database layer in a field called VAEGKAT
. Chose symbols according to your own ideas and label the roads with their numbers. The World Street Map should not be visible in any of the two maps.
Tip
Read https://docs.qgis.org/3.4/en/docs/user_manual/working_with_vector/vector_properties.html#symbology-properties if you want to learn options for symbology rendering which are not covered in the tutorials.
The second map shall show the traffic intensity as graduated symbols together with hydrography and city limits. The problem is to chose a classification scheme and number of classes that makes the map easy to understand. The road segments shall also be labelled with relevant figures about traffic intensity. You can chose any of the following fields to visualize.
Number of vehicles per day (ADTFORDON
)
Number of trucks per day (ADTLASTBIL
)
Show the result of your classifications on a layout with two map windows, one for each map. The maps should have the same scale (1:250 000 or select an appropriate one) and the same centre (city centre). Other information on the layout shall be a legend, a north arrow, scale text and your name. A report following the template must accompany the map. In the result section of the report you should give a short motivation for your choice of classification scheme and number of classes.