Skip to content

Commit 99e6a62

Browse files
committed
Removing processing of extraneous blank fields and enlarged arrays
1 parent 0beb170 commit 99e6a62

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

resources/context_help/QgsDelimitedTextSourceSelect

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ be double, otherwise the type will be text.
3535
QGIS can also read the types from an OGR CSV driver compatible "csvt" file.
3636
This is a file alongside the data file, but with a "t" appended to the file name.
3737
The file should just contain one line which lists the type of each field.
38-
Valid types are "integer", "real", "string", "date", "time", and "datetime". The date, time, and datetime types are treated as strings in QGIS.
38+
Valid types are "integer", "long", "longlong", "real",
39+
"string", "date", "time", and "datetime".
40+
The date, time, and datetime types are treated as strings by the delimited text provider.
3941
Each type may be followed by a width and precision, for example "real(10.4)".
4042
The list of types are separated by commas, regardless of the delimiter used in the data file. An
4143
example of a valid format file would be:
@@ -215,9 +217,11 @@ feature id to each record which is the line number in the source file on which
215217
the record starts.
216218
</p>
217219
<p>
218-
Each attribute also has a data type, one of string (text), integer, or real number.
220+
Each attribute also has a data type, one of string (text), integer, longlong,
221+
or real number.
219222
The data type is inferred from the content of the fields - if every non blank value
220-
is a valid integer then the type is integer, otherwise if it is a valid real
223+
is a valid integer then the type is integer, otherwise if it is a valid long long
224+
nubmer then the type is longlong, otherwise if it is a valid real
221225
number then the type is real, otherwise the type is string. Note that this is
222226
based on the content of the fields - quoting fields does not change the way they
223227
are interpreted.

src/providers/delimitedtext/qgsdelimitedtextprovider.cpp

+28-11
Original file line numberDiff line numberDiff line change
@@ -562,29 +562,46 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
562562
{
563563

564564
QString &value = parts[i];
565-
if ( value.isEmpty() ) {
565+
// Ignore empty fields - spreadsheet generated CSV files often
566+
// have random empty fields at the end of a row
567+
if ( value.isEmpty() )
568+
continue;
569+
570+
// Expand the columns to include this non empty field if necessary
571+
572+
while ( couldBeInt.size() <= i )
573+
{
566574
isEmpty.append( true );
567575
couldBeInt.append( false );
568576
couldBeLongLong.append( false );
569577
couldBeDouble.append( false );
570-
continue;
571578
}
572-
// try to convert attribute values to integer, long and double
573-
isEmpty.append( false );
574-
// try all possible formats
575-
couldBeInt.append( true );
576-
couldBeLongLong.append( true );
577-
couldBeDouble.append( true );
579+
580+
// If this column has been empty so far then initiallize it
581+
// for possible types
582+
583+
if ( isEmpty[i] )
584+
{
585+
isEmpty[i] = false;
586+
couldBeInt[i] = true;
587+
couldBeLongLong[i] = true;
588+
couldBeDouble[i] = true;
589+
}
590+
591+
// Now test for still valid possible types for the field
592+
// Types are possible until first record which cannot be parsed
593+
578594
if ( couldBeInt[i] )
579595
{
580596
value.toInt( &couldBeInt[i] );
581597
}
582598

583-
if ( couldBeLongLong[i] )
599+
if ( couldBeLongLong[i] && ! couldBeInt[i] )
584600
{
585601
value.toLongLong( &couldBeLongLong[i] );
586602
}
587-
if ( couldBeDouble[i] )
603+
604+
if ( couldBeDouble[i] && ! couldBeLongLong[i] )
588605
{
589606
if ( ! mDecimalPoint.isEmpty() )
590607
{
@@ -627,7 +644,7 @@ void QgsDelimitedTextProvider::scanFile( bool buildIndexes )
627644
fieldType = QVariant::LongLong; //QVariant doesn't support long
628645
typeName = "longlong";
629646
}
630-
else if ( csvtTypes[i] == "real" )
647+
else if ( csvtTypes[i] == "real" || csvtTypes[i] == "double" )
631648
{
632649
fieldType = QVariant::Double;
633650
typeName = "double";

0 commit comments

Comments
 (0)