Using a .csvt file to specify CSV column data type when loading a CSV in QGIS

07 Aug 2018 ᛫ 2 min read

SHARE:

CSV are very useful formats consumable not only by GIS but also by other platforms and frameworks. It’s one of the most common ways to store and share tabular data.

CSVs can be loaded in QGIS in a couple of ways.

  1. Via the Add Delimited Text Layer in the Data Source Manager (CTRL + L)
  2. Via the Add Vector Layer in the Data Source Manager (CTRL + L)
  3. Dragging the CSV in the QGIS interface

The safest method is using the Add Delimited Text Layer option. I say safest because this option guesses the data type of the columns in your CSV when adding them as layers in QGIS. Most of the time, strings will be considered strings and numbers will be considered as integers or real. This is not the case for the other two options.

When adding a CSV via the Add Vector Layer option or Dragging it into QGIS, the fields become strings by default. This becomes very problematic if you have numeric data in your CSV.

To illustrate: Lets’ say we have a CSV of names and ages of persons (sample.csv) like the one below.

Sample CSV to load in QGIS

If we import this as a layer in QGIS via the Add Delimited Text Layer and check its fields, we can see that the data types are correct. The Name is a text/string and the Age is an integer.

Add CSV delimited text layer in QGIS
Layer properties of CSV delimited text layer in QGIS using data source manager

Now, if you try to import it using the other two options. You’ll notice that the Age field becomes a string.

Load CSV delimited text layer in QGIS as vector data source manager
Load CSV delimited text layer in QGIS by drag and drop

So how do we solve this? Enter the .csvt file.

The .csvt file

The .csvt file is a one-line text file with a .csvt file extension and holds information on the data types of the columns of the CSV file it corresponds to. CSVTs enable the user to define 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).

For example, we can create the following .csvt file (sample.csvt) for our sample csv.

Sample CSVT file

For the .csvt file to work it needs to:

  1. Have the same name as the CSV file it corresponds to (i.e. sample.csv has sample.csvt).
  2. Be in the same directory as the CSV it corresponds to.

With the .csvt file present, we can see that the data types of the fields in the CSVs are classified by QGIS based on the .csvt when using Add Vector Layer or Dragging the file in QGIS.

Correct load CSV delimited text layer in QGIS as vector data source manager with csvt
Load correct CSV delimited text layer in QGIS by drag and drop with csvt

And that’s the use of .csvt files in ensuring the correct data type for your CSV fields when not using the Add Delimited Text Layer option when loading them in QGIS.

SHARE:
comments powered by Disqus

You may also like:

Towards a spatial analysis of shooting in Philippine basketball (FOSS4G2021)

01 Oct 2021 ᛫ 1 min read

The opposite of free/libre and open source isn't commercial, it's proprietary

27 May 2021 ᛫ 2 min read

Towards a Spatial Analysis of Philippine Basketball: Applications in the UAAP MBT (Season 81) [Part 1]

02 May 2021 ᛫ 4 min read

Win and let win: On being unconventional, openness, and building communities

30 Apr 2021 ᛫ 1 min read

QGIS Styles based on HLURB Land Use Categories and Color Coding (CLUP Guidebook Vol 3, 2014)

15 Dec 2020 ᛫ 2 min read

Support BNHR

If you find my website or any of the materials I share useful, you can consider donating to the cause below.

Donate and support BNHR

BNHR

[email protected]

Creative Commons License
Except when explicitly stated otherwise, this work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)