1 Introduction to QGIS and Setup

This practical is an Introduction to QGIS. QGIS (previously known as Quantum GIS) is a free and open-source desktop geographic information system (GIS) application that provides data viewing, editing, and analysis. Anyone can download it from the Internet and it works on a wide range of operating systems including Windows, Ubuntu and Mac. QGIS is consistent with the principles of Windows software, so if you are familiar with programs such as Word or Excel you should find that similar concepts apply.
Many of the principles of using QGIS are the same in other GIS software, such as ArcGIS and MapInfo. You can apply the principles you learnt here into any other GIS software, but the location and the names of some of the features might be slightly different.

1.1 Learning outcomes:

  • Be able to set up QGIS, display and add data
  • Understand how to add text/numeric data such as .csv/xlsx with a latitude/longitude coordinate
  • Join tabular data to spatial data
  • Understand how to use basic labeling and run basic spatial queries
  • To be able to use Field Calculator to create new values
  • Understand how to create spatial entities (points, lines and polygons)

1.2 Setup

  • Go to Windows Search, type QGIS and select QGIS Desktop 3.38
  • A blank new QGIS project should open

1.2.1 QGIS software

Outside of campus, students will be able to access QGIS via one of at least three possible routes:
(a) Download QGIS to your machine (go to https://qgis.org/download/, then download and install QGIS; ideally you’d choose the 3.38 version - as university computers have this version installed, however the 3.36 version should also be fine - the differences are really minor)
(b) Use Apps Anywhere - https://www.liverpool.ac.uk/csd/apps-anywhere/
(c) Connect remotely to a University PC - https://www.liverpool.ac.uk/it/pc-centres/remote/

1.2.2 Data

Once you have your access to QGIS sorted, the next step involves downloading the required data files for this practical to your designated directory on the M:drive/Documents (the university shared drive). It is important to use the M:Drive because it allows you to access your data, saved work, etc. from any University Managed Windows machine, as well as from home and any place in the world, provided you have internet access.

  • Open the Documents folder and create a new folder named GIS.
  • Then create a new folder and name it ‘Prac1’; this is where you will store all the data copied and created during this practical session.

As there are several files, they have been compressed into a single zip file, and you will need to first copy this and then extract the data files. Follow the instructions on screen to download the zip file from:

  • Canvas>Modules>Teaching and learning materials>Week 1. Download the folder called: Prac1_data
  • Unzip the data into your GIS>Prac1 folder

2 Practical exercise - Intro to QGIS

2.1 Loading data

Let’s start off by adding data to the map display.

  • Start by clicking the Open Data Source Manager button Open Data Manager and then Vector button AddVectorLayer.png

  • Next, locate the “Browse” button adjacent to the “Source>Vector Dataset(s)” window and navigate to your designated working directory, most likely within the “Documents/GIS/Prac1” folder.
    Change the option in the drop down menu on the bottom right to ESRI Shapefiles (.shp). Select the world_countries.shp file, click Open,and then click the Add and Close button.

  • Keep the coordinate system (CRS) as EPSG:4267 - NAD27 if the system asks you to change it (click on the Cancel button)

  • This will add a layer (a polygon shapefile) which shows the countries of the world

Note If you end up with a different CRS then go to Project>Properties>CRS and type 4267 in the search window, select NAD27 - EPSG:4267, then Apply> OK

2.1.2 Changing display colours

When we added the world_countries shapefile, QGIS randomly assigned a colour. We can change this to something more sensible - green, for instance.

  • Right click on the world_countries layer in the Layers Panel on the left hand side of the screen, and click on Properties
    Properties

  • This will open the Layer Properties window, where we can change many different options for how the layer is displayed, as well as other options about the layer.

  • Click on the Symbology option on the left hand side.

  • To change the colour, click on the drop down menu next to the Color option

  • You can then either select one of the preset colours, or select a colour from the colour picker. Use either option, and change the colour to something you like.

  • Click OK (and OK again if necessary) and this will close the Layer Properties window and update the colour on the map.

The options you have in this layer will depend on what type of spatial data you are dealing with. The countries layer is a polygon layer, so we can change the colour of the polygons. Point layers or line layers will have different options. Try experimenting with different colours, and other options in the Symbology section of the Layer Properties window. Click on Simple fill and play with various options e.g. Fill style,Stroke color, Stroke style etc.

2.1.3 Panels and Toolbars

You might have noticed that when QGIS started, many toolbars and panels were displayed. Let’s tidy it up by displaying only the buttons we need.

  • Right-click on the toolbar (grey area at the top) and you should see something similar to the menu below:
    Toolbox

  • You can click different panels and toolbars on/off by checking their checkbox. Spend some time turning the toolbars on/off to see what the different options do. Once you are finished, leave the following toolbars with their checkboxes checked : Layers Panel; Attributes Toolbar; Label Toolbar; Manage Layers Toolbar, Digitizing and Map Navigation Toolbar.

This is a basic set of tools to get you started; we will turn on others as we require them.

  • You can also use the handle on each tool bar to drag the toolbars around and arrange them as you wish: Handle

Once you have done this setup phase, QGIS should remember it, but if you use the software on a different computer you may need to follow these instructions again.

2.1.4 Adding new data

Using the same steps as previously (or select Layer > Add Layer > Add Vector Layer > Browse (…)) add two more shapefiles to the map. The files to add are: world_deg30.shp and world_rivers.shp shapefiles. To select multiple files, select the one at the top of the list, hold down Shift on the keyboard and click on the bottom file.

QGIS will add the layers, and again assign a random colour. However, you may not be able to see all the data. This is because the order of the different layers in the Layers Panel is important.

  • Re-order the layers (by dragging them up or down) so they are in this order (from top to bottom): world_rivers, world_countries and world_deg30.
  • Now you should be able to see the three different data sets. We can rename the layers as well - right click on world_deg30 and select Rename Layer. Rename this layer to Oceans_Grid and press enter.
  • Also use the Identify Features tool to investigate some of the new data layers.
  • Now is a good time to save the QGIS project file. Select Project > Save As.. and save the file somewhere sensible - ideally to your M:Drive.

The colours of the different layers will also need to be changed, so update the colours to reflect what the layers represent. Remember also to save the project file!

2.2 Adding point data from a delimited CSV file

We have already added polygon data and line data, and now we are going to look at adding some point data. Go to your M:drive/Documents folder and open up the world-cities.csv file included in the zip file - it should open in Excel. You can see the file contains four columns: Name, Latitude, Longitude and Country. As this data has some coordinates, QGIS can plot the data on our map. Close the file (without saving, if Excel asks), and return to QGIS.

  • Click Layer > Add Layer > Add Delimited Text Layer, click the Browse button and select the world-cities.csv file.
  • You should see the different columns appear at the bottom of the window.
  • Make sure that File format is set to CSV and Geometry definition is set to Point coordinates.
  • Check that X field is set to Longitude and Y field is set to Latitude.
  • The Geometry CRS is set to EPSG:4267 - NAD27 (please note, we use this Coordinate Reference System because all other layers in this practical are using this particular CRS; other popular CRS is WGS84 EPSG:4326; make sure all your layers are using the same CRS as otherwise you may experience issues with displaying your layers).
  • Click Add then Close

You should now see the 15 cities added to the map. Update the symbols to a colour that fits in with your existing map. See if you can work out how to add a name to each of the cities. Look in the Layer Properties window > Labels.
WorldMap

2.3 Exploring spatial data

An important component of each GIS file/layer is the data associated with it. Tabular information is the basis of geographic features, allowing you to visualize, query, and analyse your data. In the simplest terms, tables are made up of rows and columns, and all rows have the same columns. Tables allow you to map and visualize your data. For example, you can’t generate text to label each feature, perform spatial queries or classify and categorize attributes to symbolize a layer.

In GIS packages, spatial data is typically accessed through the Attribute Table.

  • Right click on the world countries layer and then on Open Attribute Table
  • Note that there are various columns showing different information for all 165 countries. There is a lot of useful information about each country such as land area, population, GDP, life expectancy and so on…
  • Check what spatial information is available for other layers that we have used

2.3.1 Labels (basic)

  • Now, we will label the cities (if you have already managed to add labels, skip to the next point)
  • Right click on the world cities layer and select Properties
  • Go to Labels tab and from the drop down menu on the top select Single labels
  • Choose Name from the Value drop down menu and click OK
  • You can play with various label properties if you wish, but we’ll focus on the more advance options in the coming weeks

2.3.2 Spatial queries (basic)

The basic spatial queries we are going to use in this practical are: select by attribute, select by location and export selected features. These are basic, but very important functions and are very often used in any GIS analysis. You will be required to select some features and extract them almost in all the practicals, so please pay extra attention to the exercise below.

  • Open Attribute Table of the world countries layer

  • Click on Select features using an expression button Select_features , type “NAME” = ‘Canada’ and click Select features and then Close

  • In the Attribute Table, from the drop down menu on the bottom left hand corner choose Show Selected Features

  • Now your attribute table should only display one row with the data associated to Canada

  • Next, in the main QGIS tool bar go to Vector > Research Tools > Select by location and try to select only those rivers that flow through Canada

  • If you are not sure how to do it, use the picture below and then click Run and Close
    Canadian)Rivers

  • You can see that only those rivers that flow through Canada have been highlighted - 15 in total (you may need to turn off the world countries layer to see the selected rivers)

  • Next create a layer of the selected rivers by right clicking on the world rivers layer > Export > Save Selected Features As..

  • Choose ESRI Shapefile as your Format, browse to your working directory and save it as Canadian Rivers (use the Browse button to navigate to your working directory)

  • The new layer should be automatically added to the Layers Panel

  • Right click the Canadian Rivers layer > Zoom to Layer and then label the rivers

  • Repeat this exercise for Brazil (how many rivers have you got selected this time?)

2.4 Joining tabular data (Video 1.1)

We only need the world_countries layer for this exercise, so turn all of the other layers off by clicking on the check box next to each layer entry in the Layers Panel.

Not every dataset you want to use comes as a Shapefile or in a spatial format (in fact, the majority do not!). Often data will be in the format of a report, spreadsheet or table that you need to link to your existing spatial data for use in some analysis. For example, you may be working with population data for a country where the data is in a table, csv, Excel file (.xls, .xlsx), or any other non-spatial format. To map this data, you have to merge this with a Shapefile containing country boundaries. This operation is known as a Join and this section will cover how to carry out table joins in QGIS. To demonstrate this we will join a table with population data to our world_countries layer.

In order to join tabular data it is necessary that both of your datasets have a common attribute (e.g. a name, unique reference or code). This attribute must be unique (i.e. a country must not be listed twice in your Attribute table) and identical (i.e. the country names must be the same and spelt identically, e.g. Gambia and The Gambia will not join successfully).

  • Open the Attribute table of the world_countries layer and you’ll see that the NAME field is unique (different) for each country.

  • Close the Attribute table.

  • In the data files that you copied today, there is also an Excel file named Pop.xls. Double-click this file and it should open in Excel. The dataset contains three fields, a unique identifier called NAME, POP2011 and POP2021. Close the Excel file once you have finished viewing the data.
    Now that we’ve identified the two fields in our datasets that we will use to make the join, we can move on.

  • To add our table to the map, we need to use either the Add Vector Layer button or just simply drag and drop the table into the Layers Panel

  • The table will now appear in the Layers Panel

  • Right-click on the table and Open Attribute Table to check that the data appears correctly (if you have all null values there has been a problem with importing your data ( remove it from the Layers Panel and add it again). You should see both fields with data populated, row 0 containing Afghanistan with a population of 25,500,000 people. Close the attribute table once you are finished with viewing the data

  • Now we will join this table with your spatial data. Right-click on the world_countries layer, select Properties and click on the Joins option

  • Click on the plus button Plus to create a new join. The Add Vector Join dialogue box will now open

  • Make sure that Pop is selected in the Join layer dropdown box

  • NAME should be selected in the Join field dropdown box

  • NAME should also be selected in the Target field dropdown box
    Join

  • Hit OK, and then OK again

  • Open Attribute Table of the world_countries layer and you will see that two new fields (named Pop Pop_2011 and Pop Pop_2021) have been added to the Attribute table! Please see the available video if you are unsure how to do it.

2.4.1 Making layers permanent (Video 1.2)

The join that we have made is not permanent; it is a temporary link between the two datasets. To make the Join permanent we need to save a new copy of the world_countries layer.

  • Right-click the world_countries layer and click on Export > Save Feature As
  • In the Save vector layer window make sure you have the following setup:
  • Format - ESRI Shapefile
  • File name - world_countries_updated (click Browse to navigate to your working directory; click Save)
  • Leave all other fields as they are
  • Check the Add saved file to map box
  • Hit OK and the new layer will be added to the map. Open the attribute table to check that the new fields (Pop Pop_20…) are present. However, the new field name that is given automatically isn’t very appealing, neither useful so let’s change it. We’ll need to use a tool called Refactor fields to do that. Follow the steps below:
  • Click on the Toolbox button Toolbox, then type Refactor in the search box
  • Double click on the Refactor fields tool
  • Find the Field name Pop Pop_2011 and rename it to Pop_2011. Then do the same for Pop Pop_2021 and click Run
  • A new layer called Refactored appears in the Layers Panel
  • This is a refactored version of the world_countries_updated layer. The refactored version is only temporary, so make it permanent by clicking on…, yes: Export > Save Feature As and name it appropriately (e.g. world_countries_Pop)

The Refactor fields tool is very useful and you can do a number of operations in here, e.g. you can delete columns or change precision of the decimal numbers; try to set the precision of the Pop_2011 layer to 1 and see what happens
- Remove the redundant layers world_countries_updated and Refactored and then Save your project

2.5 Adding a new field and calculating values (Video 1.3)

We will now add a new field to the attribute table and calculate some values (the change in population between 2004 and 2011) using a simple mathematical operation. This can be useful when you want to create new data or undertake some analyses.

  • Open Attribute Table of the world_countries_Pop layer

  • Click on the Toggle editing mode button Toggle

  • Click on New field button NewCol

  • Set Name to POP_DIFF

  • Leave Comment blank

  • Set Type to Whole number (integer), change Length to 10.

  • Hit OK

In this instance we are dealing with whole numbers, so we can use the integer setting. If we were calculating something else (for example, % population change) which involves decimal numbers, we would need to select Decimal number (real), and set Length to 10 and Precision to 2 or 3.

You will see that the new field contains values, by default, as NULL. We now need to populate this new field (column) with some data.

  • Click on the Open field calculator button FieldCal and the Field calculator window will open. It is here that we can undertake all mathematical calculations

First, we need to update the POP2004 field and then we perform a simple calculation to populate the newly created field.

  • Check the checkbox next to Update existing field and select the field named POP2004 from the dropdown box

  • Under the Function List (middle window) group click on the expand plus sign next to Fields and Values

  • Double-click the field named POP2004 to add it to the Expression window, then type * 1000 so the expression is as follows: POP2004 * 1000, and hit the OK button

  • Then, still in the field calculator, check the box next to Update existing field and select the field named: POP_DIFF from the dropdown menu

  • Under the Function List (middle window) group click on the expand plus sign next to Fields and Values

  • Double-click the field named Pop_2011 to add it to the Expression (the full name of the field has been cut off)

  • Now click on the minus (-) sign and double-click on POP2004. Your expression should look like this: “Pop_2011” - “POP2004”(notice how the expression builder puts in the double quote marks for you)

  • Hit OK to run the calculation and the POP_DIFF field will populate with the calculated values (the first few values should be 75000 (Luxembourg), 632000 (Switzerland) and 5644000 (France), ask if this is not the case)

  • Finally, click on the Toggle editing mode button and choose Save when prompted. Close the attribute table

  • Save your project.

2.6 Classifying Population Change

Now we have calculated the change in population between 2004 and 2011, we need to display it on the map.

  • Open the Symbology section of the Layer Properties window for the world_countries_pop layer.
  • Where the option says Single symbol at the top, change this to Graduated.
  • A new set of options will appear below. There are a range of different options here, which we will explore in a bit. For the moment, select the POP_DIFF field from the Column drop-down list.
  • The click Classify, you will see the 5 classes for this data set appear in the window.
  • Click OK, and QGIS will update the map.

Now we have a map of population change. However, how informative it is, are there any problems with it? There are a number of things you can do to alter the classification:

  1. Try changing the classification method (Mode), the default is Equal Interval. How do the different methods change the classification of the data.
  2. Try changing the number of classes.
  3. Try changing the class boundaries – use the Histogram tool in QGIS.

WorldPopChange
You may notice that some countries are missing from the map – for instance, Burma and Taiwan. This is because their data is missing from the files – you will see NULL written in place of the number, which means the number is missing. You do need to be aware of how missing values are represented, because each data set is likely to use a different setting. Some population data uses a value of -99 to represent a missing value, which will play havoc with the population change calculation. Some will also use a value of 0, which is more subtly misleading, and can often be missed, because a value of 0 population (meaning a missing value) is very different to a value of 0 (meaning a population count of 0).

2.6.1 Population difference (Optional)

  • Now, please attempt to calculate this yourself by finding the difference between ‘Pop_2011’ and ‘Pop_2021’
  • Display the newly created layer and observe where the population has grown/declined in the past decade.

Once you have finished, remember to save your QGIS Project. If you wish to open this up on another computer, it should be fine provided that you’re using the University M: Drive.

Please also remember that if you change the location of your files e.g move them to a different directory or change a folder, those layers won’t display in QGIS. In such case you’ll need to either move them back to the original destination or reset the source of your data using a plugin called: changeDataSource

2.7 Basemap and digitizing spatial entities

2.7.1 Adding a Basemap

We have the option of adding a base map to the data in QGIS. We can do this through plugins called QuickMapServices. To install any plugin follow the steps below:

  • Click on Plugins > Manage and Install Plugins..
  • Then select the required plugin and click install.
  • Once installed, they can be accessed through the Web menu at the top of the window.
  • Use Search QMS window to add various basemaps e.g. Open Street Map (OSM)

2.7.2 XYZ Tile Server (optional)

However, there is a new core functionality XYZ Tile provider which was implemented with some other nice enhancements for tiled services. This means, that there is no need for an external plugin although for an easy setup you can still use external plugins. Layers added this way: load faster, support reprojection, support printing and are cached in a powerful way. - In the Browser Panel, locate the Tile XYZ > right click New connection

Browser Panel

  • Enter the URL of the service which you would like to use, replacing x, y and z parts with curly brackets substitution.
  • Enter name for the background map > click OK

Some example URLs are below:

OpenStreetMap http://tile.openstreetmap.org/{z}/{x}/{y}.png

Google Hybrid https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}

Google Satellite https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}

Google Road https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}

2.7.3 Creating spatial entities (point, line & polygon shapefiles) (Video 1.4)

Data capturing is of key importance in GIS, and these types of operations are often called digitizing. It is a process of converting geographic data either from a hard copy or a scanned image into vector data. During the process, features from the map or image are captured as coordinates in either point, line, or polygon format.

In this exercise, we will create a point shapefile by adding London to the world-cities layer and then a line shapefile by adding River Thames to the world rivers layer.

  • First, make the world-cities layer permanent by right clicking on the layer > Export > Save Feature As (follow the steps from part 2.4.1)

  • Then, go to the world-countries layer and select the extent of the UK by using a simple spatial query in the Attribute table, as you did in part 2.3.2

  • Still in the Attribute table, click on Zoom map to the selected rows

  • Right click on the world-cities layer > Toggle editing

  • Turn on the basemap, and find London

  • Click on the Add Point Feature button Add_point_feature from the digitizing toolbar

  • Hover over the area where London is and then left click to add a new point

  • A dialogue box with Attribute table of the new feature will open up

  • In the Name field type London, click OK

  • Right click the world-cities layer > Open Attribute Table, find London and fill in the remaining fields, close the table

  • Right click the world-cities layer again > Toggle Editing, save all the changes that you have made

  • You have just created a new spatial entity by adding the new point shapefile (London in this case).
    London

  • Now, try to repeat the abobe steps for the world-rivers layer and digitize the Thames River

It may be a bit more tricky to digitize line features, so if there are any difficulties, follow the steps below including some tips:

  • Make sure that the world-rivers layer is in editing mode (Toggle Editing)

  • Click on the Add Line Feature button and start digitising a new line feature by adding new nodes following the River Thames (no need to add the full length of the Thames River; beginning around Reading area should be sufficient)
    Thames

  • To finish digitizing a line, right click and enter the required information about the newly created entity

  • You can of course re-visit the new entity and for instance alter its shape, extent or include more detail

  • To add a new/move an existing node (vertex) go to the Vertex Tool NodeTool , select an area (on the line) where you want to place a new vertex and hold Shift + double click on it

  • To delete an existing vertex, click on the Vertex, highlight the vertex and press Delete

  • To move a vertex: click on a vertex to enable vertex editing for that feature. Left click on vertex you want to move, drag it to new location, left click again.

  • If you are happy with the new entity, right click the world-rivers layer again, click on Toggle Editing and save all the changes that you have made

  • Save the project

So far, the new spatial entities that we have digitized used schemas (data structure) of the existing layers, rather than creating new ones. Both types of operations are really common in GIS, so in this last exercise we will create a new polygon layer called UK Islands.
If you turn the background map and world-countries layer on and then Pan the map to the South Coast where Southampton and Portsmouth is, you will notice that the largest English island (home to a popular music festival) is missing. So your task is to create a new polygon layer and digitize the Isle of Wight.

  • Make the world-countries layer semi-transparent: right click on the layer > Properties>Style tab and set the Opacityy to 50%

  • Tidy up a bit the existing UK boundary around Hampshire coastline by Toggle Editing the world countries layer and moving the existing vertices as required (use the Vertex Tool)
    SCoast

  • Then go to Layer > Create Layer > New Shapefile Layer

  • Hit the Browse button next to File Name find your working directory and and type UK Islands

  • In the window that pops up make sure that you select Polygon as the new layer Type

  • Choose the coordinate system (EPSG:4267 - NAD27)

  • Create an additional Text data field, call it Isle, click on Add to fields list
    New Shapefile

  • Click Ok

  • The new layer has been added to the Layers Panel. From now on, you can follow the instructions on digitising a line feature - the same principle applies

  • Right click on the UK Islands layer > Toggle Editing

  • Click on Add Feature from the main menu and start digitising the polygon by following the Isle of Wight coastline

  • To finish your digitising, right click and populate the window that appears, click OK

  • Right click on the UK Islands layer >Save Layer Edits> Toggle Editing

  • Save the project

2.7.4 Digitizing (Optional)

Now, try to digitize the Isle of Man, which is situated to the north west of Liverpool, by following the above steps. Once you finish with it, you are done with this practical. Well done!

Please note: This practical introduces you to only basic digitizing/editing; this is sufficient for our exercise, and as you have probably noticed it is more accurate that the provided layer of the world-countries, however often a much higher degree of accuracy and precision is required. Especially when we digitize adjacent polygons, the nodes (vertices) need to be snapped to each other in order to avoid polygons overlapping. At times, it may not be possible to do it manually and therefore there are tools which can do the job for us. It is called tracing/auto tracing tool as it allows you to trace existing features. For instance, such tool is enabled by default in ArcGIS but in QGIS you need to install an additional plugin called: Auto Trace. Using the Trace tool is beyond the scope of this practical but if you are interested, use this hyperlink to get more info.