Skip to content

Commit

Permalink
QgsRasterProjector::destExtentSize - calc destination extent and size…
Browse files Browse the repository at this point in the history
… from source
  • Loading branch information
blazek committed May 16, 2015
1 parent 3b00dc1 commit ece9a92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/raster/qgsrasterprojector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include <algorithm>

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

return outputBlock;
}

bool QgsRasterProjector::destExtentSize( const QgsRectangle& theSrcExtent, int theSrcXSize, int theSrcYSize,
QgsRectangle& theDestExtent, int& theDestXSize, int& theDesYSize )
{
if ( theSrcExtent.isEmpty() || theSrcXSize <= 0 || theSrcYSize <= 0 )
{
return false;
}
QgsCoordinateTransform ct( mSrcCRS, mDestCRS );
theDestExtent = ct.transformBoundingBox( theSrcExtent );

// We reproject pixel rectangle from center of source, of course, it gives
// bigger xRes,yRes than reprojected edges (envelope), it may also be that
// close to margins are higher resolutions (even very, too high)
// TODO: consider more precise resolution calculation
double xRes = theSrcExtent.width() / theSrcXSize;
double yRes = theSrcExtent.height() / theSrcYSize;
QgsPoint srcCenter = theSrcExtent.center();
QgsRectangle srcCenterRectangle( srcCenter.x() - xRes / 2, srcCenter.y() - yRes / 2, srcCenter.x() + xRes / 2, srcCenter.y() + yRes / 2 );
QgsRectangle destCenterRectangle = ct.transformBoundingBox( srcCenterRectangle );
theDestXSize = std::max( 1, ( int )( theDestExtent.width() / destCenterRectangle.width() ) );
theDesYSize = std::max( 1, ( int )( theDestExtent.height() / destCenterRectangle.height() ) );
return true;
}
5 changes: 5 additions & 0 deletions src/core/raster/qgsrasterprojector.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface

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

/** Calculate destination extent and size from source extent and size
*/
bool destExtentSize( const QgsRectangle& theSrcExtent, int theSrcXSize, int theSrcYSize,
QgsRectangle& theDestExtent, int& theDestXSize, int& theDesYSize );

private:
/** get source extent */
QgsRectangle srcExtent() { return mSrcExtent; }
Expand Down

0 comments on commit ece9a92

Please sign in to comment.