Delimited Text File Layer

Loads and displays delimited text files

Overview
Creating a delimited text layer
How the delimiter, quote, and escape characters work
How regular expression delimiters work
How WKT text is interpreted
Example of a text file with X,Y point coordinates
Example of a text file with WKT geometries
Using delimited text layers in Python

Overview

A "delimited text file" contains data in which each record starts on a new line, and is split into fields by a delimiter such as a comma. This type of file is commonly exported from spreadsheets (for example CSV files) or databases. Typically the first line of a delimited text file contains the names of the fields.

Delimited text files can be loaded into QGIS as a layer. The records can be displayed spatially either as a point defined by X and Y coordinates, or using a Well Known Text (WKT) definition of a geometry which may describe points, lines, and polygons of arbitrary complexity. The file can also be loaded as an attribute only table, which can then be joined to other tables in QGis.

In addition to the geometry definition the file can contain text, integer, and real number fields. QGis will choose the type of field based on its contents.

Creating a delimited text layer

Creating a delimited text layer involves choosing the data file, defining the format (how each record is to be split into fields), and defining the geometry is represented. This is managed with the delimited text dialog as detailed below. The dialog box displays a sample from the beginning of the file which shows how the format options have been applied.

Choosing the data file

Use the "Browse..." button to select the data file. Once the file is selected the layer name will automatically be populated based on the file name. The layer name is used to represent the data in the QGis legend.

By default files are assumed to be encoded as UTF-8. However other file encodings can be selected. For example "System" uses the default encoding for the operating system. If you are expecting to move the QGis project then it is safer to use a specific encoding.

Specifying the file format

The file format can be one of

Record and field options

The following options affect the selection of records and fields from the data file

Geometry definition

The geometry is can be define as one of

For point coordinates the following options apply:

For well known text geometry the following options apply:

How the delimiter, quote, and escape characters work

Records are split into fields using three character sets: delimiter characters, quote characters, and escape characters. Quote and escape characters cannot be the same as delimiter characters - they will be ignored if they are. Escape characters can be the same as quote characters, but behave differently if they are.

The delimiter characters are used to mark the end of each field. If more than one delimiter character is defined then any one of the characters can mark the end of a field. The quote and escape characters can override the delimiter character, so that it is treated as a normal character.

Quote characters may be used to mark the beginning and end of quoted fields. Quoted fields can contain delimiters and may span multiple lines in the text file. If a field is quoted then it must start and end with the same quote character. Quote characters cannot occur within a field unless they are escaped.

Escape characters which are not quote characters force the following character to be treated normally (that is, to stop it being treated as a new line, delimiter, or quote character).

If a quote character is also an escape character, then it can be represented in a quoted field by entering it twice. For example if ' is a quote character and an escape character, then the string 'Smith''s Creek' will represent the value Smith's Creek.

How regular expression delimiters work

Regular expressions are mini-language used to represent character patterns. There are many variations of regular expression syntax - QGis uses the syntax provided by the QRegExp class of the Qt framework.

In a regular expression delimited file each line is treated as a record. Each match of the regular expression in the line is treated as the end of a field.

How WKT text is interpreted

The delimited text layer recognizes the following well known text types - POINT, MULTIPOINT, LINESTRING, MULTILINESTRING, POLYGON, and MULTIPOLYGON. It will accept geometries with a Z coordinate (eg "POINT Z"), a measure ("POINT M"), or both ("POINT ZM").

It can also handle the PostGIS EWKT variation, in which the geomtry is preceded by an spatial reference system id (eg "SRID=4326;POINT(175.3 41.2)"), and a variant used by Informix in which the WKT is preceded by an integer spatial reference id (eg "1 POINT(175.3 41.2)"). In both cases the SRID is ignored.

Example of a text file with X,Y point coordinates

X;Y;ELEV
-300120;7689960;13
-654360;7562040;52
1640;7512840;3

This file:

Example of a text file with WKT geometries

id|wkt
1|POINT(172.0702250 -43.6031036)
2|POINT(172.0702250 -43.6031036)
3|POINT(172.1543206 -43.5731302)
4|POINT(171.9282585 -43.5493308)
5|POINT(171.8827359 -43.5875983)

This file:

Using delimited text layers in Python