Skip to content

Commit dfb0602

Browse files
author
gsherman
committed
Patch for delimited text, ticket #3414. Submitted by ccrook.
git-svn-id: http://svn.osgeo.org/qgis/trunk@15074 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent d80c90d commit dfb0602

10 files changed

+291
-76
lines changed

python/core/qgscoordinatereferencesystem.sip

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ class QgsCoordinateReferenceSystem
2323

2424
~QgsCoordinateReferenceSystem();
2525

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

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

3940
// Misc helper functions -----------------------
4041

41-
void createFromId(const long theId, CrsType theType=PostgisCrsId);
42+
bool createFromId(const long theId, CrsType theType=PostgisCrsId);
4243

4344
/**
4445
* \brief Set up this CRS from the given OGC CRS
@@ -70,6 +71,15 @@ class QgsCoordinateReferenceSystem
7071
* @return bool TRUE if sucess else false
7172
*/
7273
bool createFromWkt(const QString theWkt);
74+
75+
/*! Set up this srs from a string definition, by default a WKT definition. Otherwise
76+
* the string defines a authority, followed by a colon, followed by the definition.
77+
* The authority can be one of "epsg", "postgis", "internal" for integer definitions,
78+
* and "wkt" or "proj4" for string definitions. The implementation of each authority
79+
* uses the corresponding createFrom... function.
80+
* @param theDefinition A String containing a coordinate reference system definition.
81+
*/
82+
bool createFromString( const QString theDefinition );
7383

7484
/*! Set up this srs by fetching the appropriate information from the
7585
* sqlite backend. First the system level read only srs.db will be checked

src/core/qgscoordinatereferencesystem.cpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem()
5151
mCRS = OSRNewSpatialReference( NULL );
5252
}
5353

54-
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( QString theWkt )
54+
QgsCoordinateReferenceSystem::QgsCoordinateReferenceSystem( QString theDefinition )
5555
: mMapUnits( QGis::UnknownUnit )
5656
, mIsValidFlag( 0 )
5757
, mValidationHint( "" )
5858
{
5959
mCRS = OSRNewSpatialReference( NULL );
60-
createFromWkt( theWkt );
60+
createFromString( theDefinition );
6161
}
6262

6363

@@ -75,23 +75,56 @@ QgsCoordinateReferenceSystem::~QgsCoordinateReferenceSystem()
7575
OSRDestroySpatialReference( mCRS );
7676
}
7777

78-
void QgsCoordinateReferenceSystem::createFromId( const long theId, CrsType theType )
78+
bool QgsCoordinateReferenceSystem::createFromId( const long theId, CrsType theType )
7979
{
80+
bool result = false;
8081
switch ( theType )
8182
{
8283
case InternalCrsId:
83-
createFromSrsId( theId );
84+
result = createFromSrsId( theId );
8485
break;
8586
case PostgisCrsId:
86-
createFromSrid( theId );
87+
result = createFromSrid( theId );
8788
break;
8889
case EpsgCrsId:
89-
createFromEpsg( theId );
90+
result = createFromEpsg( theId );
9091
break;
9192
default:
9293
//THIS IS BAD...THIS PART OF CODE SHOULD NEVER BE REACHED...
9394
QgsDebugMsg( "Unexpected case reached!" );
9495
};
96+
return result;
97+
}
98+
99+
bool QgsCoordinateReferenceSystem::createFromString( const QString theDefinition )
100+
{
101+
bool result = false;
102+
QRegExp reCrsId("^(epsg|postgis|internal)\\:(\\d+)$",Qt::CaseInsensitive);
103+
if( reCrsId.indexIn(theDefinition) == 0)
104+
{
105+
QString authName = reCrsId.cap(1).toLower();
106+
CrsType type = InternalCrsId;
107+
if( authName == "epsg" ) type = EpsgCrsId;
108+
if( authName == "postgis" ) type = PostgisCrsId;
109+
long id = reCrsId.cap(2).toLong();
110+
result = createFromId(id,type);
111+
}
112+
else
113+
{
114+
QRegExp reCrsStr("^(?:(wkt|proj4)\\:)?(.+)$",Qt::CaseInsensitive);
115+
if( reCrsStr.indexIn(theDefinition) == 0 )
116+
{
117+
if( reCrsStr.cap(1).toLower() == "proj4" )
118+
{
119+
result = createFromProj4(reCrsStr.cap(2));
120+
}
121+
else
122+
{
123+
result = createFromWkt(reCrsStr.cap(2));
124+
}
125+
}
126+
}
127+
return result;
95128
}
96129

97130
bool QgsCoordinateReferenceSystem::createFromOgcWmsCrs( QString theCrs )

src/core/qgscoordinatereferencesystem.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
5757
~QgsCoordinateReferenceSystem();
5858

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

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

8081
// Misc helper functions -----------------------
8182

82-
void createFromId( const long theId, CrsType theType = PostgisCrsId );
83+
bool createFromId( const long theId, CrsType theType = PostgisCrsId );
8384

8485
/**
8586
* \brief Set up this CRS from the given OGC CRS
@@ -161,6 +162,15 @@ class CORE_EXPORT QgsCoordinateReferenceSystem
161162
* @return bool TRUE if sucess else false
162163
*/
163164
bool createFromProj4( const QString theProjString );
165+
166+
/*! Set up this srs from a string definition, by default a WKT definition. Otherwise
167+
* the string defines a authority, followed by a colon, followed by the definition.
168+
* The authority can be one of "epsg", "postgis", "internal" for integer definitions,
169+
* and "wkt" or "proj4" for string definitions. The implementation of each authority
170+
* uses the corresponding createFrom... function.
171+
* @param theDefinition A String containing a coordinate reference system definition.
172+
*/
173+
bool createFromString( const QString theDefinition );
164174

165175
/*! Find out whether this CRS is correctly initialised and usable */
166176
bool isValid() const;

src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <QRegExp>
2626
#include <QMessageBox>
2727
#include <QTextStream>
28+
#include <QUrl>
2829
#include "qgslogger.h"
2930

3031
QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidget * parent, Qt::WFlags fl )
@@ -56,6 +57,13 @@ QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidg
5657
delimiterRegexp->setChecked( true );
5758
}
5859

60+
QString delimiterChars = settings.value( key + "/delimiterChars", " " ).toString();
61+
cbxDelimSpace->setChecked( delimiterChars.contains(" "));
62+
cbxDelimTab->setChecked( delimiterChars.contains("\\t"));
63+
cbxDelimColon->setChecked( delimiterChars.contains(":"));
64+
cbxDelimSemicolon->setChecked( delimiterChars.contains(":"));
65+
cbxDelimComma->setChecked( delimiterChars.contains(","));
66+
5967
cmbXField->setDisabled( true );
6068
cmbYField->setDisabled( true );
6169
cmbWktField->setDisabled( true );
@@ -99,34 +107,33 @@ void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
99107
else if ( delimiterRegexp->isChecked() )
100108
delimiterType = "regexp";
101109

102-
QString uri = QString( "%1?delimiter=%2&delimiterType=%3" )
103-
.arg( txtFilePath->text() )
104-
.arg( txtDelimiter->text() )
105-
.arg( delimiterType );
110+
QUrl url(txtFilePath->text());
111+
url.addQueryItem("delimiter",txtDelimiter->text());
112+
url.addQueryItem("delimiterType",delimiterType);
106113

107114
if ( geomTypeXY->isChecked() )
108115
{
109116
if ( !cmbXField->currentText().isEmpty() && !cmbYField->currentText().isEmpty() )
110117
{
111-
uri += QString( "&xField=%1&yField=%2" )
112-
.arg( cmbXField->currentText() )
113-
.arg( cmbYField->currentText() );
118+
url.addQueryItem("xField",cmbXField->currentText());
119+
url.addQueryItem("yField",cmbYField->currentText());
114120
}
115121
}
116122
else
117123
{
118124
if ( ! cmbWktField->currentText().isEmpty() )
119125
{
120-
uri += QString( "&wktField=%1" )
121-
.arg( cmbWktField->currentText() );
126+
url.addQueryItem("wktField",cmbWktField->currentText());
122127
}
123128
}
124129

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

129134
// add the layer to the map
135+
136+
QString uri(url.toEncoded());
130137
emit drawVectorLayer( uri, txtLayerName->text(), "delimitedtext" );
131138
// store the settings
132139

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

139146
if ( delimiterSelection->isChecked() )
140147
settings.setValue( key + "/delimiterType", "selection" );
141-
if ( delimiterPlain->isChecked() )
148+
else if ( delimiterPlain->isChecked() )
142149
settings.setValue( key + "/delimiterType", "plain" );
143150
else
144151
settings.setValue( key + "/delimiterType", "regexp" );
152+
settings.setValue( key + "/delimiterChars", selectedChars());
145153

146154
accept();
147155
}
@@ -156,6 +164,17 @@ void QgsDelimitedTextPluginGui::on_buttonBox_rejected()
156164
reject();
157165
}
158166

167+
QString QgsDelimitedTextPluginGui::selectedChars()
168+
{
169+
QString chars = "";
170+
if ( cbxDelimSpace->isChecked() ) chars += " ";
171+
if ( cbxDelimTab->isChecked() ) chars += "\\t";
172+
if ( cbxDelimSemicolon->isChecked() ) chars += ";";
173+
if ( cbxDelimComma->isChecked() ) chars += ",";
174+
if ( cbxDelimColon->isChecked() ) chars += ":";
175+
return chars;
176+
}
177+
159178
QStringList QgsDelimitedTextPluginGui::splitLine( QString line )
160179
{
161180
QStringList fieldList;
@@ -171,11 +190,7 @@ QStringList QgsDelimitedTextPluginGui::splitLine( QString line )
171190
else if ( delimiterSelection->isChecked() )
172191
{
173192
delimiter = "[";
174-
if ( cbxDelimSpace->isChecked() ) delimiter += " ";
175-
if ( cbxDelimTab->isChecked() ) delimiter += "\t";
176-
if ( cbxDelimSemicolon->isChecked() ) delimiter += ";";
177-
if ( cbxDelimComma->isChecked() ) delimiter += ",";
178-
if ( cbxDelimColon->isChecked() ) delimiter += ":";
193+
delimiter += selectedChars();
179194
delimiter += "]";
180195
txtDelimiter->setText( delimiter );
181196
fieldList = line.split( QRegExp( delimiter ) );

src/plugins/delimited_text/qgsdelimitedtextplugingui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class QgsDelimitedTextPluginGui : public QDialog, private Ui::QgsDelimitedTextPl
3737
bool haveValidFileAndDelimiters();
3838
void updateFieldLists();
3939
void getOpenFileName();
40+
QString selectedChars();
4041

4142
QgisInterface * qI;
4243
QAbstractButton *pbnOK;

src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
<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>
332332
</property>
333333
<property name="editable">
334-
<bool>true</bool>
334+
<bool>false</bool>
335335
</property>
336336
</widget>
337337
</item>
@@ -366,7 +366,7 @@
366366
<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>
367367
</property>
368368
<property name="editable">
369-
<bool>true</bool>
369+
<bool>false</bool>
370370
</property>
371371
</widget>
372372
</item>
@@ -409,7 +409,7 @@
409409
<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>
410410
</property>
411411
<property name="editable">
412-
<bool>true</bool>
412+
<bool>false</bool>
413413
</property>
414414
</widget>
415415
</item>

0 commit comments

Comments
 (0)