Skip to content
Learn QGIS with BNHR

Module 3 · Intermediate

Working with attributes

Joining, calculating, and summarizing barangay population data in QGIS to prepare barangay- and district-level layers for analysis.

Module 3Level Intermediate
On this page
  1. Overview
  2. Before we begin
  3. What we want to achieve
  4. What we have
  5. Relevant QGIS knowledge/skills
  6. Exploring the data
  7. 1.1. Opening the attribute table
  8. 1.2. Selecting or filtering the data
  9. Modifying the data
  10. 2.1. Adding population data from 2000 to 2015 using attribute joins
  11. 2.2. Fixing a mistake in the Attributes
  12. 2.3. Adding a district field to the barangay boundary data
  13. 2.4. Creating a district-level admin boundary data with correct population values
  14. Certification and support

For exercises in this module, you will use the bnhr_qgis-architects-planners-urban-001.gpkg inside the data subfolder in the BNHR_QGIS-for-Architects-Planners-and-Urban-Practitioners folder.

This geopackage contains the barangay boundary layer. The population CSV (pop_brgy_quezon_city.csv) and the list of barangays per district (quezon-city-barangay-per-district.rtf) are in the same data subfolder.

  1. Create a barangay-level layer with population data from 2000 to 2020.
  2. Create a district-level layer with population data from 2000 to 2020.
  3. Prepare both layers for mapping population density and the annual rate of population growth from 2000 to 2020 in Styling and symbology.
  1. bnhr_qgis-architects-planners-urban-001.gpkg that contains:

    • adm_brgy_quezon_city - polygon layer containing the 142 barangays in Quezon City with the following attributes:
      • barangay: name of the barangay
      • municity: name of the city or municipality
      • province: name of the province
      • region: name of the region
      • psgc_barangay: Philippine Standard Geographic Code (PSGC) of the barangay
      • psgc_corr_barangay: PSGC Correspondence Code of the barangay,
      • municity_income_class: income class of the city or municipality
      • urban_rural: urban or rural classification of the barangay
      • pop2020: population of the barangay in 2020
  2. pop_brgy_quezon_city.csv - CSV file containing population data from 2000 to 2015 per barangay in Quezon City

  3. quezon-city-barangay-per-district.rtf - text document listing down the barangays per district in Quezon City

First, let’s explore the data to see what we’re working with.

  1. Load the adm_brgy_quezon_city vector layer and pop_brgy_quezon_city.csv in QGIS.

For the CSV layer, make sure that the psgc and psgc_corr fields are loaded as strings and the pop2000, pop2007, pop2010, and pop2015 fields are loaded as integers.

You can ensure this by loading the CSV as a Delimited Text Layer or by using a .csvt file

Loading the pop_brgy_quezon_city CSV
Loading pop_brgy_quezon_city.csv
Quezon City barangay admin boundary and barangay population CSV loaded in QGIS
The adm_brgy_quezon_city and pop_brgy_quezon_city layers loaded in QGIS

Level: Beginner

Let’s look at the attribute table of both layers.

  1. You can open the attribute table of a layer by:
  • Right-click layer ‣ Open Attribute Table,
  • Open Attribute Table in the Attributes Toolbar, or
  • Clicking F6 while a layer is active/selected
Attribute table of adm_brgy_quezon_city
Attribute table of adm_brgy_quezon_city
Attribute table of pop_brgy_quezon_city
Attribute table of pop_brgy_quezon_city

What do you notice? What fields/attributes are similar?

Level: Beginner

Let’s try to select and filter features based on their attributes.

To apply a selection, you can use the options on the attribute table or the Selection Toolbar.

Selection options in the attribute table
Selection options in the attribute table
The Selection Toolbar
The Selection Toolbar
  1. Select the barangays with population in 2020 greater than 10000 using a Select Features by Value. You can open the Select Features by Value dialog by clicking the appropriate button (Select Features by Value button) on the attribute table of the layer. Alternatively, you can go to Select Features by Value in the Selection Toolbar.

  2. On the Select Features by Value dialog, put 10000 and Greater than (>) in the pop2020 row.

Selecting the barangays with population in 2020 > 10,000
Selecting the barangays with population in 2020 > 10,000
  1. Your selection (65 barangays) should reflect in both the attribute table and the map canvas (i.e. the rows in the attribute table and the features in the map canvas will both be highlighted).
Selected barangays with population in 2020 > 10,000 in the map canvas
Selected barangays with population in 2020 > 10,000 in the map canvas
Selected barangays with population in 2020 > 10,000 in the attribute table
Selected barangays with population in 2020 > 10,000 in the attribute table
  1. Deselect the features by clicking the Deselect all (CTRL + SHIFT + A) button (Deselect All Features button) in the attribute table or in the Selection Toolbar.

  2. Now try to use a Select Features by Expression to select the barangays with population in 2020 greater than 50,000. Open the Select Features by Expression dialog by clicking the appropriate button (Select Features by Expression button) on the attribute table of the layer. Alternatively, you can go to Select Features by Expression in the Selection Toolbar. The Select by Expression dialog will appear. Input the expression below and click Select Features.

pop2020 > 50000
Selecting the barangays with population in 2020 > 50,000
Selecting the barangays with population in 2020 > 50,000
  1. Your selection (20 barangays) should appear in the map canvas and attribute table.

  2. Deselect the features.

Selected barangays with population in 2020 > 50,000 in the map canvas
Selected barangays with population in 2020 > 50,000 in the map canvas
Selected barangays with population in 2020 > 50,000 in the attribute table
Selected barangays with population in 2020 > 50,000 in the attribute table

Now let’s try a filter to show only the barangays with population in 2020 > 100,000.

  1. Open the query builder by right clicking on the layer and clicking Filter. Apply the filter below then click OK. You can click Test to check if your filter before applying it to the layer.
pop2020 > 100000
Applying a filter
Applying a filter
  1. You will notice that a layer has a filter applied because it will have a filter icon next to its name in the Layers Panel.
Layer with a filter
Layer with a filter
  1. Notice also that only the features that meet the specified criteria are visible in the map canvas and the attribute table of the layer.
Filter applied to the layer in the map canvas
Filter applied to the layer in the map canvas
Filter applied to the layer in the attribute table
Filter applied to the layer in the attribute table
  1. To undo a filter, remove the query or click Clear on the Query Builder then click OK.

What is the main difference between a select and a filter? What effects does this difference have?

In the following exercises, we will modify our data in order to create a barangay-level layer with population data from 2000 to 2020 and a district-level layer with population data from 2000 to 2020. You will style both layers in Styling and symbology.

2.1. Adding population data from 2000 to 2015 using attribute joins

Section titled “2.1. Adding population data from 2000 to 2015 using attribute joins”

Level: Beginner

First, let’s learn about attribute joins.

Attribute joins are used in QGIS to combine the attributes of two or more data sources based on a common field. This is useful when you have data that is related to each other but stored in separate files. It is also one of the most important functions of a GIS as it allows for non-spatial data to be made spatial.

In your case, you have a vector layer of barangay boundaries (adm_brgy_quezon_city) and a CSV file of population data (pop_brgy_quezon_city.csv) and you want to combine them to create a map of population density or population rate over time. You can do this by joining the attribute table of the CSV file with that of the vector layer.

There are two ways to perform attribute joins in QGIS. The first is via the Layer Properties ‣ Joins and the second is via the Join attributes by field value processing algorithm. The first one can be considered a soft join since it temporarily adds fields from the join table to the target table which can be removed/unjoined any time. The second option can be considered a hard join since it creates a new table from the joined attribute tables.

In order to successfully perform a join, the two attribute tables to be joined must have a common field present in both tables/layers.

The names of the common field between two layers to be joined need not be the same. What’s more important is that the values they contain correspond to one another. Think of a join similar to the matching-type activities we did in school.

There are other join processing algorithms such as Join attributes by nearest, Join attributes by location, and Join attributes by location (summary) but these perform joins using spatial relationships instead of comparing attributes. These join algorithms are discussed further in Spatial relationships and queries.

Now that we know about attribute joins, let’s apply it to our data.

  1. Load the adm_brgy_quezon_city vector layer and pop_brgy_quezon_city.csv in QGIS (if you haven’t yet).

For the CSV layer, make sure that the psgc and psgc_corr fields are loaded as strings and the pop2000, pop2007, pop2010, and pop2015 fields are loaded as integers.

You can ensure this by loading the CSV as a Delimited Text Layer or by using a .csvt file

  1. You can Right-click ‣ Properties ‣ Fields on a layer to double-check if its attributes/fields are of the correct type.
Fields of the adm_brgy_quezon_city layer
Fields of the pop_brgy_quezon_city layer
  1. Open the attribute table of the two layers again. What is/are common field/fields between the two? What can we use to join their attribute tables?
Attribute table of adm_brgy_quezon_city
Attribute table of adm_brgy_quezon_city
Attribute table of pop_brgy_quezon_city
Attribute table of pop_brgy_quezon_city
  1. Apply a soft join by going to the Layer Properties ‣ Joins tab of the adm_brgy_quezon_city layer. Click the green plus button (Add layer button) to add and join the pop_brgy_quezon_city layer.
Joins tab in the Layer Properties of adm_brgy_quezon_city
Joins tab in the Layer Properties of adm_brgy_quezon_city
Parameters for joining the pop_brgy_quezon_city layer
Parameters for joining the pop_brgy_quezon_city layer
Parameters for joining the pop_brgy_quezon_city layer
Joins tab of adm_brgy_quezon_city with the pop_brgy_quezon_city joined

Notice that the names of the Join field and Target field are not the same. This doesn’t matter because their values correspond to one another.

Under Joined fields, we unchecked barangay, psgc, and psgc_corr because the barangay names and codes already exist in the adm_brgy_quezon_city layer.

We left the Custom field name prefix blank. However, there are instances when adding a custom prefix would be useful—especially to avoid naming conflicts or duplication of field names in your layer. For example, if both the base layer and the join layer have a field named population, you may want to use a custom field name prefix so that you can identify which field came from the join layer and, more importantly, avoid conflicts and issues with your attribute table having two fields of the same name.

  1. Click OK on the Add Vector Join dialog and OK again on the Joins tab.

  2. Open the attribute table of the adm_brgy_quezon_city layer.

adm_brgy_quezon_city with the pop2015, pop2010, pop2007, and pop2000 fields joined from the pop_brgy_quezon_city layer
adm_brgy_quezon_city with the pop2015, pop2010, pop2007, and pop2000 fields joined from the pop_brgy_quezon_city layer

You should now be able to use the pop2015, pop2010, pop2007, and pop2000 fields as though they are part of the adm_brgy_quezon_city layer.

Because we did a soft join, we can easily remove the joined fields or edit what fields are joined from the Layer Properties of adm_brgy_quezon_city.

  1. Perform a hard join by using the Join attributes by Field Value algorithm with the parameters below.
Parameters for Join attributes by field value
Parameters for Join attributes by field value
Layer 2 (pop_brgy_quezon_city) fields to copy parameters
Layer 2 (pop_brgy_quezon_city) fields to copy parameters
  1. The output (Joined layer) should be loaded in the Layers panel. Open its attribute table and check if the population fields are there.
Output of Join attributes by field value
Output of Join attributes by field value
  1. Save the output (Joined layer) and name it something like adm_brgy_quezon_city_pop2000_2020.

Level: Beginner

If you check the attribute table for adm_brgy_quezon_city_pop2000_2020, you will notice that there is a mistake in the values. In particular, the province field has Quezon City in it. In practice, while the National Capital Region has no provinces, we commonly use the NCR districts in place of a province. In this case, let’s replace the values in the province field with the correct value: 'NCR District (2nd District)'

You can edit the attribute values of a layer’s features directly in the attribute table. To do this, you must first enable editing mode on the layer:

  • On the layer panel: right click on the layer ‣ Toggle Editing
  • On the attribute table: Toggle editing mode (CTRL+E)

Afterwards, you can edit the attributes by:

  • Manually editing the values in the individual cells
  • Updating an entire row
  • Using Expressions

You can also add and remove fields while editing mode is enabled.

To add a new field, click on the New Field (CTRL + W) button (New field button) on the attribute table toolbar and provide the appropriate parameters.

To remove a field, click on the Delete Field (CTRL + L) button (Delete field button) on the attribute table toolbar and select the field you want to delete.

  1. Open the Attribute table to apply edits to the province field.

  2. Using the Attribute table:

    • Toggle Editing ON
    • Select province
    • In the Expression field:
    'NCR District (2nd District)'
    • Click Update All or Update Selected
Editing the province field using the Attribute table
Editing the province field using the Attribute table
  1. Don’t forget to save your edits!

  2. Open the attribute table of adm_brgy_quezon_city_pop2000_2020 to see the results.

province field with updated value
province field with updated value

What should I do if I want to add a psgc_province or psgc_region field?

  • Check Update existing field
  • Select province
  • In the Expression tab:
'NCR District (2nd District)'
  • Click OK
Editing the province field using the Field Calculator
Editing the province field using the Field Calculator

2.3. Adding a district field to the barangay boundary data

Section titled “2.3. Adding a district field to the barangay boundary data”

Level: Intermediate

Let’s learn more about the Field Calculator.

QGIS has a powerful Field Calculator that allows you to perform calculations on fields in a layer. You can use it to create new fields, update existing fields, or calculate values based on existing fields. The kinds of operations you can run in the field calculator range from basic mathematical operations such as addition, subtraction, multiplication, and division; boolean and conditional operations such as ifs and comparisons; as well as more complex calculations using the built-in functions and operators of the QGIS expression engine.

The Field calculator can be accessed by clicking the Open Field Calculator button (Open Field Calculator button) in the Attributes Toolbar or in the attribute table itself. This will open the Field Calculator dialog where you can input the parameters of the field you want to calculate, the expression or formula you want to use, and even be able to preview the results. You also have the option to use the Field Calculator to update an existing field.

When we perform calculations using the Field Calculator, it’s important to make sure that the data types of the fields we are working with match the requirements of the calculation. For example, we cannot perform arithmetic operations on text fields or concatenate numeric fields with text fields.

You can refer to the QGIS User Manual for more information about how to use expressions: https://docs.qgis.org/latest/en/docs/user_manual/expressions/expression.html

The list of functions that are available in QGIS are also available at: https://docs.qgis.org/latest/en/docs/user_manual/expressions/functions_list.html#functions-list

Take advantage of the Search function and information bar when creating your formulas or expressions.

The Field Calculator dialog
The Field Calculator dialog

For this exercise, you want to add a district field on the adm_brgy_quezon_city_pop2000_2020 layer that pertains to the district that the barangay is part of.

The list of Quezon City barangays per district can be found in the table below or in the quezon-city-barangay-per-district.rtf file.

Quezon City Barangays by District

District Barangays
1st District Alicia, Bagong Pag-asa, Bahay Toro, Balingasa, Bungad, Damar, Damayan, Del Monte, Katipunan, Mariblo, Masambong, N.S. Amoranto, Nayong Kanluran, Paang Bundok, Pag-ibig Sa Nayon, Paltok, Paraiso, Phil-Am, Project 6, Ramon Magsaysay, Saint Peter, Salvacion, San Antonio, San Isidro Labrador, San Jose, Santa Cruz, Santa Teresita, Santo Cristo, Santo Domingo, Sienna, Talayan, Vasra, Veterans Village, West Triangle, Lourdes, Maharlika, Manresa
2nd District Bagong Silangan, Batasan Hills, Commonwealth, Holy Spirit, Payatas
3rd District Amihan, Bagumbuhay, Bagumbayan, Bayanihan, Blue Ridge A, Blue Ridge B, Camp Aguinaldo, Claro, Dioquino Zobel, Duyan-duyan, E. Rodriguez, East Kamias, Escopa I, Escopa II, Escopa III, Escopa IV, Libis, Loyola Heights, Mangga, Marilag, Masagana, Matandang Balara, Milagrosa, Pansol, Quirino 2-A, Quirino 2-B, Quirino 2-C, Quirino 3-A, Saint Ignatius, San Roque, Silangan, Socorro, Tagumpay, Ugong Norte, Villa Maria Clara, West Kamias, White Plains
4th District Bagong Lipunan Ng Crame, Botocan, Central, Kristong Hari, Damayang Lagi, Aurora, Doña Imelda, Doña Josefa, Don Manuel, Horseshoe, Immaculate Concepcion, Kalusugan, Kamuning, Kaunlaran, Krus Na Ligas, Laging Handa, Malaya, Mariana, Obrero, Old Capitol Site, Paligsahan, Pinyahan, Pinagkaisahan, Roxas, Sacred Heart, San Isidro, San Martin De Porres, San Vicente, Santo Niño, Santol, Sikatuna Village, South Triangle, Tatalon, Teachers Village East, Teachers Village West, U.P. Campus, U.P. Village, Valencia
5th District Bagbag, Capri, Fairview, Greater Lagro, Gulod, Kaligayahan, Nagkaisang Nayon, North Fairview, Novaliches Proper, Pasong Putik Proper, San Agustin, San Bartolome, Santa Lucia, Santa Monica
6th District Apolonio Samson, Baesa, Balong Bato, Culiat, New Era, Pasong Tamo, Sangandaan, Sauyo, Talipapa, Tandang Sora, Unang Sigaw

Using the Field Calculator, we’re going to:

  1. create a new field called district
  2. set the value of the district field to the district that the barangay is a part of
  1. Open the Field Calculator for the adm_brgy_quezon_city_pop2000_2020 layer that you created in the last exercise by clicking the Open field calculator (CTRL + I) button (Open Field Calculator button) .

  2. You can also open its attribute table to access the Field Calculator and organize the columns to make it less cluttered.

As discussed in QGIS Expressions, QGIS expressions follow a very SQL like syntax.

  • field/attribute = inside “double quotes” or no quotes if there is no whitespace
  • strings = inside ‘single quotes’
  • numbers = no quotes
  1. On the Field Calculator, add the following parameters:

    • Check Create new field
    • Output field name: district
    • Output field type: Text (string)
    • Output field length: 20
    • On the Expression tab:
    CASE
    WHEN "barangay" IN ('Alicia', 'Bagong Pag-asa', 'Bahay Toro', 'Balingasa', 'Bungad', 'Damar', 'Damayan', 'Del Monte', 'Katipunan', 'Mariblo', 'Masambong', 'N.S. Amoranto', 'Nayong Kanluran', 'Paang Bundok', 'Pag-ibig Sa Nayon', 'Paltok', 'Paraiso', 'Phil-Am', 'Project 6', 'Ramon Magsaysay', 'Saint Peter', 'Salvacion', 'San Antonio', 'San Isidro Labrador', 'San Jose', 'Santa Cruz', 'Santa Teresita', 'Santo Cristo', 'Santo Domingo', 'Sienna', 'Talayan', 'Vasra', 'Veterans Village', 'West Triangle', 'Lourdes', 'Maharlika', 'Manresa') THEN '1st District'
    WHEN "barangay" IN ('Bagong Silangan', 'Batasan Hills', 'Commonwealth', 'Holy Spirit', 'Payatas') THEN '2nd District'
    WHEN "barangay" IN ('Amihan', 'Bagumbuhay', 'Bagumbayan', 'Bayanihan', 'Blue Ridge A', 'Blue Ridge B', 'Camp Aguinaldo', 'Claro', 'Dioquino Zobel', 'Duyan-duyan', 'E. Rodriguez', 'East Kamias', 'Escopa I', 'Escopa II', 'Escopa III', 'Escopa IV', 'Libis', 'Loyola Heights', 'Mangga', 'Marilag', 'Masagana', 'Matandang Balara', 'Milagrosa', 'Pansol', 'Quirino 2-A', 'Quirino 2-B', 'Quirino 2-C', 'Quirino 3-A', 'Saint Ignatius', 'San Roque', 'Silangan', 'Socorro', 'Tagumpay', 'Ugong Norte', 'Villa Maria Clara', 'West Kamias', 'White Plains') THEN '3rd District'
    WHEN "barangay" IN ('Bagong Lipunan Ng Crame', 'Botocan', 'Central', 'Kristong Hari', 'Damayang Lagi', 'Aurora', 'Doña Imelda', 'Doña Josefa', 'Don Manuel', 'Horseshoe', 'Immaculate Concepcion', 'Kalusugan', 'Kamuning', 'Kaunlaran', 'Krus Na Ligas', 'Laging Handa', 'Malaya', 'Mariana', 'Obrero', 'Old Capitol Site', 'Paligsahan', 'Pinyahan', 'Pinagkaisahan', 'Roxas', 'Sacred Heart', 'San Isidro', 'San Martin De Porres', 'San Vicente', 'Santo Niño', 'Santol', 'Sikatuna Village', 'South Triangle', 'Tatalon', 'Teachers Village East', 'Teachers Village West', 'U.P. Campus', 'U.P. Village', 'Valencia') THEN '4th District'
    WHEN "barangay" IN ('Bagbag', 'Capri', 'Fairview', 'Greater Lagro', 'Gulod', 'Kaligayahan', 'Nagkaisang Nayon', 'North Fairview', 'Novaliches Proper', 'Pasong Putik Proper', 'San Agustin', 'San Bartolome', 'Santa Lucia', 'Santa Monica') THEN '5th District'
    WHEN "barangay" IN ('Apolonio Samson', 'Baesa', 'Balong Bato', 'Culiat', 'New Era', 'Pasong Tamo', 'Sangandaan', 'Sauyo', 'Talipapa', 'Tandang Sora', 'Unang Sigaw') THEN '6th District'
    END
Creating the district field with the Field Calculator
Creating the district field with the Field Calculator
  1. Click OK.

  2. Open the attribute table of the adm_brgy_quezon_city_pop2000_2020 and check if a new district field is added.

The adm_brgy_quezon_city_pop2000_2020 layer with the district field
The adm_brgy_quezon_city_pop2000_2020 layer with the district field

Don’t forget to save your edits! Otherwise, they will not be reflected in your layer. To do this, you can:

  1. Right-click on the layer being edited on the Layers Panel ‣ Current Edits ‣ Save…
  2. Clicking the Save edits (CTRL + S) button (Save edits button) on the attribute table toolbar.

2.4. Creating a district-level admin boundary data with correct population values

Section titled “2.4. Creating a district-level admin boundary data with correct population values”

Level: Intermediate

First, let’s learn about the Aggregate algorithm.

The Aggregate algorithm can be used to dissolve boundaries, remove overlaps, and merge adjacent polygons while aggregating the attributes associated with each feature. It is commonly used in data preprocessing for spatial analysis as well as for general data management tasks. With the Aggregate algorithm, you can easily summarize data for different levels of analysis such as administrative boundaries or land use types. Additionally, the algorithm can be used in combination with other processing tools, plugins, and scripts to create more advanced data workflows.

In your case, you can use the Aggregate algorithm to create a district-level administrative boundary layer while also calculating their total population by taking the sum of the population of each barangay within a district.

  1. Open the Aggregate algorithm from the Locator Bar or via Processing Toolbox.
  2. Remove other unnecessary fields by selecting them and clicking the Delete Attribute symbol.
Fields to be removed
Fields to be removed
  1. Apply the following parameters.
    • Group by expression: district
    • district, municity, province, region: first_value
    • pop2020, pop2015, pop2010, pop2007, pop2000: sum

You can specify which attributes will be included in the resulting layer as well as the aggregate function to use for each attribute. Here, you used first_value for district, municity, province, and region because the features are grouped per district, so every barangay in a group has the same values for these fields as the first one.

For the population fields, we used sum so that the resulting value will be the sum of the individual values of the features to be aggregated.

Aggregate algorithm parameters
Aggregate algorithm parameters
  1. Click Run.
  2. You should see a layer named Aggregated added to the Layer panel. You can rename or save this layer to adm_dist_quezon_city_pop2000_2020.
Output of the Aggregate algorithm.
Output of the Aggregate algorithm.
Attribute table of the Aggregate algorithm output.
Attribute table of the Aggregate algorithm output.

Compare the results of the Aggregate and the Dissolve processing algorithms.

Saved in this browser only

Certification and support

We offer additional resources, certificates, official QGIS certification, ongoing support, and other perks to those who join our instructor-led training.

Join an upcoming training to take this as an instructor-led course, or contact us to arrange one for your team or organization.

Something new

Identify results
LayerExplore a module
ModuleMap layouts and elements
AboutThis module discusses map layout concepts, elements, and templates.
CourseQGIS: Maps and Layouts, free