Skip to content

Commit 17b73cd

Browse files
committedMar 2, 2023
Don't use scientific notation when generating gdal_translate
command from georeferencer Fixes #51813 (cherry picked from commit 854b87e)
1 parent 68ade5e commit 17b73cd

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed
 

‎src/app/georeferencer/qgsgeorefmainwindow.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "qgsgeoreftooldeletepoint.h"
5959
#include "qgsgeoreftoolmovepoint.h"
6060
#include "qgsgcpcanvasitem.h"
61+
#include "qgscoordinateutils.h"
6162

6263
#include "qgsgcplistwidget.h"
6364

@@ -2114,12 +2115,16 @@ QString QgsGeoreferencerMainWindow::generateGDALtranslateCommand( bool generateT
21142115
gdalCommand << QStringLiteral( "-co TFW=YES" );
21152116
}
21162117

2118+
const int precision = QgsCoordinateUtils::calculateCoordinatePrecision( mTargetCrs );
21172119
for ( QgsGeorefDataPoint *pt : std::as_const( mPoints ) )
21182120
{
21192121
const QgsPointXY pixel = mGeorefTransform.toSourcePixel( pt->sourcePoint() );
21202122
const QgsPointXY transformedDestinationPoint = pt->transformedDestinationPoint( mTargetCrs, QgsProject::instance()->transformContext() );
2121-
gdalCommand << QStringLiteral( "-gcp %1 %2 %3 %4" ).arg( pixel.x() ).arg( -pixel.y() )
2122-
.arg( transformedDestinationPoint.x() ).arg( transformedDestinationPoint.y() );
2123+
gdalCommand << QStringLiteral( "-gcp %1 %2 %3 %4" ).arg(
2124+
qgsDoubleToString( pixel.x(), 3 ),
2125+
qgsDoubleToString( -pixel.y(), 3 ),
2126+
qgsDoubleToString( transformedDestinationPoint.x(), precision ),
2127+
qgsDoubleToString( transformedDestinationPoint.y(), precision ) );
21232128
}
21242129

21252130
QFileInfo rasterFileInfo( mFileName );
@@ -2134,11 +2139,17 @@ QString QgsGeoreferencerMainWindow::generateGDALogr2ogrCommand() const
21342139
QStringList gdalCommand;
21352140
gdalCommand << QStringLiteral( "ogr2ogr" );
21362141

2142+
const int sourcePrecision = QgsCoordinateUtils::calculateCoordinatePrecision( mLayer->crs() );
2143+
const int destPrecision = QgsCoordinateUtils::calculateCoordinatePrecision( mTargetCrs );
2144+
21372145
for ( QgsGeorefDataPoint *pt : std::as_const( mPoints ) )
21382146
{
21392147
const QgsPointXY dest = pt->transformedDestinationPoint( mTargetCrs, QgsProject::instance()->transformContext() );
2140-
gdalCommand << QStringLiteral( "-gcp %1 %2 %3 %4" ).arg( pt->sourcePoint().x() ).arg( pt->sourcePoint().y() )
2141-
.arg( dest.x() ).arg( dest.y() );
2148+
gdalCommand << QStringLiteral( "-gcp %1 %2 %3 %4" ).arg(
2149+
qgsDoubleToString( pt->sourcePoint().x(), sourcePrecision ),
2150+
qgsDoubleToString( pt->sourcePoint().y(), sourcePrecision ),
2151+
qgsDoubleToString( dest.x(), destPrecision ),
2152+
qgsDoubleToString( dest.y(), destPrecision ) );
21422153
}
21432154

21442155
switch ( mTransformMethod )

‎tests/src/app/testqgsgeoreferencer.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,17 +848,32 @@ void TestQgsGeoreferencer::testGdalCommands()
848848
QgsGeoreferencerMainWindow window;
849849
window.openLayer( QgsMapLayerType::RasterLayer, QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ) );
850850

851-
window.addPoint( QgsPointXY( 783414, 3350122 ), QgsPointXY( 783414, 3350122 ), QgsCoordinateReferenceSystem() );
851+
window.addPoint( QgsPointXY( 783414, 3350122 ), QgsPointXY( 783414.001234567, 3350122.002345678 ), QgsCoordinateReferenceSystem() );
852852
window.addPoint( QgsPointXY( 791344, 3349795 ), QgsPointXY( 791344, 33497952 ), QgsCoordinateReferenceSystem() );
853853
window.addPoint( QgsPointXY( 783077, 334093 ), QgsPointXY( 783077, 334093 ), QgsCoordinateReferenceSystem() );
854854
window.addPoint( QgsPointXY( 791134, 3341401 ), QgsPointXY( 791134, 3341401 ), QgsCoordinateReferenceSystem() );
855855

856856
QString command = window.generateGDALtranslateCommand();
857857
// gdal_translate command must use source pixels, not geographic coordinates
858-
QCOMPARE( command, QStringLiteral( "gdal_translate -of GTiff -co TFW=YES -gcp 30.7303 14.0548 783414 3.35012e+06 -gcp 169.853 19.7917 791344 3.3498e+07 -gcp 24.818 52926.8 783077 334093 -gcp 166.169 167.055 791134 3.3414e+06 \"%1\" \"%2\"" ).arg(
858+
QCOMPARE( command, QStringLiteral( "gdal_translate -of GTiff -co TFW=YES -gcp 30.73 14.055 783414.001 3350122.002 -gcp 169.853 19.792 791344 33497952 -gcp 24.818 52926.844 783077 334093 -gcp 166.169 167.055 791134 3341401 \"%1\" \"%2\"" ).arg(
859859
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ),
860860
QDir::tempPath() + QStringLiteral( "/landsat.tif" ) ) );
861861

862+
command = window.generateGDALogr2ogrCommand();
863+
QCOMPARE( command, QStringLiteral( "ogr2ogr -gcp 783414 3350122 783414.001 3350122.002 -gcp 791344 3349795 791344 33497952 -gcp 783077 334093 783077 334093 -gcp 791134 3341401 791134 3341401 -tps -t_srs EPSG:32633 \"\" \"%1\"" ).arg(
864+
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ) ) );
865+
866+
window.mTargetCrs = QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) );
867+
command = window.generateGDALtranslateCommand();
868+
QgsDebugMsg( command );
869+
QCOMPARE( command, QStringLiteral( "gdal_translate -of GTiff -co TFW=YES -gcp 30.73 14.055 783414.00123457 3350122.00234568 -gcp 169.853 19.792 791344 33497952 -gcp 24.818 52926.844 783077 334093 -gcp 166.169 167.055 791134 3341401 \"%1\" \"%2\"" ).arg(
870+
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ),
871+
QDir::tempPath() + QStringLiteral( "/landsat.tif" ) ) );
872+
873+
command = window.generateGDALogr2ogrCommand();
874+
QgsDebugMsg( command );
875+
QCOMPARE( command, QStringLiteral( "ogr2ogr -gcp 783414 3350122 783414.00123457 3350122.00234568 -gcp 791344 3349795 791344 33497952 -gcp 783077 334093 783077 334093 -gcp 791134 3341401 791134 3341401 -tps -t_srs EPSG:4326 \"\" \"%1\"" ).arg(
876+
QStringLiteral( TEST_DATA_DIR ) + QStringLiteral( "/landsat.tif" ) ) );
862877
}
863878

864879
QGSTEST_MAIN( TestQgsGeoreferencer )

0 commit comments

Comments
 (0)
Please sign in to comment.