Skip to content

Commit

Permalink
Fix reading and writing invalid crs information to gcp points files
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 14, 2022
1 parent 8a12a0b commit feaa954
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/app/georeferencer/qgsgcplist.cpp
Expand Up @@ -140,12 +140,15 @@ bool QgsGCPList::saveGcps( const QString &filePath, const QgsCoordinateReference
if ( pointFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream points( &pointFile );
points << QStringLiteral( "#CRS: %1" ).arg( targetCrs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) );
if ( targetCrs.isValid() )
{
points << QStringLiteral( "#CRS: %1" ).arg( targetCrs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) );
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
points << endl;
points << endl;
#else
points << Qt::endl;
points << Qt::endl;
#endif
}

points << "mapX,mapY,sourceX,sourceY,enable,dX,dY,residual";
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
Expand Down Expand Up @@ -199,7 +202,15 @@ QList<QgsGcpPoint> QgsGCPList::loadGcps( const QString &filePath, const QgsCoord
int i = 0;
if ( line.contains( QLatin1String( "#CRS: " ) ) )
{
actualDestinationCrs = QgsCoordinateReferenceSystem( line.remove( QStringLiteral( "#CRS: " ) ) );
const QString crsDef = line.remove( QStringLiteral( "#CRS: " ) );
if ( !crsDef.trimmed().isEmpty() )
{
actualDestinationCrs = QgsCoordinateReferenceSystem( crsDef );
}
else
{
actualDestinationCrs = defaultDestinationCrs;
}
line = points.readLine();
lineNumber++;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/src/app/testqgsgeoreferencer.cpp
Expand Up @@ -47,6 +47,7 @@ class TestQgsGeoreferencer : public QObject
void testGeorefDataPoint();
void testGcpList();
void testSaveLoadGcps();
void testSaveLoadGcpsNoCrs();
void testTransformImageNoGeoference();
void testTransformImageWithExistingGeoreference();
void testRasterChangeCoords();
Expand Down Expand Up @@ -291,6 +292,67 @@ void TestQgsGeoreferencer::testSaveLoadGcps()
QCOMPARE( res.at( 2 ).destinationPointCrs().authid(), QStringLiteral( "EPSG:3857" ) );
}

void TestQgsGeoreferencer::testSaveLoadGcpsNoCrs()
{
// test saving and loading GCPs when no destination CRS is set for the points
QgsGCPList list;
QgsMapCanvas c1;
QgsMapCanvas c2;
list.append( new QgsGeorefDataPoint( &c1, &c2,
QgsPointXY( 111, 222 ), QgsPointXY( -30, 40 ), QgsCoordinateReferenceSystem(),
true ) );
list.append( new QgsGeorefDataPoint( &c1, &c2,
QgsPointXY( 11, 22 ), QgsPointXY( 34, -50 ), QgsCoordinateReferenceSystem(),
true ) );
// disabled!
list.append( new QgsGeorefDataPoint( &c1, &c2,
QgsPointXY( 33, 44 ), QgsPointXY( 100, 200 ), QgsCoordinateReferenceSystem(),
false ) );

QTemporaryDir dir;
QVERIFY( dir.isValid() );
const QString tempFilename = dir.filePath( QStringLiteral( "test2.points" ) );

QString error;

QVERIFY( list.saveGcps( tempFilename, QgsCoordinateReferenceSystem(),
QgsProject::instance()->transformContext(), error ) );
QVERIFY( error.isEmpty() );


// load
QgsCoordinateReferenceSystem actualDestinationCrs;
QList<QgsGcpPoint> res = QgsGCPList::loadGcps( tempFilename,
QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3111" ) ),
actualDestinationCrs,
error );
QVERIFY( error.isEmpty() );
QCOMPARE( res.size(), 3 );
// should fallback to default CRS
QCOMPARE( actualDestinationCrs.authid(), QStringLiteral( "EPSG:3111" ) );

QCOMPARE( res.at( 0 ).sourcePoint().x(), 111 );
QCOMPARE( res.at( 0 ).sourcePoint().y(), 222 );
QCOMPARE( res.at( 0 ).destinationPoint().x(), -30 );
QCOMPARE( res.at( 0 ).destinationPoint().y(), 40 );
QVERIFY( res.at( 0 ).isEnabled() );
QCOMPARE( res.at( 0 ).destinationPointCrs().authid(), QStringLiteral( "EPSG:3111" ) );

QCOMPARE( res.at( 1 ).sourcePoint().x(), 11 );
QCOMPARE( res.at( 1 ).sourcePoint().y(), 22 );
QCOMPARE( res.at( 1 ).destinationPoint().x(), 34 );
QCOMPARE( res.at( 1 ).destinationPoint().y(), -50 );
QVERIFY( res.at( 1 ).isEnabled() );
QCOMPARE( res.at( 1 ).destinationPointCrs().authid(), QStringLiteral( "EPSG:3111" ) );

QCOMPARE( res.at( 2 ).sourcePoint().x(), 33 );
QCOMPARE( res.at( 2 ).sourcePoint().y(), 44 );
QCOMPARE( res.at( 2 ).destinationPoint().x(), 100 );
QCOMPARE( res.at( 2 ).destinationPoint().y(), 200 );
QVERIFY( !res.at( 2 ).isEnabled() );
QCOMPARE( res.at( 2 ).destinationPointCrs().authid(), QStringLiteral( "EPSG:3111" ) );
}

void TestQgsGeoreferencer::testTransformImageNoGeoference()
{
QgsGeorefTransform transform( QgsGcpTransformerInterface::TransformMethod::Linear );
Expand Down

0 comments on commit feaa954

Please sign in to comment.