Skip to content

Commit 270655d

Browse files
committed
[BACKPORT] use native projection selector in New SpatiaLite layer
dialog (completely fix #4549)
1 parent 4d761d0 commit 270655d

File tree

6 files changed

+72
-199
lines changed

6 files changed

+72
-199
lines changed

src/app/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ SET(QGIS_APP_SRCS
139139
)
140140

141141
IF (ANDROID)
142-
SET(QGIS_APP_SRCS
142+
SET(QGIS_APP_SRCS
143143
${QGIS_APP_SRCS}
144144
qtmain_android.cpp
145145
)
@@ -331,11 +331,9 @@ ENDIF (POSTGRES_FOUND)
331331
IF (HAVE_SPATIALITE)
332332
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
333333
spatialite/qgsnewspatialitelayerdialog.cpp
334-
spatialite/qgsspatialitesridsdialog.cpp
335334
)
336335
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
337336
spatialite/qgsnewspatialitelayerdialog.h
338-
spatialite/qgsspatialitesridsdialog.h
339337
)
340338
ENDIF (HAVE_SPATIALITE)
341339

src/app/spatialite/qgsnewspatialitelayerdialog.cpp

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
#include "qgsnewspatialitelayerdialog.h"
2222

23-
#include "qgsspatialitesridsdialog.h"
23+
#include "qgis.h"
2424
#include "qgsapplication.h"
2525
#include "qgsproviderregistry.h"
2626
#include "qgisapp.h" // <- for theme icons
2727
#include <qgsvectorlayer.h>
2828
#include <qgsmaplayerregistry.h>
29+
#include "qgscoordinatereferencesystem.h"
30+
#include "qgsgenericprojectionselector.h"
2931

3032
#include "qgslogger.h"
3133

@@ -72,7 +74,12 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
7274
buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
7375

7476
// Set the SRID box to a default of WGS84
75-
leSRID->setText( "4326" );
77+
QgsCoordinateReferenceSystem srs;
78+
srs.createFromOgcWmsCrs( GEO_EPSG_CRS_AUTHID );
79+
srs.validate();
80+
mCrsId = srs.srsid();
81+
leSRID->setText( srs.authid() + " - " + srs.description() );
82+
7683
pbnFindSRID->setEnabled( mDatabaseComboBox->count() );
7784
}
7885

@@ -176,17 +183,67 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
176183

177184
void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
178185
{
179-
// set the SRID from a list returned from the selected Spatialite database
180-
QgsSpatialiteSridsDialog *sridDlg = new QgsSpatialiteSridsDialog( this );
181-
if ( sridDlg->load( mDatabaseComboBox->currentText() ) )
186+
// first get list of supported SRID from the selected Spatialite database
187+
// to build filter for projection selector
188+
sqlite3 *db = 0;
189+
bool status = true;
190+
if ( !db )
191+
{
192+
int rc = sqlite3_open_v2( mDatabaseComboBox->currentText().toUtf8(), &db, SQLITE_OPEN_READONLY, NULL );
193+
if ( rc != SQLITE_OK )
194+
{
195+
QMessageBox::warning( this, tr( "SpatiaLite Database" ), tr( "Unable to open the database" ) );
196+
return;
197+
}
198+
}
199+
200+
// load up the srid table
201+
const char *pzTail;
202+
sqlite3_stmt *ppStmt;
203+
QString sql = "select auth_srid, auth_name, ref_sys_name from spatial_ref_sys order by srid asc";
204+
205+
QSet<QString> myCRSs;
206+
207+
int rc = sqlite3_prepare( db, sql.toUtf8(), sql.toUtf8().length(), &ppStmt, &pzTail );
208+
// XXX Need to free memory from the error msg if one is set
209+
if ( rc == SQLITE_OK )
182210
{
183-
if ( sridDlg->exec() )
211+
// get the first row of the result set
212+
while ( sqlite3_step( ppStmt ) == SQLITE_ROW )
184213
{
185-
// set the srid field to the selection
186-
leSRID->setText( sridDlg->selectedSrid() );
187-
sridDlg->accept();
214+
myCRSs.insert( QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 1 ) ) +
215+
":" + QString::fromUtf8(( const char * )sqlite3_column_text( ppStmt, 0 ) ) );
188216
}
189217
}
218+
else
219+
{
220+
// XXX query failed -- warn the user some how
221+
QMessageBox::warning( 0, tr( "Error" ), tr( "Failed to load SRIDS: %1" ).arg( sqlite3_errmsg( db ) ) );
222+
status = false;
223+
}
224+
// close the statement
225+
sqlite3_finalize( ppStmt );
226+
sqlite3_close( db );
227+
228+
if ( !status )
229+
{
230+
return;
231+
}
232+
233+
// prepare projection selector
234+
QgsGenericProjectionSelector *mySelector = new QgsGenericProjectionSelector( this );
235+
mySelector->setMessage();
236+
mySelector->setOgcWmsCrsFilter( myCRSs );
237+
238+
if ( mySelector->exec() )
239+
{
240+
QgsCoordinateReferenceSystem srs;
241+
srs.createFromOgcWmsCrs( mySelector->selectedAuthId() );
242+
bool ok;
243+
mCrsId = srs.authid().split( ':' ).at( 1 ).toInt( &ok );
244+
leSRID->setText( srs.authid() + " - " + srs.description() );
245+
}
246+
delete mySelector;
190247
}
191248

192249
bool QgsNewSpatialiteLayerDialog::createDb()
@@ -293,7 +350,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
293350
QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
294351
.arg( quotedValue( leLayerName->text() ) )
295352
.arg( quotedValue( leGeometryColumn->text() ) )
296-
.arg( leSRID->text().toInt() )
353+
.arg( mCrsId )
297354
.arg( quotedValue( selectedType() ) );
298355
QgsDebugMsg( sqlAddGeom ); // OK
299356

src/app/spatialite/qgsnewspatialitelayerdialog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
6060

6161
static QString quotedIdentifier( QString id );
6262
static QString quotedValue( QString value );
63+
64+
int mCrsId;
6365
};
6466

6567
#endif // QGSNEWVECTORLAYERDIALOG_H

src/app/spatialite/qgsspatialitesridsdialog.cpp

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/app/spatialite/qgsspatialitesridsdialog.h

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/ui/qgsnewspatialitelayerdialogbase.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>430</width>
9+
<width>450</width>
1010
<height>627</height>
1111
</rect>
1212
</property>
@@ -216,7 +216,7 @@
216216
<string>Specify the coordinate reference system of the layer's geometry.</string>
217217
</property>
218218
<property name="text">
219-
<string>Find SRID</string>
219+
<string>Specify CRS</string>
220220
</property>
221221
</widget>
222222
</item>

0 commit comments

Comments
 (0)