|
| 1 | +#include "qgsrasterlayerrenderer.h" |
| 2 | + |
| 3 | +#include "qgsmessagelog.h" |
| 4 | +#include "qgsrasterdrawer.h" |
| 5 | +#include "qgsrasteriterator.h" |
| 6 | +#include "qgsrasterlayer.h" |
| 7 | + |
| 8 | + |
| 9 | +QgsRasterLayerRenderer::QgsRasterLayerRenderer( QgsRasterLayer* layer, QgsRenderContext& rendererContext ) |
| 10 | + : mRasterViewPort( 0 ) |
| 11 | + , mPipe( 0 ) |
| 12 | +{ |
| 13 | + |
| 14 | + mPainter = rendererContext.painter(); |
| 15 | + const QgsMapToPixel& theQgsMapToPixel = rendererContext.mapToPixel(); |
| 16 | + mMapToPixel = &theQgsMapToPixel; |
| 17 | + |
| 18 | + QgsRectangle myProjectedViewExtent; |
| 19 | + QgsRectangle myProjectedLayerExtent; |
| 20 | + |
| 21 | + if ( rendererContext.coordinateTransform() ) |
| 22 | + { |
| 23 | + QgsDebugMsg( "coordinateTransform set -> project extents." ); |
| 24 | + try |
| 25 | + { |
| 26 | + myProjectedViewExtent = rendererContext.coordinateTransform()->transformBoundingBox( rendererContext.extent() ); |
| 27 | + } |
| 28 | + catch ( QgsCsException &cs ) |
| 29 | + { |
| 30 | + QgsMessageLog::logMessage( QObject::tr( "Could not reproject view extent: %1" ).arg( cs.what() ), QObject::tr( "Raster" ) ); |
| 31 | + myProjectedViewExtent.setMinimal(); |
| 32 | + } |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + myProjectedLayerExtent = rendererContext.coordinateTransform()->transformBoundingBox( layer->extent() ); |
| 37 | + } |
| 38 | + catch ( QgsCsException &cs ) |
| 39 | + { |
| 40 | + QgsMessageLog::logMessage( QObject::tr( "Could not reproject layer extent: %1" ).arg( cs.what() ), QObject::tr( "Raster" ) ); |
| 41 | + myProjectedViewExtent.setMinimal(); |
| 42 | + } |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + QgsDebugMsg( "coordinateTransform not set" ); |
| 47 | + myProjectedViewExtent = rendererContext.extent(); |
| 48 | + myProjectedLayerExtent = layer->extent(); |
| 49 | + } |
| 50 | + |
| 51 | + // clip raster extent to view extent |
| 52 | + QgsRectangle myRasterExtent = myProjectedViewExtent.intersect( &myProjectedLayerExtent ); |
| 53 | + if ( myRasterExtent.isEmpty() ) |
| 54 | + { |
| 55 | + QgsDebugMsg( "draw request outside view extent." ); |
| 56 | + // nothing to do |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + QgsDebugMsg( "theViewExtent is " + rendererContext.extent().toString() ); |
| 61 | + QgsDebugMsg( "myProjectedViewExtent is " + myProjectedViewExtent.toString() ); |
| 62 | + QgsDebugMsg( "myProjectedLayerExtent is " + myProjectedLayerExtent.toString() ); |
| 63 | + QgsDebugMsg( "myRasterExtent is " + myRasterExtent.toString() ); |
| 64 | + |
| 65 | + // |
| 66 | + // The first thing we do is set up the QgsRasterViewPort. This struct stores all the settings |
| 67 | + // relating to the size (in pixels and coordinate system units) of the raster part that is |
| 68 | + // in view in the map window. It also stores the origin. |
| 69 | + // |
| 70 | + //this is not a class level member because every time the user pans or zooms |
| 71 | + //the contents of the rasterViewPort will change |
| 72 | + mRasterViewPort = new QgsRasterViewPort(); |
| 73 | + |
| 74 | + mRasterViewPort->mDrawnExtent = myRasterExtent; |
| 75 | + if ( rendererContext.coordinateTransform() ) |
| 76 | + { |
| 77 | + mRasterViewPort->mSrcCRS = layer->crs(); |
| 78 | + mRasterViewPort->mDestCRS = rendererContext.coordinateTransform()->destCRS(); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + mRasterViewPort->mSrcCRS = QgsCoordinateReferenceSystem(); // will be invalid |
| 83 | + mRasterViewPort->mDestCRS = QgsCoordinateReferenceSystem(); // will be invalid |
| 84 | + } |
| 85 | + |
| 86 | + // get dimensions of clipped raster image in device coordinate space (this is the size of the viewport) |
| 87 | + mRasterViewPort->mTopLeftPoint = theQgsMapToPixel.transform( myRasterExtent.xMinimum(), myRasterExtent.yMaximum() ); |
| 88 | + mRasterViewPort->mBottomRightPoint = theQgsMapToPixel.transform( myRasterExtent.xMaximum(), myRasterExtent.yMinimum() ); |
| 89 | + |
| 90 | + // align to output device grid, i.e. floor/ceil to integers |
| 91 | + // TODO: this should only be done if paint device is raster - screen, image |
| 92 | + // for other devices (pdf) it can have floating point origin |
| 93 | + // we could use floating point for raster devices as well, but respecting the |
| 94 | + // output device grid should make it more effective as the resampling is done in |
| 95 | + // the provider anyway |
| 96 | + mRasterViewPort->mTopLeftPoint.setX( floor( mRasterViewPort->mTopLeftPoint.x() ) ); |
| 97 | + mRasterViewPort->mTopLeftPoint.setY( floor( mRasterViewPort->mTopLeftPoint.y() ) ); |
| 98 | + mRasterViewPort->mBottomRightPoint.setX( ceil( mRasterViewPort->mBottomRightPoint.x() ) ); |
| 99 | + mRasterViewPort->mBottomRightPoint.setY( ceil( mRasterViewPort->mBottomRightPoint.y() ) ); |
| 100 | + // recalc myRasterExtent to aligned values |
| 101 | + myRasterExtent.set( |
| 102 | + theQgsMapToPixel.toMapCoordinatesF( mRasterViewPort->mTopLeftPoint.x(), |
| 103 | + mRasterViewPort->mBottomRightPoint.y() ), |
| 104 | + theQgsMapToPixel.toMapCoordinatesF( mRasterViewPort->mBottomRightPoint.x(), |
| 105 | + mRasterViewPort->mTopLeftPoint.y() ) |
| 106 | + ); |
| 107 | + |
| 108 | + //raster viewport top left / bottom right are already rounded to int |
| 109 | + mRasterViewPort->mWidth = static_cast<int>( mRasterViewPort->mBottomRightPoint.x() - mRasterViewPort->mTopLeftPoint.x() ); |
| 110 | + mRasterViewPort->mHeight = static_cast<int>( mRasterViewPort->mBottomRightPoint.y() - mRasterViewPort->mTopLeftPoint.y() ); |
| 111 | + |
| 112 | + |
| 113 | + //the drawable area can start to get very very large when you get down displaying 2x2 or smaller, this is becasue |
| 114 | + //theQgsMapToPixel.mapUnitsPerPixel() is less then 1, |
| 115 | + //so we will just get the pixel data and then render these special cases differently in paintImageToCanvas() |
| 116 | + |
| 117 | + QgsDebugMsgLevel( QString( "mapUnitsPerPixel = %1" ).arg( theQgsMapToPixel.mapUnitsPerPixel() ), 3 ); |
| 118 | + QgsDebugMsgLevel( QString( "mWidth = %1" ).arg( layer->width() ), 3 ); |
| 119 | + QgsDebugMsgLevel( QString( "mHeight = %1" ).arg( layer->height() ), 3 ); |
| 120 | + QgsDebugMsgLevel( QString( "myRasterExtent.xMinimum() = %1" ).arg( myRasterExtent.xMinimum() ), 3 ); |
| 121 | + QgsDebugMsgLevel( QString( "myRasterExtent.xMaximum() = %1" ).arg( myRasterExtent.xMaximum() ), 3 ); |
| 122 | + QgsDebugMsgLevel( QString( "myRasterExtent.yMinimum() = %1" ).arg( myRasterExtent.yMinimum() ), 3 ); |
| 123 | + QgsDebugMsgLevel( QString( "myRasterExtent.yMaximum() = %1" ).arg( myRasterExtent.yMaximum() ), 3 ); |
| 124 | + |
| 125 | + QgsDebugMsgLevel( QString( "mTopLeftPoint.x() = %1" ).arg( mRasterViewPort->mTopLeftPoint.x() ), 3 ); |
| 126 | + QgsDebugMsgLevel( QString( "mBottomRightPoint.x() = %1" ).arg( mRasterViewPort->mBottomRightPoint.x() ), 3 ); |
| 127 | + QgsDebugMsgLevel( QString( "mTopLeftPoint.y() = %1" ).arg( mRasterViewPort->mTopLeftPoint.y() ), 3 ); |
| 128 | + QgsDebugMsgLevel( QString( "mBottomRightPoint.y() = %1" ).arg( mRasterViewPort->mBottomRightPoint.y() ), 3 ); |
| 129 | + |
| 130 | + QgsDebugMsgLevel( QString( "mWidth = %1" ).arg( mRasterViewPort->mWidth ), 3 ); |
| 131 | + QgsDebugMsgLevel( QString( "mHeight = %1" ).arg( mRasterViewPort->mHeight ), 3 ); |
| 132 | + |
| 133 | + // /\/\/\ - added to handle zoomed-in rasters |
| 134 | + |
| 135 | + // TODO R->mLastViewPort = *mRasterViewPort; |
| 136 | + |
| 137 | + // TODO: is it necessary? Probably WMS only? |
| 138 | + layer->dataProvider()->setDpi( rendererContext.rasterScaleFactor() * 25.4 * rendererContext.scaleFactor() ); |
| 139 | + |
| 140 | + |
| 141 | + // copy the whole raster pipe! |
| 142 | + mPipe = new QgsRasterPipe( *layer->pipe() ); |
| 143 | +} |
| 144 | + |
| 145 | +QgsRasterLayerRenderer::~QgsRasterLayerRenderer() |
| 146 | +{ |
| 147 | + delete mRasterViewPort; |
| 148 | + delete mPipe; |
| 149 | +} |
| 150 | + |
| 151 | +bool QgsRasterLayerRenderer::render() |
| 152 | +{ |
| 153 | + if ( !mRasterViewPort ) |
| 154 | + return true; // outside of layer extent - nothing to do |
| 155 | + |
| 156 | + //R->draw( mPainter, mRasterViewPort, &mMapToPixel ); |
| 157 | + |
| 158 | + QTime time; |
| 159 | + time.start(); |
| 160 | + // |
| 161 | + // |
| 162 | + // The goal here is to make as many decisions as possible early on (outside of the rendering loop) |
| 163 | + // so that we can maximise performance of the rendering process. So now we check which drawing |
| 164 | + // procedure to use : |
| 165 | + // |
| 166 | + |
| 167 | + QgsRasterProjector *projector = mPipe->projector(); |
| 168 | + |
| 169 | + // TODO add a method to interface to get provider and get provider |
| 170 | + // params in QgsRasterProjector |
| 171 | + if ( projector ) |
| 172 | + { |
| 173 | + projector->setCRS( mRasterViewPort->mSrcCRS, mRasterViewPort->mDestCRS ); |
| 174 | + } |
| 175 | + |
| 176 | + // Drawer to pipe? |
| 177 | + QgsRasterIterator iterator( mPipe->last() ); |
| 178 | + QgsRasterDrawer drawer( &iterator ); |
| 179 | + drawer.draw( mPainter, mRasterViewPort, mMapToPixel ); |
| 180 | + |
| 181 | + QgsDebugMsg( QString( "total raster draw time (ms): %1" ).arg( time.elapsed(), 5 ) ); |
| 182 | + |
| 183 | + return true; |
| 184 | +} |
0 commit comments