|
21 | 21 |
|
22 | 22 | #include "qgsnewspatialitelayerdialog.h"
|
23 | 23 |
|
24 |
| -#include "qgsspatialitesridsdialog.h" |
| 24 | +#include "qgis.h" |
25 | 25 | #include "qgsapplication.h"
|
26 | 26 | #include "qgisapp.h" // <- for theme icons
|
27 | 27 | #include <qgsvectorlayer.h>
|
28 | 28 | #include <qgsmaplayerregistry.h>
|
| 29 | +#include "qgscoordinatereferencesystem.h" |
| 30 | +#include "qgsgenericprojectionselector.h" |
29 | 31 |
|
30 | 32 | #include "qgslogger.h"
|
31 | 33 |
|
@@ -71,7 +73,12 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
|
71 | 73 | buttonBox->button( QDialogButtonBox::Ok )->setDefault( true );
|
72 | 74 |
|
73 | 75 | // 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 | + |
75 | 82 | pbnFindSRID->setEnabled( mDatabaseComboBox->count() );
|
76 | 83 | }
|
77 | 84 |
|
@@ -174,17 +181,67 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked()
|
174 | 181 |
|
175 | 182 | void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked()
|
176 | 183 | {
|
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 ) |
180 | 208 | {
|
181 |
| - if ( sridDlg->exec() ) |
| 209 | + // get the first row of the result set |
| 210 | + while ( sqlite3_step( ppStmt ) == SQLITE_ROW ) |
182 | 211 | {
|
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 ) ) ); |
186 | 214 | }
|
187 | 215 | }
|
| 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; |
188 | 245 | }
|
189 | 246 |
|
190 | 247 | void QgsNewSpatialiteLayerDialog::initializeSpatialMetadata( sqlite3 *sqlite_handle )
|
@@ -348,7 +405,7 @@ bool QgsNewSpatialiteLayerDialog::apply()
|
348 | 405 | QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" )
|
349 | 406 | .arg( quotedValue( leLayerName->text() ) )
|
350 | 407 | .arg( quotedValue( leGeometryColumn->text() ) )
|
351 |
| - .arg( leSRID->text().toInt() ) |
| 408 | + .arg( mCrsId ) |
352 | 409 | .arg( quotedValue( selectedType() ) );
|
353 | 410 | QgsDebugMsg( sqlAddGeom ); // OK
|
354 | 411 |
|
|
0 commit comments