Author: Ketty, Ben Hur
This module will provide you with an overview of the common steps needed to work with the attribute table and layer attributes in QGIS. At the end of the module you’ll be able to understand the following concepts;
Additionally, you will learn the following skills;
You’ll use the field calculator and qgis expression engine to run mathematical operations and functions in QGIS.
Let’s start with an example:
In some cases, for instance for infrastructure planning purposes, you may want to know the area of the polygons in a layer. If you have one polygon, this would not be a problem. But what if you have many polygons/areas in the layer? To calculate each area one by one is almost impossible. The Bangladesh administrative boundary vector layer has many polygons which means it would be a good dataset for this tutorial. We are going to calculate the area for each polygon using an automated way. In total, the area of 64 districts will be calculated.
Much of the work you do when making a map involves working with layer attributes. Whether you’re drawing the layer, configuring a popup, performing analysis or visualising you need some level of knowledge of the layer’s fields and the values they contain. If it’s your own layer, you know and understand its attributes. But if it isn’t, how do you gain that knowledge? Likely, you inspect the field names and examine field values for some clues. And if you’re lucky, you’ll find some documentation that describes the layer’s attributes.
The attribute table displays information on features of a selected layer. Each row in the table represents a feature (with or without geometry), and each column contains a particular piece of information about the feature. Features in the table can be searched, selected, moved or even edited. Specifically, there are two new field properties that describe the values contained in a field;
Here’s an example; Suppose you have a land parcel layer that has these fields:
If you were to describe these field values, you might write something like this:
In looking at these descriptions, some words stand out, like: identifier, name, location, description, number of, size, value, and date. These are the keywords that describe what the field values are. The field value type keywords are constructed from these types of words. They are:
In the parcel layer example above, the Parcel ID field is an integer field. If the field value type indicates the field values represent a unique identifier rather than a count, QGIS would not suggest drawing this field attribute using graduated colors or symbols.
In addition, other concepts like the field calculator functionality in the attribute table allows one to perform calculations on basis of existing attribute values or defined functions, e.g to calculate length, area or population density. Note that the possible calculations are determined by the attributes or make up of your data. For example, it’s only possible to calculate population density if there’s a field with population counts. Using the expression engine and field calculator, you would then go on to construct an expression or formula to calculate population density. In this case the formula would be; total population as a number of people/the land area covered by that population. You’ll notice that it’s important to know what each of the fields in the layer attribute table represents. This means you need to know and understand your data thoroughly. This makes it easier to apply functions and expressions and therefore come up with meaningful analyses and visualizations/map products.
The attribute table displays information on features of a selected layer. Each row in the table represents a feature (with or without geometry), and each column contains a particular piece of information about the feature. Features in the table can be searched, selected, moved or even edited.
Figure 6.1: Several polygons
Figure 6.2: Open attribute table
If you don’t want the attribute table to be a floating window but rather dock it on the QGIS interface, you can click the Dock attribute table button . When docked, attribute tables will appear as tabs instead of individual windows.
Figure 6.3: Docked attribute table
For area calculations, the Coordinate Reference System should be a projected one. This allows you to calculate distances correctly. Remember, our interest is to automatically calculate the area for each of the 64 districts. Check the Coordinate Reference System of the vector layer. If it’s a geographic coordinate reference system, then reproject the layer to a projected coordinate system. Check for different projections on the EPSG website. This is Bangladesh, we’ll use Gulshan 303 / Bangladesh Transverse Mercator, EPSG:3106. From previous modules where map projections are extensively discussed, you may already know that map projections are applied relative to a given location on earth.
Check the project Settings; Go to: Project ‣ Properties Properties ‣ General.
Figure 6.4: General settings
$area / 1000000
You can find this expression under Geometry. Click OK and it will automatically calculate the area for each polygon. Take note that the area computation is dependent on the coordinate reference system used so you may have different results depending on what CRS you used. You can also search and find information about expressions on the right side of the Field Calculator or Expression Builder.
Figure 6.5: Field calculator dialogue
Figure 6.6: New attribute table with new field and attributes
An attribute table is a database or tabular file containing information about a set of geographic features
Geographic features are usually arranged so that each row represents a feature and each column represents one feature attribute
It is necessary to reproject layers prior to area calculations when the layer has a geographic coordinate reference system
At this point, you might notice that the attribute table stores both spatial and non-spatial data. In this tutorial you’ll discover ways of working with attribute table data. For example, Using expressions, select Clinics in Rajshahi with an internet facility, all using data from the attribute table.
Figure 6.7: Add different layers
"amenity" = 'clinic' AND "internet_access" = 'wlan'
You’ll notice that the expression has a number of predicates like the comparison sign (=), the logical predicate (AND) and a string which is enclosed in single quotes (‘ ‘). There are also two attribute names(amenity, internet_access) and their values (clinic,wlan).
Figure 6.8: Select by Expression builder dialogue
Figure 6.9: Selected clinic/feature is highlighted (yellow)
Figure 6.10: Selected clinic/feature is highlighted (blue)
It’s also possible to make selections by clicking a feature within the map canvas.
Developing a functional expression starts with understanding your data; for example the attributes and the values they hold. Then asking the right questions and finally, developing the correct expression, putting into consideration all mathematical rules that make an error free expression.
The Expression builder dialog offers access to the:
There are many use cases for expressions, here are some examples. Notice how the expressions are developed and what operators or predicates are used. Also important is the fact that all these expressions are developed based on the content of the dataset. You may adapt this to your dataset of choice.
"total_pop" / "area_km2"
CASE WHEN "pop_density" < 50 THEN 'Low population density'
WHEN "pop_density" >= 50 AND "pop_density" < 150 THEN 'Medium population density'
WHEN "pop_density" > 150 THEN 'High population density'
END
"price_m2" > 10000
"density level" = 'High population density' and "price_m2" > 10000