@@ -137,7 +137,7 @@ void QgsCoordinateTransform::setDestCRSID( long theCRSID )
137
137
138
138
// XXX This whole function is full of multiple return statements!!!
139
139
// And probably shouldn't be a void
140
- void QgsCoordinateTransform::initialise ()
140
+ void QgsCoordinateTransform::initialise ( int srcDatumTransform, int destDatumTransform )
141
141
{
142
142
// XXX Warning - multiple return paths in this block!!
143
143
if ( !mSourceCRS .isValid () )
@@ -157,11 +157,32 @@ void QgsCoordinateTransform::initialise()
157
157
mDestCRS = QgsCRSCache::instance ()->crsByAuthId ( mSourceCRS .authid () );
158
158
}
159
159
160
+ bool useDefaultDatumTransform = ( srcDatumTransform == - 1 && destDatumTransform == -1 );
161
+
160
162
// init the projections (destination and source)
161
163
pj_free ( mDestinationProjection );
162
- mDestinationProjection = pj_init_plus ( mDestCRS .toProj4 ().toUtf8 () );
164
+ QString destProjString = mDestCRS .toProj4 ();
165
+ if ( !useDefaultDatumTransform )
166
+ {
167
+ destProjString = stripDatumTransform ( destProjString );
168
+ }
169
+ if ( destDatumTransform != -1 )
170
+ {
171
+ destProjString += ( " " + datumTransformString ( destDatumTransform ) );
172
+ }
173
+ mDestinationProjection = pj_init_plus ( destProjString.toUtf8 () );
174
+
163
175
pj_free ( mSourceProjection );
164
- mSourceProjection = pj_init_plus ( mSourceCRS .toProj4 ().toUtf8 () );
176
+ QString sourceProjString = mSourceCRS .toProj4 ();
177
+ if ( !useDefaultDatumTransform )
178
+ {
179
+ sourceProjString = stripDatumTransform ( sourceProjString );
180
+ }
181
+ if ( srcDatumTransform != -1 )
182
+ {
183
+ sourceProjString += ( " " + datumTransformString ( srcDatumTransform ) );
184
+ }
185
+ mSourceProjection = pj_init_plus ( sourceProjString.toUtf8 () );
165
186
166
187
#ifdef COORDINATE_TRANSFORM_VERBOSE
167
188
QgsDebugMsg ( " From proj : " + mSourceCRS .toProj4 () );
@@ -779,6 +800,26 @@ QList< QList< int > > QgsCoordinateTransform::datumTransformations( const QgsCoo
779
800
return transformations;
780
801
}
781
802
803
+ QString QgsCoordinateTransform::stripDatumTransform ( const QString& proj4 )
804
+ {
805
+ QStringList parameterSplit = proj4.split ( " +" , QString::SkipEmptyParts );
806
+ QString currentParameter;
807
+ QString newProjString;
808
+
809
+ for ( int i = 0 ; i < parameterSplit.size (); ++i )
810
+ {
811
+ currentParameter = parameterSplit.at ( i );
812
+ if ( !currentParameter.startsWith ( " towgs84" , Qt::CaseInsensitive )
813
+ && !currentParameter.startsWith ( " nadgrids" , Qt::CaseInsensitive ) )
814
+ {
815
+ newProjString.append ( " +" );
816
+ newProjString.append ( currentParameter );
817
+ newProjString.append ( " " );
818
+ }
819
+ }
820
+ return newProjString;
821
+ }
822
+
782
823
void QgsCoordinateTransform::searchDatumTransform ( const QString& sql, QList< int >& transforms )
783
824
{
784
825
sqlite3* db;
@@ -804,3 +845,54 @@ void QgsCoordinateTransform::searchDatumTransform( const QString& sql, QList< in
804
845
sqlite3_finalize ( stmt );
805
846
sqlite3_close ( db );
806
847
}
848
+
849
+ QString QgsCoordinateTransform::datumTransformString ( int datumTransform )
850
+ {
851
+ QString transformString;
852
+
853
+ sqlite3* db;
854
+ int openResult = sqlite3_open ( QgsApplication::srsDbFilePath ().toUtf8 ().constData (), &db );
855
+ if ( openResult != SQLITE_OK )
856
+ {
857
+ return transformString;
858
+ }
859
+
860
+ sqlite3_stmt* stmt;
861
+ QString sql = QString ( " SELECT coord_op_method_code, p1, p2, p3, p4, p5, p6, p7 FROM tbl_datum_transform WHERE coord_op_code = %1" ).arg ( datumTransform );
862
+ int prepareRes = sqlite3_prepare ( db, sql.toAscii (), sql.size (), &stmt, NULL );
863
+ if ( prepareRes != SQLITE_OK )
864
+ {
865
+ return transformString;
866
+ }
867
+
868
+ if ( sqlite3_step ( stmt ) == SQLITE_ROW )
869
+ {
870
+ // coord_op_methode_code
871
+ int methodCode = sqlite3_column_int ( stmt, 0 );
872
+ if ( methodCode == 9615 ) // ntv2
873
+ {
874
+ transformString = " +nadgrids=" + QString (( const char * )sqlite3_column_text ( stmt, 1 ) );
875
+ }
876
+ else if ( methodCode == 9603 || methodCode == 9606 || methodCode == 9607 )
877
+ {
878
+ transformString += " +towgs84=" ;
879
+ double p1 = sqlite3_column_double ( stmt, 1 );
880
+ double p2 = sqlite3_column_double ( stmt, 2 );
881
+ double p3 = sqlite3_column_double ( stmt, 3 );
882
+ double p4 = sqlite3_column_double ( stmt, 4 );
883
+ double p5 = sqlite3_column_double ( stmt, 5 );
884
+ double p6 = sqlite3_column_double ( stmt, 6 );
885
+ double p7 = sqlite3_column_double ( stmt, 7 );
886
+ if ( methodCode == 9603 ) // 3 parameter transformation
887
+ {
888
+ transformString += QString ( " %1,%2,%3" ).arg ( p1 ).arg ( p2 ).arg ( p3 );
889
+ }
890
+ else // 7 parameter transformation
891
+ {
892
+ transformString += QString ( " %1,%2,%3,%4,%5,%6,%7" ).arg ( p1 ).arg ( p2 ).arg ( p3 ).arg ( p4 ).arg ( p5 ).arg ( p6 ).arg ( p7 );
893
+ }
894
+ }
895
+ }
896
+
897
+ return transformString;
898
+ }
0 commit comments