Skip to content

Commit 0f41ca9

Browse files
committed
Improvements and docs for worldToPixel
1 parent 04708b7 commit 0f41ca9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,11 +1091,18 @@ QgsRasterIdentifyResult QgsGdalProvider::identify( const QgsPointXY &point, QgsR
10911091

10921092
bool QgsGdalProvider::worldToPixel( double x, double y, int &col, int &row ) const
10931093
{
1094+
/*
1095+
* This set of equations solves
1096+
* Xgeo = GT(0) + XpixelGT(1) + YlineGT(2)
1097+
* Ygeo = GT(3) + XpixelGT(4) + YlineGT(5)
1098+
* when Xgeo, Ygeo are given
1099+
*/
10941100
double div = ( mGeoTransform[2] * mGeoTransform[4] - mGeoTransform[1] * mGeoTransform[5] );
10951101
if ( div < 2 * std::numeric_limits<double>::epsilon() )
10961102
return false;
1097-
double doubleCol = -( mGeoTransform[2] * ( mGeoTransform[3] - y ) + mGeoTransform[5] * x - mGeoTransform[0] * mGeoTransform[5] ) / div;
1098-
double doubleRow = ( mGeoTransform[1] * ( mGeoTransform[3] - y ) + mGeoTransform[4] * x - mGeoTransform[0] * mGeoTransform[4] ) / div;
1103+
double doubleCol = -( mGeoTransform[2] * ( mGeoTransform[3] - y ) + mGeoTransform[5] * ( x - mGeoTransform[0] ) ) / div;
1104+
double doubleRow = ( mGeoTransform[1] * ( mGeoTransform[3] - y ) + mGeoTransform[4] * ( x - mGeoTransform[0] ) ) / div;
1105+
// note: we truncate here, not round, otherwise values will be 0.5 pixels off
10991106
col = static_cast< int >( doubleCol );
11001107
row = static_cast< int >( doubleRow );
11011108
return true;

src/providers/gdal/qgsgdalprovider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
292292
//! Close all cached dataset for the specified provider.
293293
static void closeCachedGdalHandlesFor( QgsGdalProvider *provider );
294294

295+
/**
296+
* Converts a world (\a x, \a y) coordinate to a pixel \a row and \a col.
297+
*/
295298
bool worldToPixel( double x, double y, int &col, int &row ) const;
296299
};
297300

0 commit comments

Comments
 (0)