Skip to content

Commit ece9a92

Browse files
committedMay 16, 2015
QgsRasterProjector::destExtentSize - calc destination extent and size from source
1 parent 3b00dc1 commit ece9a92

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎src/core/raster/qgsrasterprojector.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* (at your option) any later version. *
1515
* *
1616
***************************************************************************/
17+
#include <algorithm>
1718

1819
#include "qgsrasterdataprovider.h"
1920
#include "qgscrscache.h"
@@ -941,3 +942,27 @@ QgsRasterBlock * QgsRasterProjector::block( int bandNo, QgsRectangle const & ex
941942

942943
return outputBlock;
943944
}
945+
946+
bool QgsRasterProjector::destExtentSize( const QgsRectangle& theSrcExtent, int theSrcXSize, int theSrcYSize,
947+
QgsRectangle& theDestExtent, int& theDestXSize, int& theDesYSize )
948+
{
949+
if ( theSrcExtent.isEmpty() || theSrcXSize <= 0 || theSrcYSize <= 0 )
950+
{
951+
return false;
952+
}
953+
QgsCoordinateTransform ct( mSrcCRS, mDestCRS );
954+
theDestExtent = ct.transformBoundingBox( theSrcExtent );
955+
956+
// We reproject pixel rectangle from center of source, of course, it gives
957+
// bigger xRes,yRes than reprojected edges (envelope), it may also be that
958+
// close to margins are higher resolutions (even very, too high)
959+
// TODO: consider more precise resolution calculation
960+
double xRes = theSrcExtent.width() / theSrcXSize;
961+
double yRes = theSrcExtent.height() / theSrcYSize;
962+
QgsPoint srcCenter = theSrcExtent.center();
963+
QgsRectangle srcCenterRectangle( srcCenter.x() - xRes / 2, srcCenter.y() - yRes / 2, srcCenter.x() + xRes / 2, srcCenter.y() + yRes / 2 );
964+
QgsRectangle destCenterRectangle = ct.transformBoundingBox( srcCenterRectangle );
965+
theDestXSize = std::max( 1, ( int )( theDestExtent.width() / destCenterRectangle.width() ) );
966+
theDesYSize = std::max( 1, ( int )( theDestExtent.height() / destCenterRectangle.height() ) );
967+
return true;
968+
}

‎src/core/raster/qgsrasterprojector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface
103103

104104
QgsRasterBlock *block( int bandNo, const QgsRectangle & extent, int width, int height ) override;
105105

106+
/** Calculate destination extent and size from source extent and size
107+
*/
108+
bool destExtentSize( const QgsRectangle& theSrcExtent, int theSrcXSize, int theSrcYSize,
109+
QgsRectangle& theDestExtent, int& theDestXSize, int& theDesYSize );
110+
106111
private:
107112
/** get source extent */
108113
QgsRectangle srcExtent() { return mSrcExtent; }

0 commit comments

Comments
 (0)
Please sign in to comment.