25 #include <QDomElement>
26 #include <QApplication>
28 #include <QStringList>
42 , mInitialisedFlag( false )
43 , mSourceProjection( 0 )
44 , mDestinationProjection( 0 )
45 , mSourceDatumTransform( -1 )
46 , mDestinationDatumTransform( -1 )
53 , mInitialisedFlag( false )
54 , mSourceProjection( 0 )
55 , mDestinationProjection( 0 )
56 , mSourceDatumTransform( -1 )
57 , mDestinationDatumTransform( -1 )
67 , mInitialisedFlag( false )
70 , mSourceProjection( 0 )
71 , mDestinationProjection( 0 )
72 , mSourceDatumTransform( -1 )
73 , mDestinationDatumTransform( -1 )
80 , mInitialisedFlag( false )
81 , mSourceProjection( 0 )
82 , mDestinationProjection( 0 )
83 , mSourceDatumTransform( -1 )
84 , mDestinationDatumTransform( -1 )
100 , mInitialisedFlag( false )
101 , mSourceProjection( 0 )
102 , mDestinationProjection( 0 )
103 , mSourceDatumTransform( -1 )
104 , mDestinationDatumTransform( -1 )
185 if ( !useDefaultDatumTransform )
196 if ( !useDefaultDatumTransform )
205 if ( !useDefaultDatumTransform )
213 #ifdef COORDINATE_TRANSFORM_VERBOSE
219 if ( !mDestinationProjection )
221 mInitialisedFlag =
false;
225 mInitialisedFlag =
false;
227 #ifdef COORDINATE_TRANSFORM_VERBOSE
228 if ( mInitialisedFlag )
230 QgsDebugMsg(
"------------------------------------------------------------" );
231 QgsDebugMsg(
"The OGR Coordinate transformation for this layer was set to" );
232 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Input",
mSourceCRS, __FILE__, __FUNCTION__, __LINE__ );
233 QgsLogger::debug<QgsCoordinateReferenceSystem>(
"Output",
mDestCRS, __FILE__, __FUNCTION__, __LINE__ );
234 QgsDebugMsg(
"------------------------------------------------------------" );
238 QgsDebugMsg(
"------------------------------------------------------------" );
239 QgsDebugMsg(
"The OGR Coordinate transformation FAILED TO INITIALISE!" );
240 QgsDebugMsg(
"------------------------------------------------------------" );
243 if ( !mInitialisedFlag )
245 QgsDebugMsg(
"Coordinate transformation failed to initialize!" );
281 double x = thePoint.
x();
282 double y = thePoint.
y();
339 #ifdef COORDINATE_TRANSFORM_VERBOSE
378 int nVertices = poly.size();
380 QVector<double> x( nVertices );
381 QVector<double> y( nVertices );
382 QVector<double> z( nVertices );
384 for (
int i = 0; i < nVertices; ++i )
386 const QPointF& pt = poly.at( i );
403 for (
int i = 0; i < nVertices; ++i )
405 QPointF& pt = poly[i];
412 QVector<double>& x, QVector<double>& y, QVector<double>& z,
418 Q_ASSERT( x.size() == y.size() );
439 TransformDirection direction )
const
466 QVector<float>& x, QVector<float>& y, QVector<float>& z,
467 TransformDirection direction )
const
472 Q_ASSERT( x.size() == y.size() );
482 int vectorSize = x.size();
483 QVector<double> xd( x.size() );
484 QVector<double> yd( y.size() );
485 QVector<double> zd( z.size() );
486 for (
int i = 0; i < vectorSize; ++i )
495 for (
int i = 0; i < vectorSize; ++i )
528 static const int numP = 8;
536 double x[numP * numP];
537 double y[numP * numP];
538 double z[numP * numP];
544 double dx = rect.
width() / ( double )( numP - 1 );
545 double dy = rect.
height() / ( double )( numP - 1 );
549 for (
int i = 0; i < numP ; i++ )
555 for (
int j = 0; j < numP; j++ )
557 x[( i*numP ) + j] = pointX;
558 y[( i*numP ) + j] = pointY;
560 z[( i*numP ) + j] = 0.0;
582 for (
int i = 0; i < numP * numP; i++ )
584 if ( qIsFinite( x[i] ) && qIsFinite( y[i] ) )
604 "The coordinates can not be reprojected. The CRS is: %1" )
611 "The coordinates can not be reprojected. The CRS is: %1" ).arg(
mDestCRS.
toProj4() ),
tr(
"CRS" ) );
615 #ifdef COORDINATE_TRANSFORM_VERBOSE
618 QgsDebugMsg( QString(
"[[[[[[ Number of points to transform: %1 ]]]]]]" ).arg( numPoints ) );
628 for (
int i = 0; i < numPoints; ++i )
648 if ( projResult != 0 )
653 for (
int i = 0; i < numPoints; ++i )
657 points += QString(
"(%1, %2)\n" ).arg( x[i], 0,
'f' ).arg( y[i], 0,
'f' );
661 points += QString(
"(%1, %2)\n" ).arg( x[i] * RAD_TO_DEG, 0,
'f' ).arg( y[i] * RAD_TO_DEG, 0,
'f' );
665 dir = ( direction ==
ForwardTransform ) ?
tr(
"forward transform" ) :
tr(
"inverse transform" );
667 QString msg =
tr(
"%1 of\n"
669 "PROJ.4: %3 +to %4\n"
674 .arg( QString::fromUtf8( pj_strerrno( projResult ) ) );
676 QgsDebugMsg(
"Projection failed emitting invalid transform signal: " + msg );
690 for (
int i = 0; i < numPoints; ++i )
697 #ifdef COORDINATE_TRANSFORM_VERBOSE
698 QgsDebugMsg( QString(
"[[[[[[ Projected %1, %2 to %3, %4 ]]]]]]" )
699 .arg( xorg, 0,
'g', 15 ).arg( yorg, 0,
'g', 15 )
700 .arg( *x, 0,
'g', 15 ).arg( *y, 0,
'g', 15 ) );
707 QgsDebugMsg(
"Reading Coordinate Transform from xml ------------------------!" );
709 QDomNode mySrcNode = theNode.namedItem(
"sourcesrs" );
712 QDomNode myDestNode = theNode.namedItem(
"destinationsrs" );
725 QDomElement myNodeElement = theNode.toElement();
726 QDomElement myTransformElement = theDoc.createElement(
"coordinatetransform" );
730 QDomElement mySourceElement = theDoc.createElement(
"sourcesrs" );
732 myTransformElement.appendChild( mySourceElement );
734 QDomElement myDestElement = theDoc.createElement(
"destinationsrs" );
736 myTransformElement.appendChild( myDestElement );
738 myNodeElement.appendChild( myTransformElement );
747 proj = QApplication::applicationDirPath()
748 +
"/share/proj/" + QString( name );
752 return proj.toUtf8();
773 QList< QList< int > > transformations;
778 if ( srcGeoId.isEmpty() || destGeoId.isEmpty() )
780 return transformations;
783 QStringList srcSplit = srcGeoId.split(
":" );
784 QStringList destSplit = destGeoId.split(
":" );
786 if ( srcSplit.size() < 2 || destSplit.size() < 2 )
788 return transformations;
791 int srcAuthCode = srcSplit.at( 1 ).toInt();
792 int destAuthCode = destSplit.at( 1 ).toInt();
794 if ( srcAuthCode == destAuthCode )
796 return transformations;
799 QList<int> directTransforms;
800 searchDatumTransform( QString(
"SELECT coord_op_code FROM tbl_datum_transform WHERE source_crs_code=%1 AND target_crs_code=%2 ORDER BY deprecated ASC,preferred DESC" ).arg( srcAuthCode ).arg( destAuthCode ),
802 QList<int> reverseDirectTransforms;
803 searchDatumTransform( QString(
"SELECT coord_op_code FROM tbl_datum_transform WHERE source_crs_code = %1 AND target_crs_code=%2 ORDER BY deprecated ASC,preferred DESC" ).arg( destAuthCode ).arg( srcAuthCode ),
804 reverseDirectTransforms );
805 QList<int> srcToWgs84;
806 searchDatumTransform( QString(
"SELECT coord_op_code FROM tbl_datum_transform WHERE (source_crs_code=%1 AND target_crs_code=%2) OR (source_crs_code=%2 AND target_crs_code=%1) ORDER BY deprecated ASC,preferred DESC" ).arg( srcAuthCode ).arg( 4326 ),
808 QList<int> destToWgs84;
809 searchDatumTransform( QString(
"SELECT coord_op_code FROM tbl_datum_transform WHERE (source_crs_code=%1 AND target_crs_code=%2) OR (source_crs_code=%2 AND target_crs_code=%1) ORDER BY deprecated ASC,preferred DESC" ).arg( destAuthCode ).arg( 4326 ),
813 QList<int>::const_iterator directIt = directTransforms.constBegin();
814 for ( ; directIt != directTransforms.constEnd(); ++directIt )
816 transformations.push_back( QList<int>() << *directIt << -1 );
820 directIt = reverseDirectTransforms.constBegin();
821 for ( ; directIt != reverseDirectTransforms.constEnd(); ++directIt )
823 transformations.push_back( QList<int>() << -1 << *directIt );
826 QList<int>::const_iterator srcWgsIt = srcToWgs84.constBegin();
827 for ( ; srcWgsIt != srcToWgs84.constEnd(); ++srcWgsIt )
829 QList<int>::const_iterator dstWgsIt = destToWgs84.constBegin();
830 for ( ; dstWgsIt != destToWgs84.constEnd(); ++dstWgsIt )
832 transformations.push_back( QList<int>() << *srcWgsIt << *dstWgsIt );
836 return transformations;
841 QStringList parameterSplit = proj4.split(
"+", QString::SkipEmptyParts );
842 QString currentParameter;
843 QString newProjString;
845 for (
int i = 0; i < parameterSplit.size(); ++i )
847 currentParameter = parameterSplit.at( i );
848 if ( !currentParameter.startsWith(
"towgs84", Qt::CaseInsensitive )
849 && !currentParameter.startsWith(
"nadgrids", Qt::CaseInsensitive ) )
851 newProjString.append(
"+" );
852 newProjString.append( currentParameter );
853 newProjString.append(
" " );
856 return newProjString;
863 if ( openResult != SQLITE_OK )
870 int prepareRes = sqlite3_prepare( db, sql.toAscii(), sql.size(), &stmt, NULL );
871 if ( prepareRes != SQLITE_OK )
873 sqlite3_finalize( stmt ); sqlite3_close( db );
878 while ( sqlite3_step( stmt ) == SQLITE_ROW )
880 cOpCode = (
const char * ) sqlite3_column_text( stmt, 0 );
881 transforms.push_back( cOpCode.toInt() );
883 sqlite3_finalize( stmt ); sqlite3_close( db );
888 QString transformString;
892 if ( openResult != SQLITE_OK )
895 return transformString;
899 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 );
900 int prepareRes = sqlite3_prepare( db, sql.toAscii(), sql.size(), &stmt, NULL );
901 if ( prepareRes != SQLITE_OK )
903 sqlite3_finalize( stmt ); sqlite3_close( db );
904 return transformString;
907 if ( sqlite3_step( stmt ) == SQLITE_ROW )
910 int methodCode = sqlite3_column_int( stmt, 0 );
911 if ( methodCode == 9615 )
913 transformString =
"+nadgrids=" + QString((
const char * )sqlite3_column_text( stmt, 1 ) );
915 else if ( methodCode == 9603 || methodCode == 9606 || methodCode == 9607 )
917 transformString +=
"+towgs84=";
918 double p1 = sqlite3_column_double( stmt, 1 );
919 double p2 = sqlite3_column_double( stmt, 2 );
920 double p3 = sqlite3_column_double( stmt, 3 );
921 double p4 = sqlite3_column_double( stmt, 4 );
922 double p5 = sqlite3_column_double( stmt, 5 );
923 double p6 = sqlite3_column_double( stmt, 6 );
924 double p7 = sqlite3_column_double( stmt, 7 );
925 if ( methodCode == 9603 )
927 transformString += QString(
"%1,%2,%3" ).arg( p1 ).arg( p2 ).arg( p3 );
931 transformString += QString(
"%1,%2,%3,%4,%5,%6,%7" ).arg( p1 ).arg( p2 ).arg( p3 ).arg( p4 ).arg( p5 ).arg( p6 ).arg( p7 );
936 sqlite3_finalize( stmt ); sqlite3_close( db );
937 return transformString;
944 if ( openResult != SQLITE_OK )
951 QString sql = QString(
"SELECT epsg_nr,source_crs_code,target_crs_code,remarks,scope,preferred,deprecated FROM tbl_datum_transform WHERE coord_op_code=%1" ).arg( datumTransform );
952 int prepareRes = sqlite3_prepare( db, sql.toAscii(), sql.size(), &stmt, NULL );
953 if ( prepareRes != SQLITE_OK )
955 sqlite3_finalize( stmt ); sqlite3_close( db );
959 int srcCrsId, destCrsId;
960 if ( sqlite3_step( stmt ) != SQLITE_ROW )
962 sqlite3_finalize( stmt );
967 epsgNr = sqlite3_column_int( stmt, 0 );
968 srcCrsId = sqlite3_column_int( stmt, 1 );
969 destCrsId = sqlite3_column_int( stmt, 2 );
970 remarks = QString::fromUtf8((
const char * ) sqlite3_column_text( stmt, 3 ) );
971 scope = QString::fromUtf8((
const char * ) sqlite3_column_text( stmt, 4 ) );
972 preferred = sqlite3_column_int( stmt, 5 ) != 0;
973 deprecated = sqlite3_column_int( stmt, 6 ) != 0;
982 sqlite3_finalize( stmt );
992 destProjString +=
" +nadgrids=@null";
997 srcProjString +=
" +nadgrids=@null";
1005 srcProjString +=
" +nadgrids=@null";
1009 destProjString +=
" +nadgrids=@null";