Skip to content

Commit f5fc93c

Browse files
author
gsherman
committed
Patch for ticket #3320 to fix delimited text plugin crash
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14890 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 25dc6fd commit f5fc93c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/providers/delimitedtext/qgsdelimitedtextprovider.cpp

+17-12
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ QStringList QgsDelimitedTextProvider::splitLine( QString line )
134134
QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
135135
: QgsVectorDataProvider( uri )
136136
, mHasWktField( false )
137+
, mFirstDataLine(0)
137138
, mFieldCount( 0 )
138139
, mXFieldIndex( -1 ), mYFieldIndex( -1 )
139140
, mWktFieldIndex( -1 )
@@ -199,7 +200,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
199200
if ( mFileName.isEmpty() || mDelimiter.isEmpty() )
200201
{
201202
// uri is invalid so the layer must be too...
202-
QString( "Data source is invalid" );
203+
QgsDebugMsg( "Data source is invalid" );
203204
return;
204205
}
205206

@@ -238,12 +239,13 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
238239
{
239240
lineNumber++;
240241
line = readLine( mStream ); // line of text excluding '\n', default local 8 bit encoding.
241-
if ( line.isEmpty() )
242-
continue;
243242

244243
if ( lineNumber < mSkipLines + 1 )
245244
continue;
246245

246+
if ( line.isEmpty() )
247+
continue;
248+
247249
if ( !hasFields )
248250
{
249251
// Get the fields from the header row and store them in the
@@ -297,15 +299,14 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
297299
}
298300
else // hasFields == true - field names already read
299301
{
302+
if( mFirstDataLine == 0 ) mFirstDataLine = lineNumber;
300303

301304
// split the line on the delimiter
302305
QStringList parts = splitLine( line );
303306

304-
// Skip malformed lines silently. Report line number with nextFeature()
305-
if ( parts.size() != mFieldCount )
306-
{
307-
continue;
308-
}
307+
// Ensure that the input has at least the required number of fields (mainly to tolerate
308+
// missed blank strings at end of row)
309+
while( parts.size() < mFieldCount ) parts.append("");
309310

310311
if ( mHasWktField && mWktFieldIndex >= 0 )
311312
{
@@ -443,13 +444,17 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
443444
// lex the tokens from the current data line
444445
QStringList tokens = splitLine( line );
445446

447+
while( tokens.size() < mFieldCount ) tokens.append("");
448+
446449
QgsGeometry *geom = 0;
447450

448451
if ( mHasWktField && mWktFieldIndex >= 0 )
449452
{
450453
try
451454
{
452455
QString &sWkt = tokens[mWktFieldIndex];
456+
// Remove Z and M coordinates if present, as currently fromWkt doesn't
457+
// support these.
453458
if ( mWktHasZM )
454459
{
455460
sWkt.remove( mWktZMRegexp ).replace( mWktCrdRegexp, "\\1" );
@@ -505,6 +510,7 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
505510
i != mAttributesToFetch.end();
506511
++i )
507512
{
513+
508514
QString &value = tokens[attributeColumns[*i]];
509515
QVariant val;
510516
switch ( attributeFields[*i].type() )
@@ -624,11 +630,10 @@ void QgsDelimitedTextProvider::rewind()
624630
{
625631
// Reset feature id to 0
626632
mFid = 0;
627-
// Skip ahead one line since first record is always assumed to be
628-
// the header record
633+
// Skip to first data record
629634
mStream->seek( 0 );
630-
int n = mSkipLines + 1;
631-
while ( n-- )
635+
int n = mFirstDataLine-1;
636+
while ( n-- > 0 )
632637
readLine( mStream );
633638
}
634639

src/providers/delimitedtext/qgsdelimitedtextprovider.h

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider
222222

223223
long mNumberFeatures;
224224
int mSkipLines;
225+
int mFirstDataLine; // Actual first line of data (accounting for blank lines)
225226

226227
//! Storage for any lines in the file that couldn't be loaded
227228
QStringList mInvalidLines;

0 commit comments

Comments
 (0)