Mapping in QGIS For Health User's Manual v.1 PDF
Mapping in QGIS For Health User's Manual v.1 PDF
1. Introduction to mapping with QGIS and the HTC Data Use Tool ...... 6
1.1. Preparing Data in Excel ................................................................................................................. 6
1.2. Mapping in QGIS ........................................................................................................................... 8
1.2.1. Adding shapefiles .................................................................................................................. 8
1.2.2. Verify Coordinate Reference System .................................................................................... 9
1.2.3. Importing Excel data ........................................................................................................... 11
1.2.4. Confirming Data Import ...................................................................................................... 12
1.2.5. Joining data ......................................................................................................................... 14
1.3. Stylizing, Formatting and Managing Maps ................................................................................. 16
1.3.1. Creating a Graduated Map.................................................................................................. 16
1.3.2. Adding Labels ...................................................................................................................... 19
1.3.3. Adding additional layers ..................................................................................................... 21
1.3.4. Formatting Line Layers ........................................................................................................ 21
1.3.5. Zooming to and panning features on a map ....................................................................... 23
1.4. Creating a shapefile from x,y Coordinates .................................................................................. 26
1.4.1. Requirements for displaying coordinate point-locations ................................................... 26
1.4.2. Importing coordinate data .................................................................................................. 26
1.4.3. Specifying a Coordinate Reference System ........................................................................ 28
1.4.4. Formatting Point Layers ...................................................................................................... 30
1.5. Finalizing and exporting maps .................................................................................................... 32
1.5.1. Rules for constructing map layouts .................................................................................... 32
1.5.2. Creating a map layout in Print Composer ........................................................................... 32
1.5.3. Adding a map to the map layout ........................................................................................ 34
1.5.4. Adding a Title ...................................................................................................................... 35
1.5.5. Drawing a border ................................................................................................................ 36
1.5.6. Adding and formatting a legend ......................................................................................... 37
Version 2.0 | 2
1.5.7. Indicating scale and direction in a map layout ................................................................... 38
1.5.8. Exporting map layout .......................................................................................................... 39
1.5.9. Saving Project files .............................................................................................................. 40
1.5.10. Opening a Project file .......................................................................................................... 40
1.6. Advanced Mapping Techniques in QGIS ..................................................................................... 41
1.6.1. Adding a base layer ............................................................................................................. 41
1.6.2. Adding Secondary labels ..................................................................................................... 43
1.6.1. Editing Attribute Data ......................................................................................................... 45
1.6.2. Rule-based Styles for Points................................................................................................ 46
Version 2.0 | 3
How to Use This Module
This module offers a systematic approach to mapping using QGIS mapping software with the HIV testing
and counseling (HTC) Data Use Tool and is meant to accompany the HTC Data Use Tool User’s Manual.
Although this guide uses only HTC examples, the Tool and examples can be adapted to different data-
use questions related to HIV prevention, treatment, and care as well as other programmatic areas.
Users of this manual are required to download QGIS 2.2.0-Valmiera, a free software program created
under a General Public License. QGIS 2.2.0-Valmiera is compatible with Windows XP (or higher), MacOS
X, Linux and FreeBSD. There are no formal system requirements, however 1 GB RAM and 1.6 GHz
processor are generally recommended.
The manual is designed to provide users with a solid background and understanding of the mapping
process. It is most useful as a preparation tool for those who will take part in an HTC Data Use and
Strategic Planning workshop or those who have completed a workshop and may need to refer back to
the concepts and processes at a later time.
All materials related to this manual and the HTC Data Use and Strategic Planning Tool can be found here:
http://globalhealthsciences.ucsf.edu/prevention-public-health-group/global-strategic-information-
gsi/monitoring-and-evaluation/hiv-testing
Version 2.0 | 4
Vocabulary for Mapping
* Administrative levels – An area of a country defined for the purposes of government or
administration such as region, province, or district. Shapefiles used for mapping are
distinguished by administrative levels where higher number administrative levels represent a
smaller administrative level. For example in Tanzania, administrative level 0 is the national level,
administrative level 1 is the regional level, administrative level 2 is the district level, and
administrative level 3 is the ward level.
* Coordinates – Numerical values that represent specific point-locations on the surface of the
earth. Geographic coordinates are used to represent locations on a three-dimensional sphere;
they are defined both in terms of latitude (horizontal) and longitude (vertical) degrees.
Geographic coordinates are calculated and recorded using a GPS receiver. Projected coordinates
are used to represent locations on a two-dimensional flat surface (such as a computer screen);
they are represented as discrete x and y values.
* Coordinate Reference System (CRS) – A set of rules for assigning coordinates to real-world
locations. Because the Earth is a three dimensional oblong sphere, geographic data must be
stretched or compressed in order for (1) images to visualized on a flat surface, and (2) spatial
relationships to be measured (e.g., distance, area). GPS receivers record coordinates by
completing a set of calculations defined by the Coordinate Reference System (CRS). If the CRS is
changed, a different set of coordinates will be produced for the same location. When using a
GPS receiver to gather coordinates, the specific CRS must be specified and recorded so that
shapefiles can be accurately created from the coordinates. The most common geographic
coordinate system is the World Geodetic System 84 (WGS84).
* Geographic Information System (GIS) – A system used to capture, manage, analyze and display
geographic information. This system works by assigning (or “referencing”) data to a geographic
location on the earth’s surface to create a digital representation of the world. Geographic
Information Systems can geographically display non-spatial data, such as prevalence or testing
coverage, by associating values with specific locations or areas on a digital globe or atlas.
Version 2.0 | 5
1. Introduction to mapping with QGIS
and the HTC Data Use Tool
Mapping software use geographic information systems (GIS) and health data from national, regional, or
district levels to create simple maps that show spatial relationships. For example, maps can show the
relationships between:
To create a map, mapping software (e.g., QGIS, ArcGIS, Epi Info™) links a program or survey data file to
coordinate data stored in shapefiles (files made up of latitude and longitude geographic coordinates) by
matching the same attribute between the two files such as a Unique ID or administrative level. This
linked data is manipulated in the mapping software to display values and labels from the data as colors
or points on a map as shown in Figure 2. Important information, such as roads and water, can be added
to the map as additional layers.
Figure 2
Throughout this chapter, we will use data from Tanzania to answer the question: Where do the HIV
prevalence rates differ in magnitude?
Data to be imported into QGIS will first need to be made into a table using the excel-based HTC Data Use
Tool. Column A of your table should be the geographic level to be displayed on the map (e.g. region,
Version 2.0 | 6
district, ward, etc). The subsequent columns of the table should be all indicators to be mapped. Please
refer to the HTC Data Use Tool User’s Manual for instruction on how to create tables in the tool.
For a step-by-step video tutorial on “How to prepare data for use in Epi Info™,” download the .wmv file
from the UCSF website listed in the introduction, or stream through this link:
http://youtu.be/Gv4c2_X4WYE
After creating your table in the HTC Data Use Tool, make a copy of your table and paste the values into a
new Excel 2003 workbook. Edit your new workbook as follows (Figure 3).
Note: Any discrepancies between the Excel data and the Shapefile should be edited in Excel to match the
contents of the Shapefile.
Figure 3.
For the purposes of this manual, use the workbook under the name TZ_THMIS_15_49ALL.xlsx.
Version 2.0 | 7
1.2. Mapping in QGIS
After saving the data in as an Excel table, begin using QGIS mapping software.
1. Open QGIS (Figure 4). The map design window is now visible.
Figure 4
Figure 5
3. Browse to where the Tanzania shapefile is saved on your computer [Tanzania_adm1.shp] (Figure
6).
Version 2.0 | 8
Figure 6
4. Click Open in the Add Vector Layer window. The Tanzania administrative boundary shapefile is now
open in the QGIS application window (Figure 7).
Figure 7
Version 2.0 | 9
Figure 8
2. In the properties window, select the General tab in the left hand pane to view the CRS (Figure 9).
WGS84 is listed under ‘Coordinate reference system’ as the CRS for this shapefile.
Figure 9
As will be discussed in Section 1.4.3, shapefiles created from raw coordinates require that the CRS is
entered manually.
Version 2.0 | 10
1.2.3. Importing Excel data
In order to link a shapefile to data, you must first import the excel data file into QGIS.
Figure 10
3. Browse to your Tanzania data Excel table created in Section 1.1 (TZ_THMIS_15_49ALL.xls) (Figure
11). If you do not see the file you are looking for, change the file type to “All files (*)”.
Figure 11
4. Click Open.
Version 2.0 | 11
5. Click Open in the Add Vector Layer window. The Excel data will now be available as a table in the
Layer menu.
Figure 12
2. Select Open Attribute Table. Notice that our Attribute Table now contains the same information as
the Excel file from Section 0 (Figure 13). While this data looks similar, view the file’s metadata to
determine whether QGIS is representing numerical data as actual numbers and not text strings.
Version 2.0 | 12
Figure 13
4. To view the Metadata, right-click on the table (TZ_THMIS_15_49ALL) and select Properties.
5. In the Layer Properties window, select the Fields tab (Figure 14). Each row in the metadata should
correspond to a field column in our Attribute table. Under “Type” the geographic variable (e.g.
Region) should be list as QString (text data) while indicators (e.g. HIVPrev and HIVpos) should be
listed as either Real or integer (numerical data).
Version 2.0 | 13
Figure 14
1. Open the Properties window for the shapefile that will be used in the join.
2. Click the Joins tab and select the Add vector join button (Figure 15).
Version 2.0 | 14
Figure 15
In the Add vector join window, we will specify what data will be joined to the shapefile and what fields
should be used to match the data. “Join layer” and “Join field” dropdown menus refer to the dataset,
while the “Target field” dropdown menu refers to the shapefile (Figure 16).
3. Select the data to be joined from the Join Layer drop down menu [TZ_THMIS_15_49ALL], the name
of the geographic field in the table [Region] and the geographic field in the shapefile [Name_1]
(Figure 16).
Figure 16
Version 2.0 | 15
5. Click OK to close the Properties window.
6. Reopen the shapefile’s attribute table to ensure that the Attribute table now has additional
indicators signifying that the join was completed successfully (Figure 17).
Figure 17
Version 2.0 | 16
Figure 18
4. Click the Column arrow, scroll to and select the indicator from your dataset you would like to
show on your map [TZ_THMIS_15_49ALL_HIVPrev] (Figure 19). QGIS automatically divides the
entire range of regional HIV prevalence values into 5 intervals of equal size: 1.5 -4.34; 4.34 – 7.18;
7.18 – 10.02; 10.02 – 12.86; 12.86 – 15.70.
Figure 19
Note that the upper bound of each interval appears to be exactly the same as the lower bound of the
next. While not showing it, QGIS considers the lower bound of each interval as belonging to the interval
preceding it. For example, while it appears that the value 4.34 is included in both the first and second
intervals; in reality, QGIS only includes this value in the first interval. Therefore, if a region’s HIV
Version 2.0 | 17
prevalence is exactly 4.34, it will be colored white – not baby-blue. To reflect this convention, we need
to edit the interval labels.
5. Under Label, double click the range and increase the lower bound. For this example, double click
4.3400 – 7.1800 and type “4.35% – 7.18%”.
6. Repeat this process for each subsequent interval until all labels accurately reflect its contents as in
Figure 20.
Figure 20
7. Click OK. The map now shows five classifications of HIV prevalence ranging from lowest to highest
values with darker colors representing higher values (Figure 21).
Figure 21
Version 2.0 | 18
1.3.2. Adding Labels
Geographic features such as regions, provinces, or districts can easily be labelled using any data found in
the attribute table (e.g. Region Names, indicator values, etc.). In this example, each region of Tanzania
will be labeled by displaying on the contents of the ‘NAME_1’ column in the Attribute Table.
3. In the Labels window, click the checkbox to the left of “Label this layer with” to enable labeling
(Figure 22).
4. Click the arrow in the dropdown menu to the right of “Label this layer with” and select which
column in the attribute table to show as labels [NAME_1].
Figure 22
5. To highlight or ‘buffer’ the labels so they are more visible, select the Buffer options within the
Labels window (Figure 23).
Version 2.0 | 19
Figure 23
7. Click OK. Region labels outlined in white are now applied to the map (Figure 24).
Figure 24
Version 2.0 | 20
1.3.3. Adding additional layers
Additional map layers can be added to a map for further analysis such as roads and facility locations.
QGIS stacks layers from the bottom up, so larger features – such as polygons – should be on the layer
below smaller features – such as points and lines. For this scenario, we will be adding a road shapefile on
top of the region shapefile.
4. Click Open once more to add the road shapefile to the map (Figure 25). Be sure the TZA_roads
layer is above the Tanzania_adm1 layer in the layers window. To move a layer, click and drag that
layer to its desired location in the list.
Figure 25
Version 2.0 | 21
3. Within the Style tab, click the rectangle Color button (Figure 26).
Figure 26
4. In the Select Color window, click the soft grey color under Basic colors (Figure 27).
Figure 27
Version 2.0 | 22
6. Click OK to close the Properties window. (Figure 28). Roads are now easily discernible from the
administrative boundaries.
Figure 28
Figure 29
2. In the map display, click and drag the cursor to draw a rectangle around the area you wish to
Zoom in [Dar-Es-Salaam] and then release the mouse button (Figure 30).
Version 2.0 | 23
Figure 30
3. The resulting map displays only the zoomed area of Dar-Es-Salaam (Figure 31).
Figure 31
Version 2.0 | 24
4. To pan to a different area, in the navigation tool bar, click the Pan button .
5. On the map, click and drag the cursor to show what you want displayed in the window. In this
example, moving the pan tool upwards will display the area below Dar-Es-Salaam, Pwani (Figure 32).
Figure 32
6. To zoom out, click the Zoom-Out tool in the navigation bar and click anywhere in the map.
Notice that with each click, more of Tanzania can be viewed with less detail.
7. To return to the original view (as seen in Figure 30), click the Zoom to Full tool in the
navigation toolbar. This will zoom out to reveal the extents of all layers contained within our map.
Alternatively, if we want to view a particular layer, right-click on the desired layer and select Zoom
to Layer Extent (Figure 33).
Figure 33
Version 2.0 | 25
1.4. Creating a shapefile from x,y Coordinates
GPS receivers calculate and record specific locations on the earth’s surface as x & y coordinates – or
longitude and latitude, respectively. These coordinates can then be downloaded on to a computer and
read by GIS software for spatial display. In this section, we will cover how to import and display GPS
coordinates for health facility locations in Tanzania.
The user must know what Coordinate Reference System (CRS) was used by the GPS receiver to
create the coordinates. As mentioned, the CRS provides a set of rules for defining spatial
relationship between coordinates. Just as GPS receivers use a CRS to calculate coordinates, QGIS
uses the CRS to align the shapefiles created under different systems. The table of coordinates
does not along contain information of the CRS – users will be prompted to specify the CRS when
importing the coordinates into QGIS.
Coordinate data must be in decimal degrees (i.e., 18.4567°). If coordinates are formatted as
degrees, minutes and seconds (i.e., 18°27’24.12”) consult with a GIS expert on appropriate
conversion techniques.
Columns for longitude and latitude must be correctly labelled in the coordinate data file.
Coordinate columns can alternatively be labelled x and y respectively.
Version 2.0 | 26
Figure 34
2. In the Create a Layer from a Delimited Text File window, click browse (Figure 35).
Figure 35
3. In the next window, browse to the appropriate folder and select the coordinate data file
[Facilities_Tanzania.csv].
4. Click Open to return to the Create a Layer from a Delimited Text File window (Figure 36). Notice
that the table preview includes x and y coordinates labelled “Long” and “Lat” respectively.
Version 2.0 | 27
Figure 36
5. Under Geometry definition, select “Long” for the X field and “Lat” for the Y field (Figure 37).
Figure 37
6. Click OK.
1. Under Coordinate reference systems of the world, click the plus-box next to Geographic
Coordinate Systems to expand the selection (Figure 38).
Version 2.0 | 28
Figure 38
2. Scroll through the Coordinate Reference Systems until you reach WGS 84 and click to select
(Figure 39).
Figure 39
3. Click OK. A layer called Facilites_Tanzania is now created (Figure 40). Notice that the new layer
nicely overlays the regional layer.
Version 2.0 | 29
Figure 40
3. Click the rectangle next to Color to open the Select Color window.
5. Click the Size text box and enter “0.75” (Figure 41).
Version 2.0 | 30
Figure 41
Figure 42
Version 2.0 | 31
1.5. Finalizing and exporting maps
We have now created a well-designed map that illustrates regional variations in HIV prevalence in
Tanzania. However, to facilitate use in decision making, we need to bring it out of QGIS and format the
map for printing. This process occurs in the map layout. The map layout also contains information that
helps users orient themselves to the location and context of the information being presented.
Legend: defines the symbols or colors used on a map. The legend should clearly define what
each marker, line type, color or pattern represents
Title: easily identifiable descriptive text that indicates location and purpose of the map. The title
is the largest text on the layout, but does not dominate the map itself.
Border: a thick line drawn around the map that identifies exactly where the mapped area stops.
The border should be thick and the map should be centered within the border.
Scale: a graphic bar that indicates the relative size of the map – may not be necessary for all
audiences.
Orientation: graphic indication of which way is north. This is commonly done through a north
arrow.
1. To open the Print Composer, click “New Print Composer” in the Project dropdown menu (Figure
43).
Version 2.0 | 32
Figure 43
2. In the Composer title window, type “HIV prevalence Tanzania” (Figure 44). This title will help you
stay organized when creating multiple map layouts.
Figure 44
3. Click OK. The Print Composer window will now appear (Figure 45).
Version 2.0 | 33
Figure 45
Figure 46
2. In the Composition panel, click and drag the cursor to specify where you want to position your
map (Figure 47). Note that QGIS will draw whatever is currently being displayed in the map design
window. If your map is not displayed correctly, close Print Composer and re-adjust the Zoom of your
map in the map display window. Leave enough space for a title and a legend.
Version 2.0 | 34
Figure 47
2. Click a point on the map where you would like to place your title. A small box with the letters
“QGIS” will appear where you clicked.
3. Click and drag your cursor to one edge of the box to increase its size (Figure 48).
Figure 48
5. Under Main properties, enter “HIV Prevalence among 15-49 year old by region in Tanzania, THMIS
2010” into the text box (Figure 49). This will be the title of our map.
Version 2.0 | 35
Figure 49
7. In Select Font window, change Size to 22 and Font to Bold (Figure 50).
Figure 50
8. Click OK.
9. Back in the Item properties tab, under Alignment select Center (Figure 51).
Figure 51
Version 2.0 | 36
2. Click the Tanzania map.
3. In the Item properties tab, scroll down and click the Frame checkbox.
2. Click a desired position for the legend on your map (Figure 52).
Figure 52
3. Under Legend items in the Item Properties tab, click the item “TZ_THMIS_15_49ALL” (Figure 53).
Figure 53
4. Click the delete item button to remove the text from the legend.
Version 2.0 | 37
5. Select “Tanzania_adm1” and click the Rename button .
6. In the Legend item properties window, type “HIV Prevalence” in the item textbox.
7. Click OK.
8. Scroll down the Item properties tab until Fonts appears; expand the menu if not already done
(Figure 54).
Figure 54
9. Increase the size of the legend by clicking on the respective Fonts buttons. In our legend, Title font
refers to the text “Legend”, Subgroup font refers to “HIV Prevalence” and Item font refers to
everything else.
2. Click on the desired location in the map layout to place the scale bar (Figure 55). For this example,
place the scale bar within the bounds of the map border.
Figure 55
4. In the map layout, click and drag the cursor directly up to represent North (Figure 56).
Version 2.0 | 38
Figure 56
5. Use the Select/Move item tool in the Composer toolbar to reposition the new items as
necessary.
Figure 57
3. In the next window, select the desired image type (Figure 58).
4. Browse to the desired folder location and type the image name [HIVPrevalenceTanzania].
Version 2.0 | 39
Figure 58
5. Click Save.
1. Click the Project drop-down menu and select Save as (Figure 59).
Figure 59
2. In the next window, browse to the desired location and enter the file name [TanzaniaHIV]. Ensure
the file-extension is .qgis.
3. Click save.
4. Close QGIS.
Version 2.0 | 40
5. Open QGIS.
7. Browse and select the Project file (as saved in Section 1.5.9) [TanzaniaHTC].
8. Click Open.
5. Select WGS 84 as the CRS and click OK. This will add the base shapefile of Africa as a layer (Figure
60). Notice that because the layer was added on top of all our other layers, our previous map is now
obscured.
Version 2.0 | 41
Figure 60
6. In the layer list, click and drag the Africa layer to the bottom of the list. Our previous map of
Tanzania will now be visible on top of our base layer.
7. Label the Africa layer using the column “NAME” (see Section 1.3.2). Our previous Tanzania map
should now be nestled by Kenya, Zambia, Malawi and Mozambique (Figure 61).
Figure 61
Version 2.0 | 42
1.6.2. Adding Secondary labels
In Section 1.3.1 we explored creating gradient maps by dividing the range of HIV prevalence values into
equal intervals. Users may also want to see the prevalence values displayed in the respective provinces
as a second set of labels. As described in section 1.3.2, QGIS can add one label per layer, so we will need
to add a second, duplicate layer to display secondary labels. For this example, we will duplicate our
Regional Tanzania layer to display the HIV prevalence values for each region.
1. Right-click on the Tanazania regional layer [Tanzania_adm1] and select Duplicate. QGIS will add a
new layer to the contents panel [Tanzania_adm1 copy] (Figure 62). This layer is exactly like the one
above it.
Figure 62
4. Enable the layer by clicking the checkbox next to the name. Since duplicate layers are displayed,
each region contains two identical name labels (Figure 63).
Figure 63
Version 2.0 | 43
5. Click the checkmark box next to TZA_roads and Facilities_Tanzania to disable the roads and
facility layer. This will make the next addition easier to see.
7. In the Labels tab, click the ‘Label this layer with’ dropdown menu and select
“TZ_THMIS_15_49ALL_HIVPrev”. Since there are two labels for each region, we need to change the
placement so they do not overlap.
9. Under Placement, select Offset from centroid (Figure 64). This will allow us to specify where the
label should be placed in relation to the center of the polygon.
10. For Quadrant, click the bottom-right button (Figure 64). This will set our labels.
Figure 64
Since we only want to show labels, we need to make the new layer transparent so that it does not
obscure other shapefiles. Just as we stylized our first Tanzania regional layer in Section 1.3.1, we will
remove the gradient in the Style Tab of the Properties window.
11. Without closing the Properties window, open the Style tab.
12. In the drop down menu, select Single Symbol (Figure 65).
14. Click the dropdown menu for Fill style and select “No Brush”.
Version 2.0 | 44
Figure 65
15. Click OK to close the Properties window. Notice that each region now has a name and an associated
HIV prevalence value (Figure 66).
Figure 66
1. In the Layers panel, right click on the file you wish to edit [Tanzania_adm1]
Version 2.0 | 45
2. Select Open Attribute Table (Figure 67)
Figure 67
4. In the Attribute Table, select Toggle Editing to allow editing of the table
5. Double click within a cell in your Attribute Table and enter the desired value or text
6. Select save changes to confirm changes (Figure 68). Note that changes made to a shapefile are
permanent and cannot be undone. As a precaution, you may wish to make changes in a duplicate
layer so you can revert back to the original layer in the case of errors.
Figure 68
Version 2.0 | 46
Figure 69
1. In the Layers panel, verify the checkbox for the facility layer is enabled [Facilities_Tanzania].
Figure 70
Version 2.0 | 47
5. Click the green plus button . This will open the Rule Properties window.
6. In the Rule Properties window, type “PMTCT” for Label (Figure 71).
Figure 71
7. Click the ellipse button (…) for Filter. This will open the Expression string builder window (Figure
72).
Version 2.0 | 48
Figure 72
In this window, we will create a simple equation – also called an Expression – that will instruct QGIS to
display only those sites that are PMTCT.
8. In the Expression sting builder, expand the list for Field and Values by clicking the plus box (Figure
73).
9. Double click “PMTCT”. This will add the column PMTCT to our Expression.
Figure 73
11. Under Field values, click “all unique” for Load values (Figure 74). This will add three values to our
Field values box.
Version 2.0 | 49
12. Double-click “PMTCT”. Our expression will now appear as “PMTCT” = ‘PMTCT’, where the double
quotations indicate variable and single quotations indicate value.
Figure 74
13. Click OK. This will close the Expression string builder.
14. Click OK in the Rule properties window to return to the Properties window. Note that our new
expression has been added as a rule (Figure 75).
Version 2.0 | 50
Figure 75
15. Click the row indicated as “(no filter)” to highlight the default rule.
17. Click OK. Note that only PMTCT sites are now displayed in our map (Figure 76).
Figure 76
Version 2.0 | 51