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.
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.
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.
Now, if you try to import it using the other two options. You’ll notice that the Age field becomes a string.
So how do we solve this? Enter 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.
For the .csvt file to work it needs 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.
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.
foss4g foss4gph qgis gis presentation
foss foss4g opensource freeasinfreedom freesoftware
foss4g foss4gph thesis uaap mapping the geography of the uaap spatial analytics basketball basketball analytics
foss4g foss4gph qgis gis presentation
If you find my website or any of the materials I share useful, you can consider donating to the cause below.
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)