I am trying to load a file with many columns into QGIS. With both .txt and .csv format the leading zeroes of some data values get lost. However, I need the leading zeroes to merge the file with a map.
Using .csvt does not make much sense here, as I have too many columns and will use different files with different numbers of columns and therefore I would have to adapt the .csvt file everytime.
I am also not able to use a field calculator to adapt the columns once loaded into QGIS as the field calculator is inactive for .csv or .txt files.
Is there another way to ensure that the leading zeroes do not get lost? The file is generated with R. I don't know if this is relevant
Answer
When importing CSV files, QGIS is able to find the type of column used and suggest the best format.
The problem is when the first values are "numbers as string" like in your case, QGIS changes them automatically to numbers. It happens also when you have empty fields at the beginning of the file, making QGIS confused and displaying the bad field type.
In this case, you have to manually edit the field type in QGIS to get the right format, which can be very long to do if you have a big number of columns.
A little trick to force QGIS to set things the way you want : insert in the first line of your csv file one record with something he can't mistake (dummy values).
For example, the first line below is a looking like number like yours + an empty field that should be float with 2 decimals :
ID,Temperature, city
0001, NULL, New York
QGIS might convert the first field as an integer (removing the zeros), then the second might be a string, erasing all your floating values starting at the 150th row)
Insert a line just after the field names and put in values he can't mistake (A for a string field, 0.01 for a float with 2 decimals, etc) :
ID,Temperature, city
A,0.01,A
0001, NULL, New York
QGIS will read the first line and suggest or display directly the right field type.
The downside is that you have one line with dummy values.
No comments:
Post a Comment