Skip to content

Commit

Permalink
Add tests for source pixel to coord rect conversion, rename for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 9, 2022
1 parent 2ee2c4e commit 2ea363d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/app/georeferencer/qgsgeorefmainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ void QgsGeoreferencerMainWindow::extentsChangedGeorefCanvas()
}

// Reproject the georeference plugin canvas into world coordinates and fit axis aligned bounding box
QgsRectangle rectMap = mGeorefTransform.hasExistingGeoreference() ? mGeorefTransform.getBoundingBox( mCanvas->extent(), true ) : mCanvas->extent();
QgsRectangle rectMap = mGeorefTransform.transformSourceExtent( mCanvas->extent(), true );
QgsRectangle boundingBox = transformViewportBoundingBox( rectMap, mGeorefTransform, true );

mExtentsChangedRecursionGuard = true;
Expand Down Expand Up @@ -820,7 +820,7 @@ void QgsGeoreferencerMainWindow::extentsChangedQGisCanvas()

// Reproject the canvas into raster coordinates and fit axis aligned bounding box
QgsRectangle boundingBox = transformViewportBoundingBox( QgisApp::instance()->mapCanvas()->extent(), mGeorefTransform, false );
QgsRectangle rectMap = mGeorefTransform.hasExistingGeoreference() ? mGeorefTransform.getBoundingBox( boundingBox, false ) : boundingBox;
QgsRectangle rectMap = mGeorefTransform.transformSourceExtent( boundingBox, false );

mExtentsChangedRecursionGuard = true;
// Just set the whole extent for now
Expand Down
6 changes: 4 additions & 2 deletions src/app/georeferencer/qgsgeoreftransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ class APP_EXPORT QgsGeorefTransform : public QgsGcpTransformerInterface
*/
QgsPointXY toSourceCoordinate( const QgsPointXY &pixel );

//! \returns Bounding box of image(transform to coordinate of Map or Image )
QgsRectangle getBoundingBox( const QgsRectangle &rect, bool toPixel ) { return mRasterChangeCoords.getBoundingBox( rect, toPixel ); }
/**
* Transforms a bounding box of the source image from source coordinates to source pixels or vice versa.
*/
QgsRectangle transformSourceExtent( const QgsRectangle &rect, bool toPixel ) { return mRasterChangeCoords.transformExtent( rect, toPixel ); }

//! \brief The transform parametrisation currently in use.
TransformMethod transformParametrisation() const;
Expand Down
5 changes: 4 additions & 1 deletion src/app/georeferencer/qgsrasterchangecoords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ QVector<QgsPointXY> QgsRasterChangeCoords::getPixelCoords( const QVector<QgsPoin
return pixelCoords;
}

QgsRectangle QgsRasterChangeCoords::getBoundingBox( const QgsRectangle &rect, bool toPixel )
QgsRectangle QgsRasterChangeCoords::transformExtent( const QgsRectangle &rect, bool toPixel )
{
if ( ! mHasExistingGeoreference )
return rect;

QgsRectangle rectReturn;
const QgsPointXY p1( rect.xMinimum(), rect.yMinimum() );
const QgsPointXY p2( rect.xMaximum(), rect.yMaximum() );
Expand Down
7 changes: 6 additions & 1 deletion src/app/georeferencer/qgsrasterchangecoords.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ class APP_EXPORT QgsRasterChangeCoords
void loadRaster( const QString &fileRaster );
bool hasExistingGeoreference() const { return mHasExistingGeoreference; }
QVector<QgsPointXY> getPixelCoords( const QVector<QgsPointXY> &mapCoords );
QgsRectangle getBoundingBox( const QgsRectangle &rect, bool toPixel );

/**
* Transforms a rectangle extent of the source image from source coordinates to source pixels or vice versa.
*/
QgsRectangle transformExtent( const QgsRectangle &rect, bool toPixel );

QgsPointXY toColumnLine( const QgsPointXY &pntMap );
QgsPointXY toXY( const QgsPointXY &pntPixel );

Expand Down
22 changes: 22 additions & 0 deletions tests/src/app/testqgsgeoreferencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ void TestQgsGeoreferencer::testTransformImageNoGeoference()
QCOMPARE( res.x(), 100.0 );
QCOMPARE( res.y(), 200.0 );

QgsRectangle rect = transform.transformSourceExtent( QgsRectangle( 0, 0, 100, 200 ), true );
QCOMPARE( rect.xMinimum(), 0.0 );
QCOMPARE( rect.yMinimum(), 0.0 );
QCOMPARE( rect.xMaximum(), 100.0 );
QCOMPARE( rect.yMaximum(), 200.0 );
rect = transform.transformSourceExtent( QgsRectangle( 0, 0, 100, 200 ), false );
QCOMPARE( rect.xMinimum(), 0.0 );
QCOMPARE( rect.yMinimum(), 0.0 );
QCOMPARE( rect.xMaximum(), 100.0 );
QCOMPARE( rect.yMaximum(), 200.0 );

QVERIFY( transform.updateParametersFromGcps( {QgsPointXY( 0, 0 ), QgsPointXY( 10, 0 ), QgsPointXY( 0, 30 ), QgsPointXY( 10, 30 )},
{QgsPointXY( 10, 5 ), QgsPointXY( 16, 5 ), QgsPointXY( 10, 8 ), QgsPointXY( 16, 8 )}, true ) );

Expand Down Expand Up @@ -142,6 +153,17 @@ void TestQgsGeoreferencer::testTransformImageWithExistingGeoreference()
QGSCOMPARENEAR( res.x(), 783414, 10 );
QGSCOMPARENEAR( res.y(), 3350122, 10 );

QgsRectangle rect = transform.transformSourceExtent( QgsRectangle( 781662.375, 3350923.125, 787362.375, 3362323.125 ), true );
QGSCOMPARENEAR( rect.xMinimum(), 0.0, 0.1 );
QGSCOMPARENEAR( rect.yMinimum(), 0.0, 0.1 );
QGSCOMPARENEAR( rect.xMaximum(), 100.0, 0.1 );
QGSCOMPARENEAR( rect.yMaximum(), 200.0, 0.1 );
rect = transform.transformSourceExtent( QgsRectangle( 0, 0, 100, 200 ), false );
QGSCOMPARENEAR( rect.xMinimum(), 781662.375, 0.1 );
QGSCOMPARENEAR( rect.yMinimum(), 3350923.125, 0.1 );
QGSCOMPARENEAR( rect.xMaximum(), 787362.375, 0.1 );
QGSCOMPARENEAR( rect.yMaximum(), 3362323.125, 0.1 );

QVector<QgsPointXY> pixelCoords = transform.mRasterChangeCoords.getPixelCoords(
{
QgsPointXY( 783414, 3350122 ),
Expand Down

0 comments on commit 2ea363d

Please sign in to comment.