Skip to content

Commit

Permalink
Improvements and docs for worldToPixel
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 19, 2018
1 parent 04708b7 commit 0f41ca9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,11 +1091,18 @@ QgsRasterIdentifyResult QgsGdalProvider::identify( const QgsPointXY &point, QgsR

bool QgsGdalProvider::worldToPixel( double x, double y, int &col, int &row ) const
{
/*
* This set of equations solves
* Xgeo = GT(0) + XpixelGT(1) + YlineGT(2)
* Ygeo = GT(3) + XpixelGT(4) + YlineGT(5)
* when Xgeo, Ygeo are given
*/
double div = ( mGeoTransform[2] * mGeoTransform[4] - mGeoTransform[1] * mGeoTransform[5] );
if ( div < 2 * std::numeric_limits<double>::epsilon() )
return false;
double doubleCol = -( mGeoTransform[2] * ( mGeoTransform[3] - y ) + mGeoTransform[5] * x - mGeoTransform[0] * mGeoTransform[5] ) / div;
double doubleRow = ( mGeoTransform[1] * ( mGeoTransform[3] - y ) + mGeoTransform[4] * x - mGeoTransform[0] * mGeoTransform[4] ) / div;
double doubleCol = -( mGeoTransform[2] * ( mGeoTransform[3] - y ) + mGeoTransform[5] * ( x - mGeoTransform[0] ) ) / div;
double doubleRow = ( mGeoTransform[1] * ( mGeoTransform[3] - y ) + mGeoTransform[4] * ( x - mGeoTransform[0] ) ) / div;
// note: we truncate here, not round, otherwise values will be 0.5 pixels off
col = static_cast< int >( doubleCol );
row = static_cast< int >( doubleRow );
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/providers/gdal/qgsgdalprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
//! Close all cached dataset for the specified provider.
static void closeCachedGdalHandlesFor( QgsGdalProvider *provider );

/**
* Converts a world (\a x, \a y) coordinate to a pixel \a row and \a col.
*/
bool worldToPixel( double x, double y, int &col, int &row ) const;
};

Expand Down

0 comments on commit 0f41ca9

Please sign in to comment.