QGIS Population Density Tutorial

In this brief tutorial we will use the query builder to display features with a specific attribute. We will also link a csv file with population figures to an existing layer. Lastly we will use the population data to determine the layers population density.

Note: You will need your own data to perform the tasks in this tutorial. Please substitute your layer and field names for the ones used here. Should you like a copy of the project files used in this example please contact me.  This tutorial was created using QGIS 2.6 Brighton.


The first task will be to use “Query Builder” to define features with a specific attribute. In this example we will use it to display the “Western Cape”.

Figure 1.

  • Right click your layer in the Table of Contents, TOC
  • Select “Properties”
  • Select the “General Tab”
  • Scroll down to reveal the “Query Builder”
  • Click “Query Builder”


This will open the “Query Builder” window. Here we will enter our expression that will either include or exclude the features based on their attributes.

  • Double click “PROVINCE”to add it to the expression window
  • Click the “=” sign
  • Click the “All” button under “Values” to show all the “PROVINCIAL” field attributes
  • Double click “Western Cape”
  • Click “OK” and “OK”

Figure 2.

In this example the layer “Provinces” will now only display the features with the values “Western Cape” for the PROVINCES field.

Figure 3.

In the same way we can use the “Query Builder” to display “Local Municipalities” of the Western Cape.

Figure 4.


We will now perform a table join between the “SubPlace” layer and a csv file.

Add a csv file to the project

Our first step is to add the csv file to the project using the “Add vector layer” button.

  • Click the “Add vector layer” button
  • Select the file type csv or “comma separated value”
  • Select the .csv file
  • Click “Open”


A new csv table will be added to the TOC. In this example I have added a table called “Gender” which is a results table saved as a csv from the “STATSSA Census 2011” query tool.

Figure 5.

Figure 5 shows the “Gender” table which has the four columns or fields CODE, Male, Female and Total. The “CODE” is a unique ID that will be used to link the table records to the relevant polygon features with the same ID.

Performing the Join

In QGIS table joins are performed from the layer properties window.

  • Right click the layer
  • Select properties
  • Select the “Joins” tab
  • Click the “Green” plus sign
  • Set the join layer as “Gender”
  • Set the Join field as “CODE” (or your unique ID)
  • Set the target field as “SP_CODE_st”
  • Select the Option to “Cache join layer in vitual memory”
  • Click “OK” and “OK”


If you open the attributes table of your layer you should see a few extra fields which are the virtual link to the “Gender” table. The field named “Gender_Total” is the total population.


In order to calculate population density we will need to calculate the area for each polygon feature and then divide the number of people of that feature by this area.

Calculate the Area Using Geometry Tools

  • From the “Vector” menu
  • Select “Geometry tools”
  • Select “Export/Add geometry columns”

Figure 6.

  • Set the “Input vector layer” as “SubPlace”
  • Set the “Project CRS” as the CRS to use in the calculation


In this example I can’t use my layer CRS as it is a Geographic coordinate system and the area would be calculated in decimal degrees. For that reason I have set the Project CRS to the projected CRS “Transverse Mercator using the 19th meridian with the WGS84 ellipsoid and datum. Area and perimeter calculations will now be in meters.

  • Click “OK”


Calculate the Population Density

Population density is a measure of the number of people living per area unit. The formula being number of people living in the land parcel divided by the area of said land parcel.

  • Right click the “SubPlace” layer in the TOC’
  • Select “Open attribute table”
  • Toggle editing mode by click the button with a pencil
  • Click the “Create a new column” button
  • Name the new column “POPDENS”
  • For “Type” select “Decimal number”
  • Set the width to 6
  • Set the precision to 1
  • Click “OK”


A new column should now appear in the attribute table. With the attribute table still open:

  • Open the field calculator (Cntrl + i)
  • Check the “Update existing field” option
  • Select the “POPDENS” field from the field dropdown options
  • Maximise the options for “Fields and Values” under the “Function list”
  • Double click “Gender_Total” to add it to the “Expression” window
  • Click the “/” divide sign
  • Click the “(“ open bracket sign
  • Double click “AREA”


If we wanted to calculate the number of people living per meter squared we could close the brackets and click “OK”. This is however an unrealistic unit calculation for population density. We can modify the calculation slightly by further diving the AREA by a number to convert it to a more suitable measurement unit for area. In this case we will calculate population density per hectare so we will need to divide the area by 10,000.

  • Click the “/” divide sign again
  • Type in 10000
  • Click the “)” close bracket sign
  • Click “OK”
  • Click the “Toggle the editing” icon to stop editing
  • Save your edits

Figure 7.

Figure 8.


Now that we have calculated the population density we can remove the table join.

  • Right click the “SubPlace” layer
  • Select “Properties”
  • Select the “Join” tab
  • Click on the join listed
  • Click the red minus sign “-”
  • Click “OK”


If you view the layer attribute table again you will see that the fields from the “Gender” csv table have been dropped.


We will use the graduated color option under “Styles” to color up areas with different population densities.

  • Right click the layer “SubPlace”
  • Select “Properties”
  • Select the “Styles” tab
  • From the “Symbol” drop down list select “Graduated”
  • Select the new field “POPDENS”
  • Click the “Change..” symbol option
  • Click “Simple fill” change the border style to “No Pen”
  • Click “OK”
  • Select an appropriate “Color ramp”
  • Set the number of classes to 10
  • Set the “Mode” to “Quantile (Equal Count)”
  • Click “OK”

Figure 9.

Figure 10.

When symbolized this way the map gives the viewer an immediate idea of the population density and distribution for the City of Cape Town. I hope this QGIS Population Density tutorial has been beneficial please contact me via the contact form should you have any questions.