Skip to content

Commit c1eea90

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

6 files changed

+71
-200
lines changed

src/app/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,11 @@ IF (HAVE_SPATIALITE)
304304
SET (QGIS_APP_SRCS ${QGIS_APP_SRCS}
305305
spatialite/qgsspatialitesourceselect.cpp
306306
spatialite/qgsnewspatialitelayerdialog.cpp
307-
spatialite/qgsspatialitesridsdialog.cpp
308307
spatialite/qgsspatialitetablemodel.cpp
309308
)
310309
SET (QGIS_APP_MOC_HDRS ${QGIS_APP_MOC_HDRS}
311310
spatialite/qgsspatialitesourceselect.h
312311
spatialite/qgsnewspatialitelayerdialog.h
313-
spatialite/qgsspatialitesridsdialog.h
314312
spatialite/qgsspatialitetablemodel.h
315313
)
316314
ENDIF (HAVE_SPATIALITE)

src/app/spatialite/qgsnewspatialitelayerdialog.cpp

+67-10
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121

2222
#include "qgsnewspatialitelayerdialog.h"
2323

24-
#include "qgsspatialitesridsdialog.h"
24+
#include "qgis.h"
2525
#include "qgsapplication.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

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

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

@@ -174,17 +181,67 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
174181

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

190247
void QgsNewSpatialiteLayerDialog::initializeSpatialMetadata( sqlite3 *sqlite_handle )
@@ -348,7 +405,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
348405
QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
349406
.arg( quotedValue( leLayerName->text() ) )
350407
.arg( quotedValue( leGeometryColumn->text() ) )
351-
.arg( leSRID->text().toInt() )
408+
.arg( mCrsId )
352409
.arg( quotedValue( selectedType() ) );
353410
QgsDebugMsg( sqlAddGeom ); // OK
354411

src/app/spatialite/qgsnewspatialitelayerdialog.h

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNewSpatialiteL
6464

6565
static QString quotedIdentifier( QString id );
6666
static QString quotedValue( QString value );
67+
68+
int mCrsId;
6769
};
6870

6971
#endif // QGSNEWVECTORLAYERDIALOG_H

src/app/spatialite/qgsspatialitesridsdialog.cpp

-140
This file was deleted.

src/app/spatialite/qgsspatialitesridsdialog.h

-46
This file was deleted.

src/ui/qgsnewspatialitelayerdialogbase.ui

+2-2
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)