Quantcast
Channel: Quantum GIS
Viewing all articles
Browse latest Browse all 10

How to Join CSV Attribute Data to a Vector Layer using Fields in QGIS

$
0
0

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 WroclawSample data for this tutorial can be downloaded here, save the file to a well known location and extract the files.

 

Overview

 

This tutorial is using data gathered from a handheld GPS unit.  The data was gathered by a snowboarder at Whistler Blackcomb, located in British Columbia, Canada.

 

thematic_map

 

The files in the sample pack include:

  1. track_line.shp, a vector line layer that illustrates the path of the snowboarder
  2. track_points.shp, a vector point layer which makes up the nodes of the track_line layer
  3. data_attributes.csv, a comma separated file containing the attributes of the snowboarder taken at every feature on the track_points layer

 

The following is a description of the attribute data columns

Column Name

Data Type

Description

index

Integer

Identifier column used for joining

time

String

The time of day taken at the point

altitude

Integer

The above sea-level altitude measured in meters

leg_length

Integer

The distance of the current leg measured in meters

leg_time

String

The amount of time that has elapsed for the current leg

leg_speed

Real

The speed the snowboarder is traveling measured in kilometers / hour

leg_course

Integer

The cardinal direction of travel measured in degrees.

position

String

The latitude / longitude coordinate

 

 

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 Wroclaw and open the 3 layers provided in the sample data pack by selecting Add vector layer.  Rearrange the layers so that the track_line is underneath the track_points.  Right click on the track_line layer and select Properties.  In the Style tab change the width of the line to 0.5 and set the color to a sky blue, click OK.


blue_track_line 


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:

  • Join Layer: data_attributes
  • Join field: index
  • Target field: track_se_1

You can leave the Cache join layer in virtual memory checked

Click OK, then Apply and OK to close the Properties window.


vector_join_using_field 


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.

 

Renaming Columns

 

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.

 

Removing Columns

 

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:

  • track_seg_
  • magvar
  • geoidheigh
  • name
  • cmt
  • desc
  • src
  • link1_href
  • link1_text
  • link1_type
  • link2_href
  • link2_text
  • link2_type
  • sym
  • type
  • fix
  • sat
  • hdop
  • vdop
  • pdop
  • ageofdgpsd
  • dgpsid
  • time_1

 

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.


remove_columns_using_table_manager 


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_thematic_map 


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.


creating_custom_color_ramp 


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.

 

thematic_map

 

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.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images