In a previous article I have shown how data from a text file with points can be displayed as an additional layer on top of a layer consisting of areas. There was no connection between de data from both layers. This article explains how areas in one layer can be coloured based on data from a text file in another layer. As an example the proximity of department stores in Dutch municipalities in 2010 is used.
Not all layers are equal
First it is necessary to discuss a regularly occurring problem in QGIS. Not all layers are treated equally. Layers can have different sources, like a database, shape file or text file. After opening those layers you don’t see any difference at first. The origin of a layer does matter, however. If you join two layers and want to visualise the joined data, you will be confronted with the way QGIS handles data types. Numerical fields are sometimes seen as strings, which can’t be used to visualize data.
From version 1.7, it is easy to join two layers, by means of the ‘Joins’ tab on the ‘Layer properties’ dialogue window. A layer added with ‘Add delimited text layer’ cannot be joined directly. It is possible to use an additional step, by saving the layer as a shapefile. In this article another method is used.
There is another way to import a text file in QGIS. It’s a bit confusing that you need to use the butten ‘Add vector layer’ for this. The preparation of the data consists of two steps.
The first step is to make a CSV file, containing at least a column to be used to join both layers (in this case a municipality code). Further, you need one or more columns with the data you want to display (in this case the proximity of supermarkets and department stores). Co-ordinates are not required.
The second step is to make a CSVT file, in which the data types in the CSV-file are specified. This is a text file containing just one line. For each column the data type is put between double quotes. Specifying field width and precision is optional. The CSVT file forces QGIS to treat numerical fields really as numerical.
Now the CSV file can be opened in QGIS. Click on the button ‘Add vector layer’. Check if the right encoding is used for your language. If not, special characters will not be displayed properly. Next click on ‘Browse’.
Choose the right filter (‘Comma Separated Value’ or ‘All files’), in order to see your file. You have to select the file with the extension ‘.csv’.
The new layer will appear in the layer list. The symbol indicates it contains only non-spatial data. Now open the shapefile holding your areas (in this case the municipalities of 2010). Double click on the layer, go to the ‘Joins’ tab en click on the plus sign. Select the right layer and pick the fields to be used for the join.
After clicking ‘OK’, the layer will appear on the list. The data types of the joined layer can be checked on ‘Fields’ tab. The values can be examined in the attribute table.
Now the data are joined to the municipality map. The map file itself is unchanged. (If you want to add certain data to your area shapefile permanently, you can save it with a different name.)
Colours can be applied to the map with the usual method. Automatic classification doesn’t always work. In that case you have to add the lower and upper values for each category yourself
There are more ways to present the joined data, but not all options will work. Pie charts will be no problem if you use the ‘Diagrams’ tab, but the ‘Overlay’ tab ignores the joined data. In order to display circles, pie charts or bar charts, joining layers isn’t really necessary. ‘Add delimited text layer’ is a better option for that. In that case your text file must contain co-ordinates. An additional advantage of that is more influence on the position of the symbols.