|
20 | 20 |
|
21 | 21 | #include "qgsnewspatialitelayerdialog.h" |
22 | 22 |
|
23 | | -#include "qgsspatialitesridsdialog.h" |
| 23 | +#include "qgis.h" |
24 | 24 | #include "qgsapplication.h" |
25 | 25 | #include "qgsproviderregistry.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 |
|
@@ -72,7 +74,12 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W |
72 | 74 | buttonBox->button( QDialogButtonBox::Ok )->setDefault( true ); |
73 | 75 |
|
74 | 76 | // 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 | + |
76 | 83 | pbnFindSRID->setEnabled( mDatabaseComboBox->count() ); |
77 | 84 | } |
78 | 85 |
|
@@ -176,17 +183,67 @@ void QgsNewSpatialiteLayerDialog::on_mRemoveAttributeButton_clicked() |
176 | 183 |
|
177 | 184 | void QgsNewSpatialiteLayerDialog::on_pbnFindSRID_clicked() |
178 | 185 | { |
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 ) |
182 | 210 | { |
183 | | - if ( sridDlg->exec() ) |
| 211 | + // get the first row of the result set |
| 212 | + while ( sqlite3_step( ppStmt ) == SQLITE_ROW ) |
184 | 213 | { |
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 ) ) ); |
188 | 216 | } |
189 | 217 | } |
| 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; |
190 | 247 | } |
191 | 248 |
|
192 | 249 | bool QgsNewSpatialiteLayerDialog::createDb() |
@@ -293,7 +350,7 @@ bool QgsNewSpatialiteLayerDialog::apply() |
293 | 350 | QString sqlAddGeom = QString( "select AddGeometryColumn(%1,%2,%3,%4,2)" ) |
294 | 351 | .arg( quotedValue( leLayerName->text() ) ) |
295 | 352 | .arg( quotedValue( leGeometryColumn->text() ) ) |
296 | | - .arg( leSRID->text().toInt() ) |
| 353 | + .arg( mCrsId ) |
297 | 354 | .arg( quotedValue( selectedType() ) ); |
298 | 355 | QgsDebugMsg( sqlAddGeom ); // OK |
299 | 356 |
|
|
0 commit comments