|How to Join CSV Attribute Data to a Vector Layer using Fields in QGIS|
This beginner QGIS tutorial will teach users how to join .csv attribute data to a vector point file using fields. Users will also learn how to manage attribute data by renaming or removing columns using the Table Manager. This tutorial also explains how to add in satellite imagery as a thematic base layer using the OpenLayers plugin.
This tutorial requires QGIS 1.7.3
This tutorial is using data gathered from a handheld GPS unit. The data was gathered by a snowboarder at Whistler Blackcomb, located in
The files in the sample pack include:
The following is a description of the attribute data columns
The data_attributes file contains a column with position information, however in this tutorial we will not be using spatial information to complete the table join. This tutorial will teach users how to use field values to properly join .csv attribute data to an existing vector point files.
Using Field Values to Join Attribute Data
Before we open up any files, you must create an additional file which helps QGIS read in different data types. By default all columns that come into QGIS through OGR are considered to be a String data type. In order to work around this you must create a .csvt file which describes the column data types in order. Thankfully “underdark” has created an article explaining ‘How to Specify Data Types of CSV Columns for Use in QGIS’. Following these instructions, open a text editor program and create the .csvt file with the Data Type information provided above. Makes sure to save the file as data_attributes.csvt and place it in the same directory as the data_attributes.csv file.
Open up QGIS 1.7.3
Before we can perform the table join, it is best to take a look at the vector layer and .csv attribute tables to identify fields with common values. Right click the track_points layer and select Open attribute table. Do the same with the data_attributes. Now that both tables are open, you will notice that both contain the exact same number of features (1731). The description above states that the index column is the identifier column used in the join. Taking a look at the track_points attribute table the index column values correlate with the track_se_1 column. Therefore we will be performing a 1-to-1 join between the data_attributes (index column) and the track_points layer (track_se_1 column). Close both attribute tables.
Right click on the track_points layer and select Properties. Along the top, select the Joins tab then click on the green + icon. Use the following settings in the popup window:
You can leave the Cache join layer in virtual memory checked
Click OK, then Apply and OK to close the Properties window.
The table join has just been created in memory between the data_attributes and the track_points layer. To see the effects of the join, again right click on the track_points layer and select Open attribute table. Scroll to the far right and you should see the additional columns are now linked. To make these changes permanent we will export this joined data to a new shapefile. Close the attribute table. Right click the track_points layer and select Save as. Click the browse button and navigate to the location of your sample data, save the file as data_points.shp and click OK. At this point we no longer require the data_attributes or track_points, so remove them from QGIS by right clicking and selecting Remove.
Renaming and Removing Columns Using the Table Manager
Click Add vector layer and select the data_points.shp file. Right click the data_points layer and select Open attribute table. As you can see all the columns are now permanently stored with the vector layer. You can also see that there are a lot of additional columns which we can remove to clean things up.
Select Plugins > Fetch Python Plugins. In the filter type ‘table’ and install the Table Manager plugin. Once installed change the filter to ‘openlayers’ and install the OpenLayers Plugin, once that is installed you can close the popup. In the Table of Contents make sure that data_points is selected, then select Plugins > Table > Table Manager.
Select the ‘track_se_1’ column and click the Rename button. Rename the column to ‘track_ptid’ meaning the track’s point identifier. Rename the ‘ele’ column to ‘elev’ for elevation.
To remove multiple columns at once you can hold down the Ctrl key on your keyboard while selecting individual columns with the mouse pointer. Hold down the Ctrl key and select all of the following columns:
Once all these columns are selected click the Delete button. It should popup with a list of all the columns that are about to be deleted, if it matches the list provided click Yes to remove all the unnecessary fields. You should now have 11 columns listed in the Table Manager window. Click Save, when asked to keep the layer style click No, then click Close.
Creating a Thematic Map
Now that all the data_points information has been cleaned, we are ready to create a thematic map based on the speed the snowboarder was traveling. Right click on the data_points layer and select Properties. Along the top select the Style tab, in the top left side of the menu select a Graduated symbology from the drop-down list.
Set the Column to leg_speed, set the Classes to 7 and Mode to Pretty Breaks, then click the Classify button. This should produce some nice classes of 10km/h speed ranges.
Creating a Custom Color Ramp
Next, we will create a new color ramp by selecting New color ramp… from the Color Ramp drop down. Select to use a Gradient color ramp type and click OK. Set the Color 1 to dark blue and Color 2 to be red, then place a checkbox in Multiple stops. Click the Add stop button, select yellow and click OK, then enter 50 as the % value and click OK. Click Add stop again, select lime green and click OK, then enter 25 as the %. Add another stop using orange at 75%. When your finished click OK and save the color ramp as ‘bgyor’ meaning 'blue-green-yellow-orange-red’. This will be helpful for classifying slow areas as dark blue and fast areas as bright red. Select bgyor as the color ramp and click the Classify button again, then click Apply and OK.
Adding a Satellite Imagery Thematic Base Layer using the OpenLayers Plugin
The OpenLayers plugin is a great tool for presenting your vector data layers on top of Google, Bing, Yahoo and OpenStreetMap thematic layers. To add the layer select Plugins > OpenLayers plugin > Add Google Satellite Layer. This will cause your data to reproject into the Google maps projection which can be seen in the bottom right corner of QGIS, EPSG: 900913. In the Table of Contents drag the Google Satellite layer to the bottom so that your vector layers will appear on top. You can now pan and zoom around and explore the path of the snowboarder.
That completes the tutorial on joining .csv attribute data to existing vector point files for the purposes of thematic mapping. By completing this tutorial users have learned how to: properly import .csv using different data types by using the additional .csvt file, perform a join using field values, rename and remove columns using the Table Manager, create a custom color ramp and add a satellite image base layer using the OpenLayers plugin.