Importing Text Files in to QGIS

 

Introduction

Some data, such as Points of Interest and MasterMap Topography Layer Building Height Attribute, are provided as text files and do not contain feature geometries (Points of Interest contains eastings and northings which can be used to locate the features). When loading these in to QGIS it is necessary to create a definition file that specifies the data type of each field. There are two ways of loading text files in to QGIS and the definition file ensures that QGIS loads the file correctly.

Preparing the data

Before you can load data from a text file in to QGIS you must first create a definition file called <filename>.csvt in the same directory as the text file you wish to import, where <filename> is the same as the name of the file you wish to import. The .csvt file enables definition of the following data types: Integer, Real, String, Date (YYYY-MM-DD), Time (HH:MM:SS+nn) and DateTime (YYYY-MM-DD HH:MM:SS+nn).

The structure of .csvt files is very simple: it should contain just a single line and the data types for each column should be enclosed in double quotes and comma separated. For example if we have a comma separated text file called example.csv which contains a header row and has three fields for FeatureID (whole number), Area (decimal number) and Name (free text) the example.csvt file would look as follows:

"Integer","Real","String"

Adding the data to QGIS

There are two ways of adding text files to QGIS:

  1. Add Vector Layer
  2. Add Delimited Text Layer

The Add Vector Layer function only works for text files that use the comma character as the field delimiter and it automatically assigns each column as the String data type, which potentially limits subsequent analysis.

If your text file uses a different delimiter character, for example the Points of Interest data available through Digimap is supplied with a pipe character as the field delimiter, then you should use the Add Delimited Text Layer instead. As well as handling different delimiters, the Add Delimited Text Layer option will also create point features from x,y coordinates in the text file and allow you to select the correct geographic projection. The instructions below use the Add Delimited Text Layer function as this is far more flexible.

  1. Having already created your .csvt definition file, in your QGIS window select the 'Add Delimited Text Layer' button or menu item:
  2. The following window will appear:
  3. Browse to the file you wish to import (ensuring that the .csvt definition file has already been created and has the same filename as the file you wish to import).
  4. Modify the 'Layer Name' if required.
  5. In the 'File Format' section select the correct delimiter, in the screen grab above we have selected the pipe symbol (|) as the delimiter.
  6. If your text file has a header row, ensure the 'First record has field names' check box is selected.
  7. To create point features when importing the data ensure 'Point coordinates' is selected and choose the two columns that contain the X and Y coordinates of each point using the drop downs. If your data does not include coordinates select the 'No geometry (attribute table only)' option.
  8. The bottom half of the window shows the first few rows of the data, the data types will be set using the corresponding .csvt file.
  9. Press the OK button to begin the import.
  10. You will be prompted to select the correct geographic projection to ensure the features are drawn at the correct location.

Using the data for analysis

Once added to QGIS the data can be analysed as required, however you may wish to save the data as a geographic dataset for future use.

  1. Right click on the dataset in the contents panel.
  2. Select 'Save As...' from the popup menu:
  3. In the 'Save vector layer as...' dialog specify the format, filename, and projection and press the OK button:
  4. The data will be automatically added to your QGIS map window.

Download .csvt Files

Standard .csvt files created by EDINA for use with the data products downloaded through Digimap are available using the links below:

OS Product Download
MasterMap Topography Layer Building Height Attribute bha.csvt
Points of Interest poi.csvt
Note: you may need to rename the above .csvt files so that they match the filenames of the text files you are loading in to QGIS.

Further Information

Further information on importing CSV files in to QGIS can be found here: http://www.gdal.org/drv_csv.html. There are also numerous online tutorials available that cover the topic.