Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrook committed Apr 11, 2013
1 parent c6f678f commit ebe0406
Show file tree
Hide file tree
Showing 12 changed files with 1,125 additions and 423 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ bool QgsDelimitedTextFeatureIterator::nextFeature( QgsFeature& feature )
if ( !geom && P->mWkbType != QGis::WKBNoGeometry ) if ( !geom && P->mWkbType != QGis::WKBNoGeometry )
{ {
// Already dealt with invalid lines in provider - no need to repeat // Already dealt with invalid lines in provider - no need to repeat
// P->mInvalidLines << line; // removed code (CC 2013-04-13) ...
// P->mInvalidLines << line;
// In any case it may be a valid line that is excluded because of
// bounds check...
continue; continue;
} }


Expand Down Expand Up @@ -153,7 +156,7 @@ QgsGeometry* QgsDelimitedTextFeatureIterator::loadGeometryWkt( const QStringList
// support these. // support these.
if ( P->mWktHasZM ) if ( P->mWktHasZM )
{ {
sWkt.remove( P->mWktZMRegexp ).replace( P->mWktCrdRegexp, "\\1" ); sWkt.remove( P->WktZMRegexp ).replace( P->WktCrdRegexp, "\\1" );
} }


geom = QgsGeometry::fromWkt( sWkt ); geom = QgsGeometry::fromWkt( sWkt );
Expand All @@ -163,7 +166,7 @@ QgsGeometry* QgsDelimitedTextFeatureIterator::loadGeometryWkt( const QStringList
geom = 0; geom = 0;
} }


if ( geom && geom->wkbType() != P->mWkbType ) if ( geom && geom->type() != P->mGeometryType )
{ {
delete geom; delete geom;
geom = 0; geom = 0;
Expand Down
36 changes: 28 additions & 8 deletions src/providers/delimitedtext/qgsdelimitedtextfile.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include <QFile> #include <QFile>
#include <QDataStream> #include <QDataStream>
#include <QTextStream> #include <QTextStream>
#include <QTextCodec>
#include <QStringList> #include <QStringList>
#include <QRegExp> #include <QRegExp>
#include <QUrl> #include <QUrl>


QgsDelimitedTextFile::QgsDelimitedTextFile( QString url ) : QgsDelimitedTextFile::QgsDelimitedTextFile( QString url ) :
mFileName(QString()), mFileName(QString()),
mEncoding("UTF-8"),
mFile(0), mFile(0),
mStream(0), mStream(0),
mDefinitionValid(false), mDefinitionValid(false),
Expand Down Expand Up @@ -74,6 +76,11 @@ bool QgsDelimitedTextFile::open()
return false; return false;
} }
mStream = new QTextStream( mFile ); mStream = new QTextStream( mFile );
if( mEncoding.isEmpty() && mEncoding != "System")
{
QTextCodec *codec = QTextCodec::codecForName(mEncoding.toAscii());
mStream->setCodec(codec);
}
} }
return true; return true;
} }
Expand Down Expand Up @@ -101,6 +108,12 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
// Extract the file name // Extract the file name
setFileName( url.toLocalFile()); setFileName( url.toLocalFile());


// Extract the encoding
if( url.hasQueryItem("encoding"))
{
mEncoding =url.queryItemValue("encoding");
}

// The default type is csv, to be consistent with the // The default type is csv, to be consistent with the
// previous implementation (except that quoting should be handled properly) // previous implementation (except that quoting should be handled properly)


Expand Down Expand Up @@ -150,19 +163,16 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
} }


QgsDebugMsg( "Delimited text file is: " + mFileName ); QgsDebugMsg( "Delimited text file is: " + mFileName );
QgsDebugMsg( "Encoding is: " + mEncoding);
QgsDebugMsg( "Delimited file type is: " + type ); QgsDebugMsg( "Delimited file type is: " + type );
QgsDebugMsg( "Delimiter is: " + delimiter ); QgsDebugMsg( "Delimiter is: [" + delimiter + "]" );
QgsDebugMsg( "Quote character is: " + quote); QgsDebugMsg( "Quote character is: [" + quote +"]");
QgsDebugMsg( "Escape character is: " + escape); QgsDebugMsg( "Escape character is: [" + escape + "]");
QgsDebugMsg( "Skip lines: " + QString::number(mSkipLines) ); QgsDebugMsg( "Skip lines: " + QString::number(mSkipLines) );
QgsDebugMsg( "Skip lines: " + QString(mUseHeader ? "Yes" : "No") ); QgsDebugMsg( "Skip lines: " + QString(mUseHeader ? "Yes" : "No") );


if( type == "csv" )
{
setTypeCSV(delimiter,quote,escape);
}
// Support for previous version of plain characters // Support for previous version of plain characters
else if( type == "plain" ) if( type == "csv" || type == "plain" )
{ {
setTypeCSV(delimiter,quote,escape); setTypeCSV(delimiter,quote,escape);
} }
Expand All @@ -184,6 +194,10 @@ bool QgsDelimitedTextFile::setFromUrl( QUrl &url )
QUrl QgsDelimitedTextFile::url() QUrl QgsDelimitedTextFile::url()
{ {
QUrl url = QUrl::fromLocalFile( mFileName ); QUrl url = QUrl::fromLocalFile( mFileName );
if( mEncoding != "UTF-8" )
{
url.addQueryItem("encoding",mEncoding);
}
url.addQueryItem("type",type()); url.addQueryItem("type",type());
if( mType == DelimTypeRegexp ) if( mType == DelimTypeRegexp )
{ {
Expand Down Expand Up @@ -212,6 +226,12 @@ void QgsDelimitedTextFile::setFileName( QString filename )
mFileName = filename; mFileName = filename;
} }


void QgsDelimitedTextFile::setEncoding( QString encoding )
{
resetDefinition();
mEncoding = encoding;
}

QString QgsDelimitedTextFile::type() QString QgsDelimitedTextFile::type()
{ {
if( mType == DelimTypeWhitespace ) return QString("whitespace"); if( mType == DelimTypeWhitespace ) return QString("whitespace");
Expand Down
10 changes: 10 additions & 0 deletions src/providers/delimitedtext/qgsdelimitedtextfile.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ class QgsDelimitedTextFile
return mFileName; return mFileName;
} }


/** Set the file encoding (defuault is UTF-8)
* @param encoding the encoding to use for the fileName()
*/
void setEncoding( QString encoding );
/** Return the file encoding
* @return encoding The file encoding
*/
QString encoding(){ return mEncoding; }

/** Decode the parser settings from a url as a string /** Decode the parser settings from a url as a string
* @param url The url from which the delimiter and delimiterType items are read * @param url The url from which the delimiter and delimiterType items are read
*/ */
Expand Down Expand Up @@ -259,6 +268,7 @@ class QgsDelimitedTextFile
Status (QgsDelimitedTextFile::*mParser)( QStringList &fields ); Status (QgsDelimitedTextFile::*mParser)( QStringList &fields );


QString mFileName; QString mFileName;
QString mEncoding;
QFile *mFile; QFile *mFile;
QTextStream *mStream; QTextStream *mStream;


Expand Down
Loading

0 comments on commit ebe0406

Please sign in to comment.