-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgscustomprojectiondialog.cpp
529 lines (465 loc) · 18.5 KB
/
qgscustomprojectiondialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/***************************************************************************
qgscustomprojectiondialog.cpp
-------------------
begin : 2005
copyright : (C) 2005 by Tim Sutton
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscustomprojectiondialog.h"
//qgis includes
#include "qgis.h" //<--magick numbers
#include "qgisapp.h" //<--theme icons
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsprojectionselectiondialog.h"
#include "qgscrscache.h"
#include "qgssettings.h"
//qt includes
#include <QFileInfo>
#include <QMessageBox>
#include <QLocale>
//stdc++ includes
#include <fstream>
#include <sqlite3.h>
//proj4 includes
extern "C"
{
#include <proj_api.h>
}
QgsCustomProjectionDialog::QgsCustomProjectionDialog( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
QgsSettings settings;
restoreGeometry( settings.value( QStringLiteral( "Windows/CustomProjection/geometry" ) ).toByteArray() );
// user database is created at QGIS startup in QgisApp::createDB
// we just check whether there is our database [MD]
QFileInfo myFileInfo;
myFileInfo.setFile( QgsApplication::qgisSettingsDirPath() );
if ( !myFileInfo.exists() )
{
QgsDebugMsg( "The qgis.db does not exist" );
}
populateList();
if ( !customCRSnames.empty() )
{
leName->setText( customCRSnames[0] );
teParameters->setPlainText( customCRSparameters[0] );
leNameList->setCurrentItem( leNameList->topLevelItem( 0 ) );
}
leNameList->hideColumn( QgisCrsIdColumn );
}
QgsCustomProjectionDialog::~QgsCustomProjectionDialog()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "Windows/CustomProjection/geometry" ), saveGeometry() );
}
void QgsCustomProjectionDialog::populateList()
{
//Setup connection to the existing custom CRS database:
sqlite3 *myDatabase = nullptr;
const char *myTail = nullptr;
sqlite3_stmt *myPreparedStatement = nullptr;
int myResult;
//check the db is available
myResult = sqlite3_open_v2( QgsApplication::qgisUserDatabaseFilePath().toUtf8().data(), &myDatabase, SQLITE_OPEN_READONLY, nullptr );
if ( myResult != SQLITE_OK )
{
QgsDebugMsg( QString( "Can't open database: %1" ).arg( sqlite3_errmsg( myDatabase ) ) );
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
Q_ASSERT( myResult == SQLITE_OK );
}
QString mySql = QStringLiteral( "select srs_id,description,parameters from tbl_srs" );
QgsDebugMsg( QString( "Query to populate existing list:%1" ).arg( mySql ) );
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
// XXX Need to free memory from the error msg if one is set
if ( myResult == SQLITE_OK )
{
QTreeWidgetItem *newItem = nullptr;
QString id, name, parameters;
QgsCoordinateReferenceSystem crs;
while ( sqlite3_step( myPreparedStatement ) == SQLITE_ROW )
{
id = QString::fromUtf8( ( char * ) sqlite3_column_text( myPreparedStatement, 0 ) );
name = QString::fromUtf8( ( char * ) sqlite3_column_text( myPreparedStatement, 1 ) );
parameters = QString::fromUtf8( ( char * ) sqlite3_column_text( myPreparedStatement, 2 ) );
crs.createFromProj4( parameters );
existingCRSnames[id] = name;
existingCRSparameters[id] = crs.toProj4();
newItem = new QTreeWidgetItem( leNameList, QStringList() );
newItem->setText( QgisCrsNameColumn, name );
newItem->setText( QgisCrsIdColumn, id );
newItem->setText( QgisCrsParametersColumn, crs.toProj4() );
}
}
else
{
QgsDebugMsg( QString( "Populate list query failed: %1" ).arg( mySql ) );
}
sqlite3_finalize( myPreparedStatement );
sqlite3_close( myDatabase );
leNameList->sortByColumn( QgisCrsNameColumn, Qt::AscendingOrder );
QTreeWidgetItemIterator it( leNameList );
while ( *it )
{
QString id = ( *it )->text( QgisCrsIdColumn );
customCRSids.push_back( id );
customCRSnames.push_back( existingCRSnames[id] );
customCRSparameters.push_back( existingCRSparameters[id] );
it++;
}
}
bool QgsCustomProjectionDialog::deleteCrs( const QString &id )
{
sqlite3 *myDatabase = nullptr;
const char *myTail = nullptr;
sqlite3_stmt *myPreparedStatement = nullptr;
int myResult;
QString mySql = "delete from tbl_srs where srs_id=" + quotedValue( id );
QgsDebugMsg( mySql );
//check the db is available
myResult = sqlite3_open( QgsApplication::qgisUserDatabaseFilePath().toUtf8(), &myDatabase );
if ( myResult != SQLITE_OK )
{
QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( sqlite3_errmsg( myDatabase ), QgsApplication::qgisUserDatabaseFilePath() ) );
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
Q_ASSERT( myResult == SQLITE_OK );
}
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
// XXX Need to free memory from the error msg if one is set
if ( myResult != SQLITE_OK || sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
{
QgsDebugMsg( QString( "failed to remove CRS from database in custom projection dialog: %1 [%2]" ).arg( mySql, sqlite3_errmsg( myDatabase ) ) );
}
sqlite3_close( myDatabase );
QgsCoordinateReferenceSystem::invalidateCache();
QgsCoordinateTransformCache::instance()->invalidateCrs( QStringLiteral( "USER:%1" ).arg( id ) );
return myResult == SQLITE_OK;
}
void QgsCustomProjectionDialog::insertProjection( const QString &myProjectionAcronym )
{
sqlite3 *myDatabase = nullptr;
sqlite3_stmt *myPreparedStatement = nullptr;
sqlite3 *srsDatabase = nullptr;
QString mySql;
const char *myTail = nullptr;
//check the db is available
int myResult = sqlite3_open( QgsApplication::qgisUserDatabaseFilePath().toUtf8(), &myDatabase );
if ( myResult != SQLITE_OK )
{
QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( sqlite3_errmsg( myDatabase ), QgsApplication::qgisUserDatabaseFilePath() ) );
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
Q_ASSERT( myResult == SQLITE_OK );
}
int srsResult = sqlite3_open( QgsApplication::srsDatabaseFilePath().toUtf8(), &srsDatabase );
if ( myResult != SQLITE_OK )
{
QgsDebugMsg( QString( "Can't open database %1 [%2]" ).arg( QgsApplication::srsDatabaseFilePath(), sqlite3_errmsg( srsDatabase ) ) );
}
else
{
// Set up the query to retrieve the projection information needed to populate the PROJECTION list
QString srsSql = "select acronym,name,notes,parameters from tbl_projection where acronym=" + quotedValue( myProjectionAcronym );
const char *srsTail = nullptr;
sqlite3_stmt *srsPreparedStatement = nullptr;
srsResult = sqlite3_prepare( srsDatabase, srsSql.toUtf8(), srsSql.length(), &srsPreparedStatement, &srsTail );
// XXX Need to free memory from the error msg if one is set
if ( srsResult == SQLITE_OK )
{
if ( sqlite3_step( srsPreparedStatement ) == SQLITE_ROW )
{
QgsDebugMsg( "Trying to insert projection" );
// We have the result from system srs.db. Now insert into user db.
mySql = "insert into tbl_projection(acronym,name,notes,parameters) values ("
+ quotedValue( QString::fromUtf8( ( char * )sqlite3_column_text( srsPreparedStatement, 0 ) ) )
+ ',' + quotedValue( QString::fromUtf8( ( char * )sqlite3_column_text( srsPreparedStatement, 1 ) ) )
+ ',' + quotedValue( QString::fromUtf8( ( char * )sqlite3_column_text( srsPreparedStatement, 2 ) ) )
+ ',' + quotedValue( QString::fromUtf8( ( char * )sqlite3_column_text( srsPreparedStatement, 3 ) ) )
+ ')'
;
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.length(), &myPreparedStatement, &myTail );
if ( myResult != SQLITE_OK || sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
{
QgsDebugMsg( QString( "Update or insert failed in custom projection dialog: %1 [%2]" ).arg( mySql, sqlite3_errmsg( myDatabase ) ) );
}
sqlite3_finalize( myPreparedStatement );
}
sqlite3_finalize( srsPreparedStatement );
}
else
{
QgsDebugMsg( QString( "prepare failed: %1 [%2]" ).arg( srsSql, sqlite3_errmsg( srsDatabase ) ) );
}
sqlite3_close( srsDatabase );
}
// close sqlite3 db
sqlite3_close( myDatabase );
}
bool QgsCustomProjectionDialog::saveCrs( QgsCoordinateReferenceSystem myCRS, const QString &myName, QString myId, bool newEntry )
{
QString mySql;
int return_id;
QString myProjectionAcronym = myCRS.projectionAcronym();
QString myEllipsoidAcronym = myCRS.ellipsoidAcronym();
QgsDebugMsg( QString( "Saving a CRS:%1, %2, %3" ).arg( myName, myCRS.toProj4() ).arg( newEntry ) );
if ( newEntry )
{
return_id = myCRS.saveAsUserCrs( myName );
if ( return_id == -1 )
return false;
else
myId = QString::number( return_id );
}
else
{
mySql = "update tbl_srs set description="
+ quotedValue( myName )
+ ",projection_acronym=" + quotedValue( myProjectionAcronym )
+ ",ellipsoid_acronym=" + quotedValue( myEllipsoidAcronym )
+ ",parameters=" + quotedValue( myCRS.toProj4() )
+ ",is_geo=0" // <--shamelessly hard coded for now
+ " where srs_id=" + quotedValue( myId )
;
QgsDebugMsg( mySql );
sqlite3 *myDatabase = nullptr;
const char *myTail = nullptr;
sqlite3_stmt *myPreparedStatement = nullptr;
int myResult;
//check if the db is available
myResult = sqlite3_open( QgsApplication::qgisUserDatabaseFilePath().toUtf8(), &myDatabase );
if ( myResult != SQLITE_OK )
{
QgsDebugMsg( QString( "Can't open database: %1 \n please notify QGIS developers of this error \n %2 (file name) " ).arg( sqlite3_errmsg( myDatabase ), QgsApplication::qgisUserDatabaseFilePath() ) );
// XXX This will likely never happen since on open, sqlite creates the
// database if it does not exist.
Q_ASSERT( myResult == SQLITE_OK );
}
myResult = sqlite3_prepare( myDatabase, mySql.toUtf8(), mySql.toUtf8().length(), &myPreparedStatement, &myTail );
// XXX Need to free memory from the error msg if one is set
if ( myResult != SQLITE_OK || sqlite3_step( myPreparedStatement ) != SQLITE_DONE )
{
QgsDebugMsg( QString( "failed to write to database in custom projection dialog: %1 [%2]" ).arg( mySql, sqlite3_errmsg( myDatabase ) ) );
}
sqlite3_finalize( myPreparedStatement );
// close sqlite3 db
sqlite3_close( myDatabase );
if ( myResult != SQLITE_OK )
return false;
}
existingCRSparameters[myId] = myCRS.toProj4();
existingCRSnames[myId] = myName;
QgsCoordinateReferenceSystem::invalidateCache();
QgsCoordinateTransformCache::instance()->invalidateCrs( QStringLiteral( "USER:%1" ).arg( myId ) );
// If we have a projection acronym not in the user db previously, add it.
// This is a must, or else we can't select it from the vw_srs table.
// Actually, add it always and let the SQL PRIMARY KEY remove duplicates.
insertProjection( myProjectionAcronym );
return true;
}
void QgsCustomProjectionDialog::on_pbnAdd_clicked()
{
QString name = tr( "new CRS" );
QString id = QLatin1String( "" );
QgsCoordinateReferenceSystem parameters;
QTreeWidgetItem *newItem = new QTreeWidgetItem( leNameList, QStringList() );
newItem->setText( QgisCrsNameColumn, name );
newItem->setText( QgisCrsIdColumn, id );
newItem->setText( QgisCrsParametersColumn, parameters.toProj4() );
customCRSnames.push_back( name );
customCRSids.push_back( id );
customCRSparameters.push_back( parameters.toProj4() );
leNameList->setCurrentItem( newItem );
}
void QgsCustomProjectionDialog::on_pbnRemove_clicked()
{
int i = leNameList->currentIndex().row();
if ( i == -1 )
{
return;
}
QTreeWidgetItem *item = leNameList->takeTopLevelItem( i );
delete item;
if ( customCRSids[i] != QLatin1String( "" ) )
{
deletedCRSs.push_back( customCRSids[i] );
}
customCRSids.erase( customCRSids.begin() + i );
customCRSnames.erase( customCRSnames.begin() + i );
customCRSparameters.erase( customCRSparameters.begin() + i );
}
void QgsCustomProjectionDialog::on_leNameList_currentItemChanged( QTreeWidgetItem *current, QTreeWidgetItem *previous )
{
//Store the modifications made to the current element before moving on
int currentIndex, previousIndex;
if ( previous )
{
previousIndex = leNameList->indexOfTopLevelItem( previous );
customCRSnames[previousIndex] = leName->text();
customCRSparameters[previousIndex] = teParameters->toPlainText();
previous->setText( QgisCrsNameColumn, leName->text() );
previous->setText( QgisCrsParametersColumn, teParameters->toPlainText() );
}
if ( current )
{
currentIndex = leNameList->indexOfTopLevelItem( current );
leName->setText( customCRSnames[currentIndex] );
teParameters->setPlainText( current->text( QgisCrsParametersColumn ) );
}
else
{
//Can happen that current is null, for example if we just deleted the last element
leName->setText( QLatin1String( "" ) );
teParameters->setPlainText( QLatin1String( "" ) );
return;
}
return;
}
void QgsCustomProjectionDialog::on_pbnCopyCRS_clicked()
{
QgsProjectionSelectionDialog *mySelector = new QgsProjectionSelectionDialog( this );
if ( mySelector->exec() )
{
QgsCoordinateReferenceSystem srs = mySelector->crs();
if ( leNameList->topLevelItemCount() == 0 )
{
on_pbnAdd_clicked();
}
teParameters->setPlainText( srs.toProj4() );
customCRSparameters[leNameList->currentIndex().row()] = srs.toProj4();
leNameList->currentItem()->setText( QgisCrsParametersColumn, srs.toProj4() );
}
delete mySelector;
}
void QgsCustomProjectionDialog::on_buttonBox_accepted()
{
//Update the current CRS:
int i = leNameList->currentIndex().row();
if ( i != -1 )
{
customCRSnames[i] = leName->text();
customCRSparameters[i] = teParameters->toPlainText();
}
QgsDebugMsg( "We save the modified CRS." );
//Check if all CRS are valid:
QgsCoordinateReferenceSystem CRS;
for ( int i = 0; i < customCRSids.size(); ++i )
{
CRS.createFromProj4( customCRSparameters[i] );
if ( !CRS.isValid() )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "The proj4 definition of '%1' is not valid." ).arg( customCRSnames[i] ) );
return;
}
}
//Modify the CRS changed:
bool save_success = true;
for ( int i = 0; i < customCRSids.size(); ++i )
{
CRS.createFromProj4( customCRSparameters[i] );
//Test if we just added this CRS (if it has no existing ID)
if ( customCRSids[i] == QLatin1String( "" ) )
{
save_success &= saveCrs( CRS, customCRSnames[i], QLatin1String( "" ), true );
}
else
{
if ( existingCRSnames[customCRSids[i]] != customCRSnames[i] || existingCRSparameters[customCRSids[i]] != customCRSparameters[i] )
{
save_success &= saveCrs( CRS, customCRSnames[i], customCRSids[i], false );
}
}
if ( ! save_success )
{
QgsDebugMsg( QString( "Error when saving CRS '%1'" ).arg( customCRSnames[i] ) );
}
}
QgsDebugMsg( "We remove the deleted CRS." );
for ( int i = 0; i < deletedCRSs.size(); ++i )
{
save_success &= deleteCrs( deletedCRSs[i] );
if ( ! save_success )
{
QgsDebugMsg( QString( "Problem for layer '%1'" ).arg( customCRSparameters[i] ) );
}
}
if ( save_success )
{
accept();
}
}
void QgsCustomProjectionDialog::on_pbnCalculate_clicked()
{
//
// We must check the prj def is valid!
//
projPJ myProj = pj_init_plus( teParameters->toPlainText().toLocal8Bit().data() );
QgsDebugMsg( QString( "My proj: %1" ).arg( teParameters->toPlainText() ) );
if ( !myProj )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "This proj4 projection definition is not valid." ) );
projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) );
pj_free( myProj );
return;
}
// Get the WGS84 coordinates
bool okN, okE;
double northing = northWGS84->text().toDouble( &okN ) * DEG_TO_RAD;
double easthing = eastWGS84->text().toDouble( &okE ) * DEG_TO_RAD;
if ( !okN || !okE )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "Northing and Easthing must be in decimal form." ) );
projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) );
pj_free( myProj );
return;
}
projPJ wgs84Proj = pj_init_plus( GEOPROJ4.toLocal8Bit().data() ); //defined in qgis.h
if ( !wgs84Proj )
{
QMessageBox::information( this, tr( "QGIS Custom Projection" ),
tr( "Internal Error (source projection invalid?)" ) );
projectedX->setText( QLatin1String( "" ) );
projectedY->setText( QLatin1String( "" ) );
pj_free( wgs84Proj );
return;
}
double z = 0.0;
int projResult = pj_transform( wgs84Proj, myProj, 1, 0, &easthing, &northing, &z );
if ( projResult != 0 )
{
projectedX->setText( tr( "Error" ) );
projectedY->setText( tr( "Error" ) );
QgsDebugMsg( pj_strerrno( projResult ) );
}
else
{
QString tmp;
tmp = QLocale::system().toString( northing, 'f', 4 );
projectedX->setText( tmp );
tmp = QLocale::system().toString( easthing, 'f', 4 );
projectedY->setText( tmp );
}
//
pj_free( myProj );
pj_free( wgs84Proj );
}
QString QgsCustomProjectionDialog::quotedValue( QString value )
{
value.replace( '\'', QLatin1String( "''" ) );
return value.prepend( '\'' ).append( '\'' );
}