Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for rendering of rasters
- Loading branch information
Showing
7 changed files
with
230 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
#include "qgsrasterlayerrenderer.h" | ||
|
||
#include "qgsmessagelog.h" | ||
#include "qgsrasterdrawer.h" | ||
#include "qgsrasteriterator.h" | ||
#include "qgsrasterlayer.h" | ||
|
||
|
||
QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer* layer, QgsRenderContext& rendererContext ) | ||
: mRasterViewPort( 0 ) | ||
, mPipe( 0 ) | ||
{ | ||
|
||
mPainter = rendererContext.painter(); | ||
const QgsMapToPixel& theQgsMapToPixel = rendererContext.mapToPixel(); | ||
mMapToPixel = &theQgsMapToPixel; | ||
|
||
QgsRectangle myProjectedViewExtent; | ||
QgsRectangle myProjectedLayerExtent; | ||
|
||
if ( rendererContext.coordinateTransform() ) | ||
{ | ||
QgsDebugMsg( "coordinateTransform set -> project extents." ); | ||
try | ||
{ | ||
myProjectedViewExtent = rendererContext.coordinateTransform()->transformBoundingBox( rendererContext.extent() ); | ||
} | ||
catch ( QgsCsException &cs ) | ||
{ | ||
QgsMessageLog::logMessage( QObject::tr( "Could not reproject view extent: %1" ).arg( cs.what() ), QObject::tr( "Raster" ) ); | ||
myProjectedViewExtent.setMinimal(); | ||
} | ||
|
||
try | ||
{ | ||
myProjectedLayerExtent = rendererContext.coordinateTransform()->transformBoundingBox( layer->extent() ); | ||
} | ||
catch ( QgsCsException &cs ) | ||
{ | ||
QgsMessageLog::logMessage( QObject::tr( "Could not reproject layer extent: %1" ).arg( cs.what() ), QObject::tr( "Raster" ) ); | ||
myProjectedViewExtent.setMinimal(); | ||
} | ||
} | ||
else | ||
{ | ||
QgsDebugMsg( "coordinateTransform not set" ); | ||
myProjectedViewExtent = rendererContext.extent(); | ||
myProjectedLayerExtent = layer->extent(); | ||
} | ||
|
||
// clip raster extent to view extent | ||
QgsRectangle myRasterExtent = myProjectedViewExtent.intersect( &myProjectedLayerExtent ); | ||
if ( myRasterExtent.isEmpty() ) | ||
{ | ||
QgsDebugMsg( "draw request outside view extent." ); | ||
// nothing to do | ||
return; | ||
} | ||
|
||
QgsDebugMsg( "theViewExtent is " + rendererContext.extent().toString() ); | ||
QgsDebugMsg( "myProjectedViewExtent is " + myProjectedViewExtent.toString() ); | ||
QgsDebugMsg( "myProjectedLayerExtent is " + myProjectedLayerExtent.toString() ); | ||
QgsDebugMsg( "myRasterExtent is " + myRasterExtent.toString() ); | ||
|
||
// | ||
// The first thing we do is set up the QgsRasterViewPort. This struct stores all the settings | ||
// relating to the size (in pixels and coordinate system units) of the raster part that is | ||
// in view in the map window. It also stores the origin. | ||
// | ||
//this is not a class level member because every time the user pans or zooms | ||
//the contents of the rasterViewPort will change | ||
mRasterViewPort = new QgsRasterViewPort(); | ||
|
||
mRasterViewPort->mDrawnExtent = myRasterExtent; | ||
if ( rendererContext.coordinateTransform() ) | ||
{ | ||
mRasterViewPort->mSrcCRS = layer->crs(); | ||
mRasterViewPort->mDestCRS = rendererContext.coordinateTransform()->destCRS(); | ||
} | ||
else | ||
{ | ||
mRasterViewPort->mSrcCRS = QgsCoordinateReferenceSystem(); // will be invalid | ||
mRasterViewPort->mDestCRS = QgsCoordinateReferenceSystem(); // will be invalid | ||
} | ||
|
||
// get dimensions of clipped raster image in device coordinate space (this is the size of the viewport) | ||
mRasterViewPort->mTopLeftPoint = theQgsMapToPixel.transform( myRasterExtent.xMinimum(), myRasterExtent.yMaximum() ); | ||
mRasterViewPort->mBottomRightPoint = theQgsMapToPixel.transform( myRasterExtent.xMaximum(), myRasterExtent.yMinimum() ); | ||
|
||
// align to output device grid, i.e. floor/ceil to integers | ||
// TODO: this should only be done if paint device is raster - screen, image | ||
// for other devices (pdf) it can have floating point origin | ||
// we could use floating point for raster devices as well, but respecting the | ||
// output device grid should make it more effective as the resampling is done in | ||
// the provider anyway | ||
mRasterViewPort->mTopLeftPoint.setX( floor( mRasterViewPort->mTopLeftPoint.x() ) ); | ||
mRasterViewPort->mTopLeftPoint.setY( floor( mRasterViewPort->mTopLeftPoint.y() ) ); | ||
mRasterViewPort->mBottomRightPoint.setX( ceil( mRasterViewPort->mBottomRightPoint.x() ) ); | ||
mRasterViewPort->mBottomRightPoint.setY( ceil( mRasterViewPort->mBottomRightPoint.y() ) ); | ||
// recalc myRasterExtent to aligned values | ||
myRasterExtent.set( | ||
theQgsMapToPixel.toMapCoordinatesF( mRasterViewPort->mTopLeftPoint.x(), | ||
mRasterViewPort->mBottomRightPoint.y() ), | ||
theQgsMapToPixel.toMapCoordinatesF( mRasterViewPort->mBottomRightPoint.x(), | ||
mRasterViewPort->mTopLeftPoint.y() ) | ||
); | ||
|
||
//raster viewport top left / bottom right are already rounded to int | ||
mRasterViewPort->mWidth = static_cast<int>( mRasterViewPort->mBottomRightPoint.x() - mRasterViewPort->mTopLeftPoint.x() ); | ||
mRasterViewPort->mHeight = static_cast<int>( mRasterViewPort->mBottomRightPoint.y() - mRasterViewPort->mTopLeftPoint.y() ); | ||
|
||
|
||
//the drawable area can start to get very very large when you get down displaying 2x2 or smaller, this is becasue | ||
//theQgsMapToPixel.mapUnitsPerPixel() is less then 1, | ||
//so we will just get the pixel data and then render these special cases differently in paintImageToCanvas() | ||
|
||
QgsDebugMsgLevel( QString( "mapUnitsPerPixel = %1" ).arg( theQgsMapToPixel.mapUnitsPerPixel() ), 3 ); | ||
QgsDebugMsgLevel( QString( "mWidth = %1" ).arg( layer->width() ), 3 ); | ||
QgsDebugMsgLevel( QString( "mHeight = %1" ).arg( layer->height() ), 3 ); | ||
QgsDebugMsgLevel( QString( "myRasterExtent.xMinimum() = %1" ).arg( myRasterExtent.xMinimum() ), 3 ); | ||
QgsDebugMsgLevel( QString( "myRasterExtent.xMaximum() = %1" ).arg( myRasterExtent.xMaximum() ), 3 ); | ||
QgsDebugMsgLevel( QString( "myRasterExtent.yMinimum() = %1" ).arg( myRasterExtent.yMinimum() ), 3 ); | ||
QgsDebugMsgLevel( QString( "myRasterExtent.yMaximum() = %1" ).arg( myRasterExtent.yMaximum() ), 3 ); | ||
|
||
QgsDebugMsgLevel( QString( "mTopLeftPoint.x() = %1" ).arg( mRasterViewPort->mTopLeftPoint.x() ), 3 ); | ||
QgsDebugMsgLevel( QString( "mBottomRightPoint.x() = %1" ).arg( mRasterViewPort->mBottomRightPoint.x() ), 3 ); | ||
QgsDebugMsgLevel( QString( "mTopLeftPoint.y() = %1" ).arg( mRasterViewPort->mTopLeftPoint.y() ), 3 ); | ||
QgsDebugMsgLevel( QString( "mBottomRightPoint.y() = %1" ).arg( mRasterViewPort->mBottomRightPoint.y() ), 3 ); | ||
|
||
QgsDebugMsgLevel( QString( "mWidth = %1" ).arg( mRasterViewPort->mWidth ), 3 ); | ||
QgsDebugMsgLevel( QString( "mHeight = %1" ).arg( mRasterViewPort->mHeight ), 3 ); | ||
|
||
// /\/\/\ - added to handle zoomed-in rasters | ||
|
||
// TODO R->mLastViewPort = *mRasterViewPort; | ||
|
||
// TODO: is it necessary? Probably WMS only? | ||
layer->dataProvider()->setDpi( rendererContext.rasterScaleFactor() * 25.4 * rendererContext.scaleFactor() ); | ||
|
||
|
||
// copy the whole raster pipe! | ||
mPipe = new QgsRasterPipe( *layer->pipe() ); | ||
} | ||
|
||
QgsRasterLayerRenderer::~QgsRasterLayerRenderer() | ||
{ | ||
delete mRasterViewPort; | ||
delete mPipe; | ||
} | ||
|
||
bool QgsRasterLayerRenderer::render() | ||
{ | ||
if ( !mRasterViewPort ) | ||
return true; // outside of layer extent - nothing to do | ||
|
||
//R->draw( mPainter, mRasterViewPort, &mMapToPixel ); | ||
|
||
QTime time; | ||
time.start(); | ||
// | ||
// | ||
// The goal here is to make as many decisions as possible early on (outside of the rendering loop) | ||
// so that we can maximise performance of the rendering process. So now we check which drawing | ||
// procedure to use : | ||
// | ||
|
||
QgsRasterProjector *projector = mPipe->projector(); | ||
|
||
// TODO add a method to interface to get provider and get provider | ||
// params in QgsRasterProjector | ||
if ( projector ) | ||
{ | ||
projector->setCRS( mRasterViewPort->mSrcCRS, mRasterViewPort->mDestCRS ); | ||
} | ||
|
||
// Drawer to pipe? | ||
QgsRasterIterator iterator( mPipe->last() ); | ||
QgsRasterDrawer drawer( &iterator ); | ||
drawer.draw( mPainter, mRasterViewPort, mMapToPixel ); | ||
|
||
QgsDebugMsg( QString( "total raster draw time (ms): %1" ).arg( time.elapsed(), 5 ) ); | ||
|
||
return true; | ||
} |
Oops, something went wrong.