4

How to Specify Data Types of CSV Columns for Use in QGIS

 3 years ago
source link: https://anitagraser.com/2011/03/07/how-to-specify-data-types-of-csv-columns-for-use-in-qgis/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Specify Data Types of CSV Columns for Use in QGIS

Foreword (added 2015-04-11)

There are two main options to load .csv files into QGIS

  • “Add delimited text layer” will guess the column data types. Use the “no geometry” option to load CSVs without coordinates.
  • “Add vector layer” by default interprets all columns as strings.

The following post describes how to change the default behavior of “Add vector layer”.

If you load .csv files through “Add vector layer”, all columns are interpreted as strings. That’s most likely not what you want, but it’s OGR’s default behaviour:

The OGR CSV driver returns all attribute columns with a type of string if no field type information file (with .csvt extension) is available.

Let’s create a .csvt file then!

The .csvt file has to have the same name as the .csv file it describes. (The same concept is used for Shapefiles.) It 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).

A .csvt file contains only one line and the types for each column have to be quoted and comma separated, e.g.

"Integer","Real","String"

You can even specify width and precision of each column, e.g.

"Integer(6)","Real(5.5)","String(22)"

Read more at: www.gdal.org/ogr/drv_csv.html

Advertisements
Report this ad

← Previous post Next post →

  1. Alister said: 2011-08-2300:42
    • underdark said: 2011-08-2307:11
  2. Alister said: 2011-10-1906:20
  3. Fabrizio said: 2011-11-0915:07
    • underdark said: 2011-11-0915:48
  4. Hendrik said: 2012-01-0412:27
    • underdark said: 2012-01-0416:03
      • Hendrik said: 2012-01-0510:20

        I tried it again with exemple-files from http://gothos.info/2011/04/joining-csv-files-in-qgis/ but qgis sets all types to “String” :/

        maybe i make a mistake creating the files?
        I have an excel xls-file and i save this with excel as an csv-file.
        then I open it with the editior an save it as a csvt-file by replace the preset “.txt” from the editor with “.csvt”.

        then i load the csv with qgis 1.7.3 by add it as vector layer and join it with my shapefile.

        i thought that import and joining tables is one of the simplest and basic functions a gis program should be able to run easily. and this is not what i call easy :(

        anyway where is my mistake?

      • underdark said: 2012-01-0521:52
      • Hendrik said: 2012-01-0511:16
  5. Matthew Sussman said: 2012-04-2423:29
  6. pieter said: 2012-05-0714:20

    about the problem of creating/opening a CSV with Excell (I’ve got this information from http://www.baruch.cuny.edu/geoportal/practicum/gis_prac_4.html):

    “You should be careful when opening csv files, or any delimited text files, in Microsoft Excel. Excel imports the CSV and automatically saves any value that looks like a number as a number. This has the unintended effect of rendering identifiers like FIPS codes and ZIP Codes useless, as zeros are dropped from preceding values. Even if you open a CSV file in Excel and don’t save it, the file is still altered. In order to convert values back to their original form you would have to use the concatenate formula on any values that are less than their expected length and pad them with zeros. You can avoid these problems by working with CSV in text editors, or by using other spreadsheet programs. For example, when you open a csv file in Open Office Calc you are prompted to designate the data type for each field. Designate your identifiers as text / strings and they will be preserved. “


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK