[GIS] Using field calculator for attribute table data in QGIS

attribute-tablecsvfield-calculatorqgisqgis-processing

I'm experimenting with adding fields to attribute table data in QGIS 2.14.5LTR (mainly *.csv files) and stumble into a variety of issues or some things I do not understand, respectively. I imported a csv file via import text file as layer button, drag and and dropped the same file to the project and saved the latter as csv from QGIS.

My s(i/a)mple project looks like this (note the edit state of the layers):

enter image description here

1st scenario – import *.csv file and trying to add a field with field calculator.

When opening a *.csv file and trying to do so, I'm only able to add a virtual field to the table (Which can obviously not be used for joins, which is the aim of the game in the end). Strange enough: When dragging the same file into the project, I'm able to toggle editing mode and add new fields.

enter image description here

The imported *.csv file looks like this (no column headers):

92891A14.1;Eigenausbau;92891A14;09475169;Bayern;Hof, Landkreis;95131;Schwarzenbach;Döbra;Tannenweg;3;;2;4475738;5571977;272;11,99;11300;;30001;;100001;
92891A17.32;Eigenausbau;92891A17;09475169;Bayern;Hof, Landkreis;95131;Schwarzenbach;Schwarzenbach;Thiemitztalstr.;11;;1;4472917;5572009;432;16,61;12430;;30001;;90001;
92891A30.31;Eigenausbau;92891A30;09475169;Bayern;Hof, Landkreis;95131;Schwarzenbach;Schwarzenbach;Hochstr.;26;;15;4473440;5571918;147;4,78;13560;;30001;;100001;
92891A1.69;Eigenausbau;92891A1;09475169;Bayern;Hof, Landkreis;95131;Schwarzenbach;Schwarzenbach;Hauptstr.;16;;1;4473041;5571724;428;16,93;12430;;30001;;90001;

2nd scenario – saving the opened csv file as csv

Done so, I'm now able to switch to toggle editing mode and add a new field to the layer the usual way:

enter image description here

The saved *.csv file looks like

field_1,field_2,field_3,field_4,field_5,field_6,field_7,field_8,field_9,field_10,field_11,field_12,field_13,field_14,field_15,field_16,field_17,field_18,field_19,field_20,field_21,field_22
92891A17.32,Eigenausbau,92891A17,9475169,Bayern,"Hof, Landkreis",95131,Schwarzenbach,Schwarzenbach,Thiemitztalstr.,11,,1,4472917,5572009,432,16.61,12430,,30001,,90001
92891A30.31,Eigenausbau,92891A30,9475169,Bayern,"Hof, Landkreis",95131,Schwarzenbach,Schwarzenbach,Hochstr.,26,,15,4473440,5571918,147,4.78,13560,,30001,,100001
92891A1.69,Eigenausbau,92891A1,9475169,Bayern,"Hof, Landkreis",95131,Schwarzenbach,Schwarzenbach,Hauptstr.,16,,1,4473041,5571724,428,16.93,12430,,30001,,90001
92891A17.31,Eigenausbau,92891A17,9475169,Bayern,"Hof, Landkreis",95131,Schwarzenbach,Schwarzenbach,Thiemitztalstr.,22,,1,4472928,5572055,428,16.47,12430,,30001,,90001

Note: re-importing the saved *.csv layer leeds to the same issue as in 1st scenario

3rd scenario – Trying to add a field to the saved layer via field calculator from processing toolbox

Further more, attribute tables can not be selected as input layers for the field calculator from the processing toolbox (note the empty dropdown):

enter image description here

Questions:

  • What is the difference between the two *.csv layers in 1st and 2nd scenario (or the different ways they come into the project)? Why can the saved layer be edited and have fields added and the layer imported from a text file can not, but can, when dragged and dropped to the project?

  • What is the difference between all the field calculators and is there a way to add fields to a table with any processing tool?

Best Answer

Interesting! I don't usually drag and drop files, but I could add fields to a CSV layer when I tried this. Which feels wrong :)

The changes weren't saved when I committed the changes. I could only get the changes to commit if I saved it as a new CSV file using save as... (Tested on QGIS 2.16.3)

I usually use CSV as a quick way of getting data from python scripts into QGIS, by writing WKT into a CSV. I have to use TAB or pipe (|) as a delimiter because WKT contains commas. If you drag-and-drop you don't get the option to set the delimiter and it assumes commas.

Take a look at the layer capabilities with the different scenarios (use Layer Properties > Metadata and scroll down the Properties section to look for Capabilities)

If you import a delimited layer (with geometry) you should see...

Create Spatial Index, Fast Access to Features at ID, Curved Geometries

If I drag in a CSV file, or import one without a geometry, I see

Add Features

Notice that in both cases, there isn't an "Add attributes" capability. This might explain why I need to save as a new CSV.

The GDAL/OGR CSV driver doesn't allow in-place edits (no 'Add features' or 'Add attributes' capabilities).

I suspect that when you drag in a CSV file without a geometry, QGIS treats it as a text file. If you import a csv and the geometry is recognised, the GDAL/OGR driver is used, and it won't let you make edits.

Related Question