Skip to content
Permalink
Browse files
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
  • Loading branch information
gsherman committed Dec 11, 2010
1 parent 25dc6fd commit f5fc93cf853764ac0c628cc2816788734c6b3704
Showing with 18 additions and 12 deletions.
  1. +17 −12 src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
  2. +1 −0 src/providers/delimitedtext/qgsdelimitedtextprovider.h
@@ -134,6 +134,7 @@ QStringList QgsDelimitedTextProvider::splitLine( QString line )
QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
: QgsVectorDataProvider( uri )
, mHasWktField( false )
, mFirstDataLine(0)
, mFieldCount( 0 )
, mXFieldIndex( -1 ), mYFieldIndex( -1 )
, mWktFieldIndex( -1 )
@@ -199,7 +200,7 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
if ( mFileName.isEmpty() || mDelimiter.isEmpty() )
{
// uri is invalid so the layer must be too...
QString( "Data source is invalid" );
QgsDebugMsg( "Data source is invalid" );
return;
}

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

if ( lineNumber < mSkipLines + 1 )
continue;

if ( line.isEmpty() )
continue;

if ( !hasFields )
{
// Get the fields from the header row and store them in the
@@ -297,15 +299,14 @@ QgsDelimitedTextProvider::QgsDelimitedTextProvider( QString uri )
}
else // hasFields == true - field names already read
{
if( mFirstDataLine == 0 ) mFirstDataLine = lineNumber;

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

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

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

while( tokens.size() < mFieldCount ) tokens.append("");

QgsGeometry *geom = 0;

if ( mHasWktField && mWktFieldIndex >= 0 )
{
try
{
QString &sWkt = tokens[mWktFieldIndex];
// Remove Z and M coordinates if present, as currently fromWkt doesn't
// support these.
if ( mWktHasZM )
{
sWkt.remove( mWktZMRegexp ).replace( mWktCrdRegexp, "\\1" );
@@ -505,6 +510,7 @@ bool QgsDelimitedTextProvider::nextFeature( QgsFeature& feature )
i != mAttributesToFetch.end();
++i )
{

QString &value = tokens[attributeColumns[*i]];
QVariant val;
switch ( attributeFields[*i].type() )
@@ -624,11 +630,10 @@ void QgsDelimitedTextProvider::rewind()
{
// Reset feature id to 0
mFid = 0;
// Skip ahead one line since first record is always assumed to be
// the header record
// Skip to first data record
mStream->seek( 0 );
int n = mSkipLines + 1;
while ( n-- )
int n = mFirstDataLine-1;
while ( n-- > 0 )
readLine( mStream );
}

@@ -222,6 +222,7 @@ class QgsDelimitedTextProvider : public QgsVectorDataProvider

long mNumberFeatures;
int mSkipLines;
int mFirstDataLine; // Actual first line of data (accounting for blank lines)

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

0 comments on commit f5fc93c

Please sign in to comment.