Skip to content

Commit

Permalink
Patch for delimited text, ticket #3414. Submitted by ccrook.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@15074 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
gsherman committed Jan 25, 2011
1 parent d80c90d commit dfb0602
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 76 deletions.
20 changes: 15 additions & 5 deletions python/core/qgscoordinatereferencesystem.sip
Expand Up @@ -23,11 +23,12 @@ class QgsCoordinateReferenceSystem

~QgsCoordinateReferenceSystem();

/*!
* Constructs a CRS object from a Wkt string
* @param theWkt A String containing a valid Wkt def
/*!
* Constructs a CRS object from a string definition as defined in the createFromString
* member function (by default a WKT definition).
* @param theDefinition A String containing a coordinate reference system definition.
*/
explicit QgsCoordinateReferenceSystem(QString theWkt);
explicit QgsCoordinateReferenceSystem( QString theDefinition );

/*! Use this constructor when you want to create a CRS object using
* a postgis SRID, an Epsg Id id or a QGIS CRS_ID.
Expand All @@ -38,7 +39,7 @@ class QgsCoordinateReferenceSystem

// Misc helper functions -----------------------

void createFromId(const long theId, CrsType theType=PostgisCrsId);
bool createFromId(const long theId, CrsType theType=PostgisCrsId);

/**
* \brief Set up this CRS from the given OGC CRS
Expand Down Expand Up @@ -70,6 +71,15 @@ class QgsCoordinateReferenceSystem
* @return bool TRUE if sucess else false
*/
bool createFromWkt(const QString theWkt);

/*! Set up this srs from a string definition, by default a WKT definition. Otherwise
* the string defines a authority, followed by a colon, followed by the definition.
* The authority can be one of "epsg", "postgis", "internal" for integer definitions,
* and "wkt" or "proj4" for string definitions. The implementation of each authority
* uses the corresponding createFrom... function.
* @param theDefinition A String containing a coordinate reference system definition.
*/
bool createFromString( const QString theDefinition );

/*! Set up this srs by fetching the appropriate information from the
* sqlite backend. First the system level read only srs.db will be checked
Expand Down
45 changes: 39 additions & 6 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -51,13 +51,13 @@ QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem()
mCRS = OSRNewSpatialReference( NULL );
}

QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( QString theWkt )
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( QString theDefinition )
: mMapUnits( QGis::UnknownUnit )
, mIsValidFlag( 0 )
, mValidationHint( "" )
{
mCRS = OSRNewSpatialReference( NULL );
createFromWkt( theWkt );
createFromString( theDefinition );
}


Expand All @@ -75,23 +75,56 @@ QgsCoordinateReferenceSystem::~QgsCoordinateReferenceSystem()
OSRDestroySpatialReference( mCRS );
}

void QgsCoordinateReferenceSystem::createFromId( const long theId, CrsType theType )
bool QgsCoordinateReferenceSystem::createFromId( const long theId, CrsType theType )
{
bool result = false;
switch ( theType )
{
case InternalCrsId:
createFromSrsId( theId );
result = createFromSrsId( theId );
break;
case PostgisCrsId:
createFromSrid( theId );
result = createFromSrid( theId );
break;
case EpsgCrsId:
createFromEpsg( theId );
result = createFromEpsg( theId );
break;
default:
//THIS IS BAD...THIS PART OF CODE SHOULD NEVER BE REACHED...
QgsDebugMsg( "Unexpected case reached!" );
};
return result;
}

bool QgsCoordinateReferenceSystem::createFromString( const QString theDefinition )
{
bool result = false;
QRegExp reCrsId("^(epsg|postgis|internal)\\:(\\d+)$",Qt::CaseInsensitive);
if( reCrsId.indexIn(theDefinition) == 0)
{
QString authName = reCrsId.cap(1).toLower();
CrsType type = InternalCrsId;
if( authName == "epsg" ) type = EpsgCrsId;
if( authName == "postgis" ) type = PostgisCrsId;
long id = reCrsId.cap(2).toLong();
result = createFromId(id,type);
}
else
{
QRegExp reCrsStr("^(?:(wkt|proj4)\\:)?(.+)$",Qt::CaseInsensitive);
if( reCrsStr.indexIn(theDefinition) == 0 )
{
if( reCrsStr.cap(1).toLower() == "proj4" )
{
result = createFromProj4(reCrsStr.cap(2));
}
else
{
result = createFromWkt(reCrsStr.cap(2));
}
}
}
return result;
}

bool QgsCoordinateReferenceSystem::createFromOgcWmsCrs( QString theCrs )
Expand Down
18 changes: 14 additions & 4 deletions src/core/qgscoordinatereferencesystem.h
Expand Up @@ -57,10 +57,11 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
~QgsCoordinateReferenceSystem();

/*!
* Constructs a CRS object from a Wkt string
* @param theWkt A String containing a valid Wkt def
* Constructs a CRS object from a string definition as defined in the createFromString
* member function (by default a WKT definition).
* @param theDefinition A String containing a coordinate reference system definition.
*/
explicit QgsCoordinateReferenceSystem( QString theWkt );
explicit QgsCoordinateReferenceSystem( QString theDefinition );

/*! Use this constructor when you want to create a CRS object using
* a postgis SRID, an EpsgCrsId id or a QGIS CRS_ID.
Expand All @@ -79,7 +80,7 @@ class CORE_EXPORT QgsCoordinateReferenceSystem

// Misc helper functions -----------------------

void createFromId( const long theId, CrsType theType = PostgisCrsId );
bool createFromId( const long theId, CrsType theType = PostgisCrsId );

/**
* \brief Set up this CRS from the given OGC CRS
Expand Down Expand Up @@ -161,6 +162,15 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
* @return bool TRUE if sucess else false
*/
bool createFromProj4( const QString theProjString );

/*! Set up this srs from a string definition, by default a WKT definition. Otherwise
* the string defines a authority, followed by a colon, followed by the definition.
* The authority can be one of "epsg", "postgis", "internal" for integer definitions,
* and "wkt" or "proj4" for string definitions. The implementation of each authority
* uses the corresponding createFrom... function.
* @param theDefinition A String containing a coordinate reference system definition.
*/
bool createFromString( const QString theDefinition );

/*! Find out whether this CRS is correctly initialised and usable */
bool isValid() const;
Expand Down
47 changes: 31 additions & 16 deletions src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp
Expand Up @@ -25,6 +25,7 @@
#include <QRegExp>
#include <QMessageBox>
#include <QTextStream>
#include <QUrl>
#include "qgslogger.h"

QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidget * parent, Qt::WFlags fl )
Expand Down Expand Up @@ -56,6 +57,13 @@ QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidg
delimiterRegexp->setChecked( true );
}

QString delimiterChars = settings.value( key + "/delimiterChars", " " ).toString();
cbxDelimSpace->setChecked( delimiterChars.contains(" "));
cbxDelimTab->setChecked( delimiterChars.contains("\\t"));
cbxDelimColon->setChecked( delimiterChars.contains(":"));
cbxDelimSemicolon->setChecked( delimiterChars.contains(":"));
cbxDelimComma->setChecked( delimiterChars.contains(","));

cmbXField->setDisabled( true );
cmbYField->setDisabled( true );
cmbWktField->setDisabled( true );
Expand Down Expand Up @@ -99,34 +107,33 @@ void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
else if ( delimiterRegexp->isChecked() )
delimiterType = "regexp";

QString uri = QString( "%1?delimiter=%2&delimiterType=%3" )
.arg( txtFilePath->text() )
.arg( txtDelimiter->text() )
.arg( delimiterType );
QUrl url(txtFilePath->text());
url.addQueryItem("delimiter",txtDelimiter->text());
url.addQueryItem("delimiterType",delimiterType);

if ( geomTypeXY->isChecked() )
{
if ( !cmbXField->currentText().isEmpty() && !cmbYField->currentText().isEmpty() )
{
uri += QString( "&xField=%1&yField=%2" )
.arg( cmbXField->currentText() )
.arg( cmbYField->currentText() );
url.addQueryItem("xField",cmbXField->currentText());
url.addQueryItem("yField",cmbYField->currentText());
}
}
else
{
if ( ! cmbWktField->currentText().isEmpty() )
{
uri += QString( "&wktField=%1" )
.arg( cmbWktField->currentText() );
url.addQueryItem("wktField",cmbWktField->currentText());
}
}

int skipLines = rowCounter->value();
if ( skipLines > 0 )
uri += QString( "&skipLines=%1" ).arg( skipLines );
url.addQueryItem("skipLines",QString( "%1" ).arg( skipLines ));

// add the layer to the map

QString uri(url.toEncoded());
emit drawVectorLayer( uri, txtLayerName->text(), "delimitedtext" );
// store the settings

Expand All @@ -138,10 +145,11 @@ void QgsDelimitedTextPluginGui::on_buttonBox_accepted()

if ( delimiterSelection->isChecked() )
settings.setValue( key + "/delimiterType", "selection" );
if ( delimiterPlain->isChecked() )
else if ( delimiterPlain->isChecked() )
settings.setValue( key + "/delimiterType", "plain" );
else
settings.setValue( key + "/delimiterType", "regexp" );
settings.setValue( key + "/delimiterChars", selectedChars());

accept();
}
Expand All @@ -156,6 +164,17 @@ void QgsDelimitedTextPluginGui::on_buttonBox_rejected()
reject();
}

QString QgsDelimitedTextPluginGui::selectedChars()
{
QString chars = "";
if ( cbxDelimSpace->isChecked() ) chars += " ";
if ( cbxDelimTab->isChecked() ) chars += "\\t";
if ( cbxDelimSemicolon->isChecked() ) chars += ";";
if ( cbxDelimComma->isChecked() ) chars += ",";
if ( cbxDelimColon->isChecked() ) chars += ":";
return chars;
}

QStringList QgsDelimitedTextPluginGui::splitLine( QString line )
{
QStringList fieldList;
Expand All @@ -171,11 +190,7 @@ QStringList QgsDelimitedTextPluginGui::splitLine( QString line )
else if ( delimiterSelection->isChecked() )
{
delimiter = "[";
if ( cbxDelimSpace->isChecked() ) delimiter += " ";
if ( cbxDelimTab->isChecked() ) delimiter += "\t";
if ( cbxDelimSemicolon->isChecked() ) delimiter += ";";
if ( cbxDelimComma->isChecked() ) delimiter += ",";
if ( cbxDelimColon->isChecked() ) delimiter += ":";
delimiter += selectedChars();
delimiter += "]";
txtDelimiter->setText( delimiter );
fieldList = line.split( QRegExp( delimiter ) );
Expand Down
1 change: 1 addition & 0 deletions src/plugins/delimited_text/qgsdelimitedtextplugingui.h
Expand Up @@ -37,6 +37,7 @@ class QgsDelimitedTextPluginGui : public QDialog, private Ui::QgsDelimitedTextPl
bool haveValidFileAndDelimiters();
void updateFieldLists();
void getOpenFileName();
QString selectedChars();

QgisInterface * qI;
QAbstractButton *pbnOK;
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui
Expand Up @@ -331,7 +331,7 @@
<string>Name of the field containing x values. Choose a field from the list. The list is generated by parsing the header row of the delimited text file.</string>
</property>
<property name="editable">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</item>
Expand Down Expand Up @@ -366,7 +366,7 @@
<string>Name of the field containing y values. Choose a field from the list. The list is generated by parsing the header row of the delimited text file.</string>
</property>
<property name="editable">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</item>
Expand Down Expand Up @@ -409,7 +409,7 @@
<string>Name of the field containing y values. Choose a field from the list. The list is generated by parsing the header row of the delimited text file.</string>
</property>
<property name="editable">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</item>
Expand Down

0 comments on commit dfb0602

Please sign in to comment.