/
qgsspatialrefsys.cpp
1291 lines (1122 loc) · 39.8 KB
/
qgsspatialrefsys.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "qgsspatialrefsys.h"
#include <cassert>
#include <cmath>
#include <iostream>
#include <sqlite3.h>
#include <QSettings>
#include <QRegExp>
#include <QFileInfo>
#include <QDir>
#include <projects.h>
#include <QDomNode>
#include <QDomElement>
#include <QMessageBox>
#include <qgsapplication.h>
#include <qgslayerprojectionselector.h>
#include "qgslogger.h"
#include <qgsproject.h>
#include <qgis.h> //const vals declared here
//gdal and ogr includes (needed for == operator)
#include <ogr_api.h>
#include <ogr_spatialref.h>
#include <cpl_error.h>
//--------------------------
QgsSpatialRefSys::QgsSpatialRefSys()
: mMapUnits(QGis::UNKNOWN),
mIsValidFlag(0)
{
// NOOP
}
QgsSpatialRefSys::QgsSpatialRefSys(QString theWkt)
: mMapUnits(QGis::UNKNOWN),
mIsValidFlag(0)
{
createFromWkt(theWkt);
}
QgsSpatialRefSys::QgsSpatialRefSys(long theSrsId,
QString theDescription,
QString theProjectionAcronym,
QString theEllipsoidAcronym,
QString theProj4String,
long theSRID,
long theEpsg,
bool theGeoFlag)
: mMapUnits(QGis::UNKNOWN),
mIsValidFlag(0)
{
// NOOP
}
QgsSpatialRefSys::QgsSpatialRefSys(const long theId, SRS_TYPE theType)
: mMapUnits(QGis::UNKNOWN),
mIsValidFlag(0)
{
createFromId(theId, theType);
}
void QgsSpatialRefSys::createFromId(const long theId, SRS_TYPE theType)
{
switch (theType)
{
case QGIS_SRSID:
createFromSrsId(theId);
break;
case POSTGIS_SRID:
createFromSrid(theId);
break;
case EPSG:
createFromEpsg(theId);
break;
default:
//THIS IS BAD...THIS PART OF CODE SHOULD NEVER BE REACHED...
QgsLogger::critical("Unexpected case reached in " + QString(__FILE__) + " : " + QString(__LINE__));
};
}
bool QgsSpatialRefSys::createFromOgcWmsCrs(QString theCrs)
{
QStringList parts = theCrs.split(":");
if (parts.at(0) == "EPSG")
{
createFromEpsg( parts.at(1).toLong() );
}
else if (parts.at(0) == "CRS")
{
if (parts.at(1) == "84")
{
//! \todo - CRS:84 is hardcoded to EPSG:4326 - see if this is appropriate
/**
* See WMS 1.3 standard appendix B3 for details
*/
createFromEpsg( 4326 );
}
}
else
{
return FALSE;
}
return TRUE;
}
// Assignment operator
QgsSpatialRefSys& QgsSpatialRefSys::operator=(const QgsSpatialRefSys& srs)
{
if (&srs != this)
{
mSrsId = srs.mSrsId;
mDescription = srs.mDescription;
mProjectionAcronym = srs.mProjectionAcronym;
mEllipsoidAcronym = srs.mEllipsoidAcronym;
mProj4String = srs.mProj4String;
mGeoFlag = srs.mGeoFlag;
mMapUnits = srs.mMapUnits;
mSRID = srs.mSRID;
mEpsg = srs.mEpsg;
mIsValidFlag = srs.mIsValidFlag;
}
return *this;
}
// Misc helper functions -----------------------
void QgsSpatialRefSys::validate()
{
QgsDebugMsg("QgsSpatialRefSys::validate");
//dont bother trying to do an initial test with gdal if
//the proj4String is not even populated
if (QString::null!=mProj4String && !mProj4String.isEmpty())
{
//first of all use gdal to test if this is an ok srs already
//if not we will prompt the user for and srs
//then retest using gdal
//if the retest fails we will then set this srs to the GEOCS/WGS84 default
/* Here are the possible OGR error codes :
typedef int OGRErr;
#define OGRERR_NONE 0
#define OGRERR_NOT_ENOUGH_DATA 1 --> not enough data to deserialize
#define OGRERR_NOT_ENOUGH_MEMORY 2
#define OGRERR_UNSUPPORTED_GEOMETRY_TYPE 3
#define OGRERR_UNSUPPORTED_OPERATION 4
#define OGRERR_CORRUPT_DATA 5
#define OGRERR_FAILURE 6
#define OGRERR_UNSUPPORTED_SRS 7 */
//get the wkt into ogr
//this is really ugly but we need to get a QString to a char**
const char *mySourceCharArrayPointer = mProj4String.latin1();
//create the sr and populate it from a wkt proj definition
OGRSpatialReference myOgrSpatialRef;
OGRErr myInputResult = myOgrSpatialRef.importFromProj4( mySourceCharArrayPointer );
if (myInputResult==OGRERR_NONE)
{
//srs is valid so nothing more to do...
createFromProj4(mProj4String);
return;
}
}
QSettings mySettings;
QString myDefaultProjectionOption =
mySettings.readEntry("/Projections/defaultBehaviour");
if (myDefaultProjectionOption=="prompt")
{
//@note this class is not a descendent of QWidget so we cant pass
//it in the ctor of the layer projection selector
QgsLayerProjectionSelector * mySelector = new QgsLayerProjectionSelector();
long myDefaultSRS =
QgsProject::instance()->readNumEntry("SpatialRefSys","/ProjectSRSID",GEOSRS_ID);
mySelector->setSelectedSRSID(myDefaultSRS);
if(mySelector->exec())
{
createFromSrsId(mySelector->getCurrentSRSID());
}
else
{
QApplication::restoreOverrideCursor();
}
delete mySelector;
}
else if (myDefaultProjectionOption=="useProject")
{
// XXX TODO: Change project to store selected CS as 'projectSRS' not 'selectedWKT'
mProj4String = QgsProject::instance()->readEntry("SpatialRefSys","//ProjectSRSProj4String",GEOPROJ4);
}
else ///Projections/defaultBehaviour==useDefault
{
// XXX TODO: Change global settings to store default CS as 'defaultSRS' not 'defaultProjectionWKT'
mProj4String = mySettings.readEntry("/Projections/defaultSRS",GEOPROJ4);
}
//
// This is the second check after the user assigned SRS has been retrieved
// If it still does not work, we will simply use the QgsSpatialRefSys const GEOPROJ4 for the job
//
//this is really ugly but we need to get a QString to a char**
const char *mySourceCharArrayPointer = mProj4String.latin1();
//create the sr and populate it from a wkt proj definition
OGRSpatialReference myOgrSpatialRef;
OGRErr myInputResult = myOgrSpatialRef.importFromProj4( mySourceCharArrayPointer );
if (! myInputResult==OGRERR_NONE)
{
//default to proj 4..if all else fails we will use that for this srs
mProj4String = GEOPROJ4;
}
createFromProj4(mProj4String);
return;
}
bool QgsSpatialRefSys::createFromSrid(long theSrid)
{
QgsDebugMsg("QgsSpatialRefSys::createFromSrid");
// Get the full path name to the sqlite3 spatial reference database.
QString myDatabaseFileName = QgsApplication::srsDbFilePath();
sqlite3 *myDatabase;
const char *myTail;
sqlite3_stmt *myPreparedStatement;
int myResult;
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return false;
}
/*
srs_id INTEGER PRIMARY KEY,
description text NOT NULL,
projection_acronym text NOT NULL,
ellipsoid_acronym NOT NULL,
parameters text NOT NULL,
srid integer NOT NULL,
epsg integer NOT NULL,
is_geo integer NOT NULL);
*/
QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,epsg,is_geo from tbl_srs where srid='" + QString::number(theSrid) + "'";
myResult = sqlite3_prepare(myDatabase, mySql.utf8(), mySql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
mSrsId = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,0)).toLong();
mDescription = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,1));
mProjectionAcronym = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,2));
mEllipsoidAcronym = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,3));
mProj4String = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,4));
mSRID = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,5)).toLong();
mEpsg = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,6)).toLong();
int geo = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,7)).toInt();
mGeoFlag = (geo == 0 ? false : true);
setMapUnits();
mIsValidFlag = true;
}
else
{
QgsDebugMsg("QgsSpatialRefSys::createFromSrid failed : " + mySql);
mIsValidFlag = false;
}
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
return mIsValidFlag;
}
bool QgsSpatialRefSys::createFromWkt(QString theWkt)
{
if (theWkt.isEmpty())
{
QgsLogger::critical("QgsSpatialRefSys::createFromWkt -- theWkt is uninitialised, operation failed");
mIsValidFlag = false;
return false;
}
QgsDebugMsg("QgsSpatialRefSys::createFromWkt(QString theWkt) using: " + theWkt);
//this is really ugly but we need to get a QString to a char**
const char *myCharArrayPointer = theWkt.latin1(); //Why doesn't it work with toLocal8Bit().data()?
char *pWkt = (char *)myCharArrayPointer;
/* Here are the possible OGR error codes :
typedef int OGRErr;
#define OGRERR_NONE 0
#define OGRERR_NOT_ENOUGH_DATA 1 --> not enough data to deserialize
#define OGRERR_NOT_ENOUGH_MEMORY 2
#define OGRERR_UNSUPPORTED_GEOMETRY_TYPE 3
#define OGRERR_UNSUPPORTED_OPERATION 4
#define OGRERR_CORRUPT_DATA 5
#define OGRERR_FAILURE 6
#define OGRERR_UNSUPPORTED_SRS 7
*/
OGRSpatialReference myOgrSpatialRef;
OGRErr myInputResult = myOgrSpatialRef.importFromWkt( &pWkt );
if (myInputResult != OGRERR_NONE)
{
QgsDebugMsg("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
QgsDebugMsg("QgsSpatialRefSys::createFromWkt(QString theWkt) ");
QgsDebugMsg("This SRS could *** NOT *** be set from the supplied WKT ");
QgsDebugMsg("INPUT: " + theWkt);
QgsDebugMsg("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
mIsValidFlag = false;
return false;
}
// always morph from esri as it doesn't hurt anything
myOgrSpatialRef.morphFromESRI();
// create the proj4 structs needed for transforming
char *proj4src;
myOgrSpatialRef.exportToProj4(&proj4src);
//now that we have the proj4string, delegate to createFromProj4String so
// that we can try to fill in the remaining class members...
//create from Proj wil set the isValidFalg
createFromProj4(QString(proj4src));
return mIsValidFlag;
//setMapunits will be called by createfromproj above
}
bool QgsSpatialRefSys::createFromEpsg(long theEpsg)
{
QgsDebugMsg("QgsSpatialRefSys::createFromEpsg with " + QString::number(theEpsg));
// Get the full path name to the sqlite3 spatial reference database.
QString myDatabaseFileName = QgsApplication::srsDbFilePath();
sqlite3 *myDatabase;
const char *myTail;
sqlite3_stmt *myPreparedStatement;
int myResult;
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return false;
}
/*
srs_id INTEGER PRIMARY KEY,
description text NOT NULL,
projection_acronym text NOT NULL,
ellipsoid_acronym NOT NULL,
parameters text NOT NULL,
srid integer NOT NULL,
epsg integer NOT NULL,
is_geo integer NOT NULL);
*/
QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,epsg,is_geo from tbl_srs where epsg='" + QString::number(theEpsg) + "'";
myResult = sqlite3_prepare(myDatabase, mySql.utf8(), mySql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
mSrsId = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,0)).toLong();
mDescription = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,1));
mProjectionAcronym = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,2));
mEllipsoidAcronym = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,3));
mProj4String = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,4));
mSRID = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,5)).toLong();
mEpsg = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,6)).toLong();
int geo = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,7)).toInt();
mGeoFlag = (geo == 0 ? false : true);
setMapUnits();
mIsValidFlag = true;
}
else
{
QgsLogger::critical(" QgsSpatialRefSys::createFromEpsg failed : " + mySql);
mIsValidFlag = false;
}
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
return mIsValidFlag;
}
bool QgsSpatialRefSys::createFromSrsId (long theSrsId)
{
QgsDebugMsg("QgsSpatialRefSys::createFromSrsId");
QString myDatabaseFileName;
//
// Determine if this is a user projection or a system on
// user projection defs all have srs_id >= 100000
//
if (theSrsId>= USER_PROJECTION_START_ID)
{
myDatabaseFileName = QgsApplication::qgisUserDbFilePath();
QFileInfo myFileInfo;
myFileInfo.setFile(myDatabaseFileName);
if ( !myFileInfo.exists( ) )
{
mIsValidFlag = false;
QgsLogger::warning("QgsSpatialRefSys::createFromSrid failed : users qgis.db not found");
return mIsValidFlag;
}
}
else //must be a system projection then
{
// Get the full path name to the sqlite3 spatial reference database.
myDatabaseFileName = QgsApplication::srsDbFilePath();
}
sqlite3 *myDatabase;
const char *myTail;
sqlite3_stmt *myPreparedStatement;
int myResult;
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return false;
}
/*
srs_id INTEGER PRIMARY KEY,
description text NOT NULL,
projection_acronym text NOT NULL,
ellipsoid_acronym NOT NULL,
parameters text NOT NULL,
srid integer NOT NULL,
epsg integer NOT NULL,
is_geo integer NOT NULL);
*/
QString mySql = "select srs_id,description,projection_acronym,ellipsoid_acronym,parameters,srid,epsg,is_geo from tbl_srs where srs_id='" + QString::number(theSrsId) + "'";
myResult = sqlite3_prepare(myDatabase, mySql.utf8(), mySql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
mSrsId = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,0)).toLong();
mDescription = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,1));
mProjectionAcronym = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,2));
mEllipsoidAcronym = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,3));
mProj4String = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,4));
mSRID = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,5)).toLong();
mEpsg = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,6)).toLong();
int geo = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,7)).toInt();
mGeoFlag = (geo == 0 ? false : true);
setMapUnits();
mIsValidFlag = true;
}
else
{
QgsLogger::warning("QgsSpatialRefSys::createFromSrsId failed : " + mySql);
mIsValidFlag = false;
}
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
return mIsValidFlag;
}
bool QgsSpatialRefSys::isValid() const
{
if (mProj4String.isEmpty())
return false;
//this is really ugly but we need to get a QString to a char**
const char *mySourceCharArrayPointer = mProj4String.latin1();
//create the sr and populate it from a wkt proj definition
OGRSpatialReference myOgrSpatialRef;
OGRErr myResult = myOgrSpatialRef.importFromProj4( mySourceCharArrayPointer );
if (myResult==OGRERR_NONE)
{
//srs is valid so nothing more to do...
return true;
}
else
{
return false;
}
}
bool QgsSpatialRefSys::createFromProj4 (const QString theProj4String)
{
//
// Example:
// +proj=tmerc +lat_0=0 +lon_0=-62 +k=0.999500 +x_0=400000 +y_0=0
// +ellps=clrk80 +towgs84=-255,-15,71,0,0,0,0 +units=m +no_defs
//
mIsValidFlag=false;
QRegExp myProjRegExp( "\\+proj=\\S+" );
int myStart= 0;
int myLength=0;
myStart = myProjRegExp.search(theProj4String, myStart);
if (myStart==-1)
{
QgsLogger::warning("QgsSpatialRefSys::createFromProj4 error proj string supplied has no +proj argument");
return mIsValidFlag;
}
else
{
myLength = myProjRegExp.matchedLength();
}
mProjectionAcronym = theProj4String.mid(myStart+PROJ_PREFIX_LEN,myLength-PROJ_PREFIX_LEN);
QRegExp myEllipseRegExp( "\\+ellps=\\S+" );
myStart= 0;
myLength=0;
myStart = myEllipseRegExp.search(theProj4String, myStart);
if (myStart==-1)
{
std::cout << "QgsSpatialRefSys::createFromProj4 error proj string supplied has no +ellps argument" << std::endl;
return mIsValidFlag;
}
else
{
myLength = myEllipseRegExp.matchedLength();
}
mEllipsoidAcronym = theProj4String.mid(myStart+ELLPS_PREFIX_LEN,myLength-ELLPS_PREFIX_LEN);
//mproj4string must be set here for the rest of this method to behave in a meaningful way...
mProj4String = theProj4String;
/*
* We try to match the proj string to and srsid using the following logic:
*
* - perform a whole text search on srs name (if not null). The srs name will
* have been set if this method has been delegated to from createFromWkt.
* Normally we wouldnt expect this to work, but its worth trying first
* as its quicker than methods below..
*/
long mySrsId = 0;
QgsSpatialRefSys::RecordMap myRecord;
if (!mDescription.stripWhiteSpace ().isEmpty())
{
myRecord = getRecord("select * from tbl_srs where description='" + mDescription.stripWhiteSpace () + "'");
}
if (!myRecord.empty())
{
mySrsId=myRecord["srs_id"].toLong();
QgsDebugMsg("QgsSpatialRefSys::createFromProj4 Projection Description match search for srsid returned srsid: "\
+ QString::number(mySrsId));
if (mySrsId > 0)
{
createFromSrsId(mySrsId);
}
}
else
{
/*
* - if the above does not match perform a whole text search on proj4 string (if not null)
*/
QgsDebugMsg("QgsSpatialRefSys::createFromProj4 wholetext match on name failed, trying proj4string match");
myRecord = getRecord("select * from tbl_srs where parameters='" + mProj4String.stripWhiteSpace () + "'");
if (!myRecord.empty())
{
mySrsId=myRecord["srs_id"].toLong();
QgsDebugMsg("QgsSpatialRefSys::createFromProj4 proj4string match search for srsid returned srsid: " \
+ QString::number(mySrsId));
if (mySrsId > 0)
{
createFromSrsId(mySrsId);
}
}
else
{
QgsDebugMsg("QgsSpatialRefSys::createFromProj4 globbing search for srsid from this proj string");
mySrsId = findMatchingProj();
QgsDebugMsg("QgsSpatialRefSys::createFromProj4 globbing search for srsid returned srsid: "\
+ QString::number(mySrsId));
if (mySrsId > 0)
{
createFromSrsId(mySrsId);
}
}
}
/* If its still empty after all the above steps then all we can do is keep the proj string
* with what was passed in and hope for the best...If its not empty we can fill other member details in
from the record*/
return mIsValidFlag;
}
QgsSpatialRefSys::RecordMap QgsSpatialRefSys::getRecord(QString theSql)
{
QString myDatabaseFileName;
QgsSpatialRefSys::RecordMap myMap;
QString myFieldName;
QString myFieldValue;
sqlite3 *myDatabase;
const char *myTail;
sqlite3_stmt *myPreparedStatement;
int myResult;
QgsDebugMsg("QgsSpatialRefSys::getRecord...running query: " + theSql);
// Get the full path name to the sqlite3 spatial reference database.
myDatabaseFileName = QgsApplication::srsDbFilePath();
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return myMap;
}
myResult = sqlite3_prepare(myDatabase, theSql.utf8(), theSql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
int myColumnCount = sqlite3_column_count(myPreparedStatement);
//loop through each column in the record adding its field name and vvalue to the map
for (int myColNo=0;myColNo < myColumnCount;myColNo++)
{
myFieldName = QString::fromUtf8((char *)sqlite3_column_name(myPreparedStatement,myColNo));
myFieldValue = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,myColNo));
myMap[myFieldName]=myFieldValue;
}
}
else
{
QgsDebugMsg("QgsSpatialRefSys::getRecord...trying system users.db");
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
myDatabaseFileName = QgsApplication::qgisUserDbFilePath();
QFileInfo myFileInfo;
myFileInfo.setFile(myDatabaseFileName);
if ( !myFileInfo.exists( ) )
{
QgsLogger::warning("QgsSpatialRefSys::getRecord failed : users qgis.db not found");
return myMap;
}
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return myMap;
}
myResult = sqlite3_prepare(myDatabase, theSql.utf8(), theSql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK && sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
int myColumnCount = sqlite3_column_count(myPreparedStatement);
//loop through each column in the record adding its field name and vvalue to the map
for (int myColNo=0;myColNo < myColumnCount;myColNo++)
{
myFieldName = QString::fromUtf8((char *)sqlite3_column_name(myPreparedStatement,myColNo));
myFieldValue = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,myColNo));
myMap[myFieldName]=myFieldValue;
}
}
else
{
QgsLogger::warning("QgsSpatialRefSys::getRecord failed : " + theSql);
}
}
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
#ifdef QGISDEBUG
QgsDebugMsg("QgsSpatialRefSys::getRecord retrieved: " + theSql);
RecordMap::Iterator it;
for ( it = myMap.begin(); it != myMap.end(); ++it )
{
QgsDebugMsgLevel(it.key() + " => " + it.data(), 2);
}
#endif
return myMap;
}
// Accessors -----------------------------------
/*! Get the SrsId
* @return long theSrsId The internal sqlite3 srs.db primary key for this srs
*/
long QgsSpatialRefSys::srsid() const
{
return mSrsId;
}
/*! Get the Postgis SRID - if possible
* @return long theSRID The internal postgis SRID for this SRS
*/
long QgsSpatialRefSys::srid() const
{
return mSRID;
}
/*! Get the Description
* @return QString the Description A textual description of the srs.
*/
QString QgsSpatialRefSys::description () const
{
if (mDescription.isNull())
{
return "";
}
else
{
return mDescription;
}
}
/*! Get the Projection Acronym
* @return QString theProjectionAcronym The official proj4 acronym for the projection family
*/
QString QgsSpatialRefSys::projectionAcronym() const
{
if (mProjectionAcronym.isNull())
{
return "";
}
else
{
return mProjectionAcronym;
}
}
/*! Get the Ellipsoid Acronym
* @return QString theEllipsoidAcronym The official proj4 acronym for the ellipoid
*/
QString QgsSpatialRefSys::ellipsoidAcronym () const
{
if (mEllipsoidAcronym.isNull())
{
return "";
}
else
{
return mEllipsoidAcronym;
}
}
/* Get the Proj Proj4String.
* @return QString theProj4String Proj4 format specifies that define this srs.
*/
QString QgsSpatialRefSys::proj4String() const
{
if (mProj4String.isNull())
{
return "";
}
else
{
return mProj4String;
}
}
/*! Get this Geographic? flag
* @return bool theGeoFlag Whether this is a geographic or projected coordinate system
*/
bool QgsSpatialRefSys::geographicFlag () const
{
return mGeoFlag;
}
/*! Get the units that the projection is in
* @return QGis::units
*/
QGis::units QgsSpatialRefSys::mapUnits() const
{
return mMapUnits;
}
/*! Set the postgis srid for this srs
* @return long theSRID the Postgis spatial_ref_sys identifier for this srs (defaults to 0)
*/
long QgsSpatialRefSys::postgisSrid () const
{
return mSRID ;
}
/*! Set the EPSG identifier for this srs
* @return long theEpsg the ESPG identifier for this srs (defaults to 0)
*/
long QgsSpatialRefSys::epsg () const
{
return mEpsg;
}
// Mutators -----------------------------------
void QgsSpatialRefSys::setSrsId(long theSrsId)
{
mSrsId = theSrsId;
}
void QgsSpatialRefSys::setSrid(long theSrid)
{
mSRID=theSrid;
}
void QgsSpatialRefSys::setDescription (QString theDescription)
{
mDescription = theDescription;
}
void QgsSpatialRefSys::setProj4String (QString theProj4String)
{
mProj4String = theProj4String;
}
void QgsSpatialRefSys::setGeographicFlag (bool theGeoFlag)
{
mGeoFlag=theGeoFlag;
}
void QgsSpatialRefSys::setEpsg (long theEpsg)
{
mEpsg=theEpsg;
}
void QgsSpatialRefSys::setProjectionAcronym(QString theProjectionAcronym)
{
mProjectionAcronym=theProjectionAcronym;
}
void QgsSpatialRefSys::setEllipsoidAcronym(QString theEllipsoidAcronym)
{
mEllipsoidAcronym=theEllipsoidAcronym;
}
/*! Work out the projection units and set the appropriate local variable
*
*/
void QgsSpatialRefSys::setMapUnits()
{
if (mProj4String.isEmpty())
{
QgsLogger::warning("No proj4 projection string. Unable to set map units.");
mMapUnits = QGis::UNKNOWN;
return;
}
char *unitName;
OGRSpatialReference myOgrSpatialRef;
myOgrSpatialRef.importFromProj4(mProj4String.latin1());
// Of interest to us is that this call adds in a unit parameter if
// one doesn't already exist.
myOgrSpatialRef.Fixup();
if (myOgrSpatialRef.IsProjected())
{
double toMeter = myOgrSpatialRef.GetLinearUnits(&unitName);
QString unit(unitName);
// If the units parameter was created during the Fixup() call
// above, the name of the units is likely to be 'unknown'. Try to
// do better than that ... (but perhaps ogr should be enhanced to
// do this instead?).
static const double feetToMeter = 0.3048;
static const double smallNum = 1e-3;
if (std::abs(toMeter - feetToMeter) < smallNum)
unit = "Foot";
QgsDebugMsg("Projection has linear units of " + unit);
if (unit == "Meter")
mMapUnits = QGis::METERS;
else if (unit == "Foot")
mMapUnits = QGis::FEET;
else
{
QgsLogger::warning("Unsupported map units of " + unit);
mMapUnits = QGis::UNKNOWN;
}
}
else
{
myOgrSpatialRef.GetAngularUnits(&unitName);
QString unit(unitName);
if (unit == "degree")
mMapUnits = QGis::DEGREES;
else
{
QgsLogger::warning("Unsupported map units of " + unit);
mMapUnits = QGis::UNKNOWN;
}
QgsDebugMsg("Projection has angular units of " + unit);
}
}
/*
* check if srs is a geocs or a proj cs (using ogr isGeographic)
* then sequentially walk through the database (first users qgis.db srs tbl then
* system srs.db tbl), converting each entry into an ogr srs and using isSame
* or isSameGeocs (essentially calling the == overloaded operator). We'll try to
* be smart about this and first parse out the proj and ellpse strings and only
* check for a match in entities that have the same ellps and proj entries so
* that it doesnt munch yer cpu so much.
*/
long QgsSpatialRefSys::findMatchingProj()
{
QgsDebugMsg("QgsSpatialRefSys::findMatchingProj...");
if (mEllipsoidAcronym.isNull() || mProjectionAcronym.isNull() || mProj4String.isNull())
{
QgsLogger::warning("QgsSpatialRefSys::findMatchingProj will only work if prj acr ellipsoid acr and proj4string are set!...");
return 0;
}
sqlite3 *myDatabase;
const char *myTail;
sqlite3_stmt *myPreparedStatement;
int myResult;
// Set up the query to retreive the projection information needed to populate the list
QString mySql = QString ("select srs_id,parameters from tbl_srs where projection_acronym='" +
mProjectionAcronym + "' and ellipsoid_acronym='" + mEllipsoidAcronym + "'");
// Get the full path name to the sqlite3 spatial reference database.
QString myDatabaseFileName = QgsApplication::srsDbFilePath();
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return 0;
}
myResult = sqlite3_prepare(myDatabase, mySql.utf8(), mySql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK)
{
while(sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
QString mySrsId = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,0));
QString myProj4String = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement, 1));
if (this->equals(myProj4String))
{
QgsDebugMsg("QgsSpatialRefSys::findMatchingProj -------> MATCH FOUND in srs.db srsid: " + mySrsId);
// close the sqlite3 statement
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
return mySrsId.toLong();
}
else
{
//std::cout << " Not matched : " << myProj4String << std::endl;
}
}
}
//std::cout << "QgsSpatialRefSys::findMatchingProj -------> no match found in srs.db, trying user db now!" << std::endl;
// close the sqlite3 statement
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
//
// Try the users db now
//
myDatabaseFileName = QgsApplication::qgisUserDbFilePath();
//check the db is available
myResult = openDb(myDatabaseFileName, &myDatabase);
if(myResult)
{
return 0;
}
myResult = sqlite3_prepare(myDatabase, mySql.utf8(), mySql.length(), &myPreparedStatement, &myTail);
// XXX Need to free memory from the error msg if one is set
if(myResult == SQLITE_OK)
{
while(sqlite3_step(myPreparedStatement) == SQLITE_ROW)
{
QString mySrsId = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement,0));
QString myProj4String = QString::fromUtf8((char *)sqlite3_column_text(myPreparedStatement, 1));
if (this->equals(myProj4String))
{
QgsDebugMsg("QgsSpatialRefSys::findMatchingProj -------> MATCH FOUND in user qgis.db srsid: " + mySrsId);
// close the sqlite3 statement
sqlite3_finalize(myPreparedStatement);
sqlite3_close(myDatabase);
return mySrsId.toLong();
}
else
{
//std::cout << " Not matched : " << myProj4String << std::endl;
}
}
}