Skip to content

Commit c903529

Browse files
committedJul 7, 2012
Use drawer and iterator to render maps
1 parent 3f1056a commit c903529

File tree

4 files changed

+14
-230
lines changed

4 files changed

+14
-230
lines changed
 

‎src/core/raster/qgsrasterdrawer.cpp

Lines changed: 6 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,31 @@
1717

1818
#include "qgslogger.h"
1919
#include "qgsrasterdrawer.h"
20-
//#include "qgsrasterresampler.h"
21-
#include "qgsrasterprojector.h"
22-
#include "qgsrastertransparency.h"
20+
#include "qgsrasteriterator.h"
2321
#include "qgsrasterviewport.h"
2422
#include "qgsmaptopixel.h"
25-
26-
//resamplers
27-
//#include "qgsbilinearrasterresampler.h"
28-
//#include "qgscubicrasterresampler.h"
29-
30-
#include <QDomDocument>
31-
#include <QDomElement>
3223
#include <QImage>
3324
#include <QPainter>
3425

35-
QgsRasterDrawer::QgsRasterDrawer( QgsRasterInterface* input ): mInput( input )
26+
QgsRasterDrawer::QgsRasterDrawer( QgsRasterIterator* iterator ): mIterator( iterator )
3627
{
3728
}
3829

3930
QgsRasterDrawer::~QgsRasterDrawer()
4031
{
41-
//remove remaining memory in partinfos
42-
/*
43-
QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.begin();
44-
for ( ; partIt != mRasterPartInfos.end(); ++partIt )
45-
{
46-
CPLFree( partIt.value().data );
47-
}
48-
*/
4932
}
5033

5134
void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel )
5235
{
5336
QgsDebugMsg( "Entered" );
54-
if ( !p || !mInput || !viewPort || !theQgsMapToPixel )
37+
if ( !p || !mIterator || !viewPort || !theQgsMapToPixel )
5538
{
5639
return;
5740
}
5841

5942
// last pipe filter has only 1 band
6043
int bandNumber = 1;
61-
startRasterRead( bandNumber, viewPort, theQgsMapToPixel );
44+
mIterator->startRasterRead( bandNumber, viewPort, theQgsMapToPixel );
6245

6346
//number of cols/rows in output pixels
6447
int nCols = 0;
@@ -76,8 +59,8 @@ void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsM
7659
void* rasterData;
7760

7861
// readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
79-
while ( readNextRasterPart( bandNumber, viewPort, nCols, nRows,
80-
&rasterData, topLeftCol, topLeftRow ) )
62+
while ( mIterator->readNextRasterPart( bandNumber, viewPort, nCols, nRows,
63+
&rasterData, topLeftCol, topLeftRow ) )
8164
{
8265
//create image
8366
//QImage img( nRasterCols, nRasterRows, QImage::Format_ARGB32_Premultiplied );
@@ -91,120 +74,6 @@ void QgsRasterDrawer::draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsM
9174
}
9275
}
9376

94-
void QgsRasterDrawer::startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel )
95-
{
96-
if ( !viewPort || !mapToPixel || !mInput )
97-
{
98-
return;
99-
}
100-
101-
//remove any previous part on that band
102-
removePartInfo( bandNumber );
103-
104-
//split raster into small portions if necessary
105-
RasterPartInfo pInfo;
106-
pInfo.nCols = viewPort->drawableAreaXDim;
107-
pInfo.nRows = viewPort->drawableAreaYDim;
108-
109-
//effective oversampling factors are different to global one because of rounding
110-
//oversamplingX = (( double )pInfo.nCols * oversampling ) / viewPort->drawableAreaXDim;
111-
//oversamplingY = (( double )pInfo.nRows * oversampling ) / viewPort->drawableAreaYDim;
112-
113-
// TODO : we dont know oversampling (grid size) here - how to get totalMemoryUsage ?
114-
//int totalMemoryUsage = pInfo.nCols * oversamplingX * pInfo.nRows * oversamplingY * mInput->dataTypeSize( bandNumber );
115-
int totalMemoryUsage = pInfo.nCols * pInfo.nRows * mInput->dataTypeSize( bandNumber );
116-
int parts = totalMemoryUsage / 100000000 + 1;
117-
int nPartsPerDimension = sqrt( parts );
118-
pInfo.nColsPerPart = pInfo.nCols / nPartsPerDimension;
119-
pInfo.nRowsPerPart = pInfo.nRows / nPartsPerDimension;
120-
pInfo.currentCol = 0;
121-
pInfo.currentRow = 0;
122-
pInfo.data = 0;
123-
pInfo.prj = 0;
124-
mRasterPartInfos.insert( bandNumber, pInfo );
125-
}
126-
127-
bool QgsRasterDrawer::readNextRasterPart( int bandNumber, QgsRasterViewPort* viewPort,
128-
int& nCols, int& nRows, void** rasterData, int& topLeftCol, int& topLeftRow )
129-
{
130-
if ( !viewPort )
131-
{
132-
return false;
133-
}
134-
135-
//get partinfo
136-
QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
137-
if ( partIt == mRasterPartInfos.end() )
138-
{
139-
return false;
140-
}
141-
142-
RasterPartInfo& pInfo = partIt.value();
143-
144-
//remove last data block
145-
// TODO: data are released somewhere else (check)
146-
//free( pInfo.data );
147-
pInfo.data = 0;
148-
delete pInfo.prj;
149-
pInfo.prj = 0;
150-
151-
//already at end
152-
if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow == pInfo.nRows )
153-
{
154-
return false;
155-
}
156-
157-
//read data block
158-
nCols = qMin( pInfo.nColsPerPart, pInfo.nCols - pInfo.currentCol );
159-
nRows = qMin( pInfo.nRowsPerPart, pInfo.nRows - pInfo.currentRow );
160-
int typeSize = mInput->dataTypeSize( bandNumber ) / 8;
161-
162-
//get subrectangle
163-
QgsRectangle viewPortExtent = viewPort->mDrawnExtent;
164-
double xmin = viewPortExtent.xMinimum() + pInfo.currentCol / ( double )pInfo.nCols * viewPortExtent.width();
165-
double xmax = viewPortExtent.xMinimum() + ( pInfo.currentCol + nCols ) / ( double )pInfo.nCols * viewPortExtent.width();
166-
double ymin = viewPortExtent.yMaximum() - ( pInfo.currentRow + nRows ) / ( double )pInfo.nRows * viewPortExtent.height();
167-
double ymax = viewPortExtent.yMaximum() - pInfo.currentRow / ( double )pInfo.nRows * viewPortExtent.height();
168-
QgsRectangle blockRect( xmin, ymin, xmax, ymax );
169-
170-
pInfo.data = mInput->block( bandNumber, blockRect, nCols, nRows );
171-
172-
*rasterData = pInfo.data;
173-
topLeftCol = pInfo.currentCol;
174-
topLeftRow = pInfo.currentRow;
175-
176-
pInfo.currentCol += nCols;
177-
if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow + nRows == pInfo.nRows ) //end of raster
178-
{
179-
pInfo.currentRow = pInfo.nRows;
180-
}
181-
else if ( pInfo.currentCol == pInfo.nCols ) //start new row
182-
{
183-
pInfo.currentCol = 0;
184-
pInfo.currentRow += pInfo.nRowsPerPart;
185-
}
186-
187-
return true;
188-
}
189-
190-
void QgsRasterDrawer::stopRasterRead( int bandNumber )
191-
{
192-
removePartInfo( bandNumber );
193-
}
194-
195-
void QgsRasterDrawer::removePartInfo( int bandNumber )
196-
{
197-
QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
198-
if ( partIt != mRasterPartInfos.end() )
199-
{
200-
RasterPartInfo& pInfo = partIt.value();
201-
//CPLFree( pInfo.data );
202-
free( pInfo.data );
203-
delete pInfo.prj;
204-
mRasterPartInfos.remove( bandNumber );
205-
}
206-
}
207-
20877
void QgsRasterDrawer::drawImage( QPainter* p, QgsRasterViewPort* viewPort, const QImage& img, int topLeftCol, int topLeftRow ) const
20978
{
21079
if ( !p || !viewPort )

‎src/core/raster/qgsrasterdrawer.h

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,119 +18,31 @@
1818
#ifndef QGSRASTERDRAWER_H
1919
#define QGSRASTERDRAWER_H
2020

21-
//#include "qgsrasterdataprovider.h"
2221
#include "qgsrasterinterface.h"
23-
2422
#include <QMap>
2523

2624
class QPainter;
2725
class QImage;
2826
class QgsMapToPixel;
29-
class QgsRasterResampler;
30-
class QgsRasterProjector;
31-
class QgsRasterTransparency;
3227
class QgsRasterViewPort;
33-
34-
class QDomElement;
28+
class QgsRasterIterator;
3529

3630
class QgsRasterDrawer
3731
{
3832
public:
39-
//Stores information about reading of a raster band. Columns and rows are in unsampled coordinates
40-
struct RasterPartInfo
41-
{
42-
int currentCol;
43-
int currentRow;
44-
int nCols;
45-
int nRows;
46-
int nColsPerPart;
47-
int nRowsPerPart;
48-
void* data; //data (can be in oversampled/undersampled resolution)
49-
QgsRasterProjector* prj; //raster projector (or 0 if no reprojection is done)
50-
};
51-
52-
QgsRasterDrawer( QgsRasterInterface* input );
33+
QgsRasterDrawer( QgsRasterIterator* iterator );
5334
~QgsRasterDrawer();
5435

5536
void draw( QPainter* p, QgsRasterViewPort* viewPort, const QgsMapToPixel* theQgsMapToPixel );
5637

5738
protected:
58-
//inline double readValue( void *data, QgsRasterInterface::DataType type, int index );
59-
60-
/**Start reading of raster band. Raster data can then be retrieved by calling readNextRasterPart until it returns false.
61-
@param bandNumer number of raster band to read
62-
@param viewPort describes raster position on screen
63-
*/
64-
void startRasterRead( int bandNumber, QgsRasterViewPort* viewPort, const QgsMapToPixel* mapToPixel );
65-
/**Fetches next part of raster data
66-
@param nCols number of columns on output device
67-
@param nRows number of rows on output device
68-
@param nColsRaster number of raster columns (different to nCols if oversamplingX != 1.0)
69-
@param nRowsRaster number of raster rows (different to nRows if oversamplingY != 0)*/
70-
//bool readNextRasterPart( int bandNumber, QgsRasterViewPort* viewPort,
71-
// int& nCols, int& nRows,
72-
// int& nColsRaster, int& nRowsRaster,
73-
// void** rasterData, int& topLeftCol, int& topLeftRow );
74-
bool readNextRasterPart( int bandNumber, QgsRasterViewPort* viewPort,
75-
int& nCols, int& nRows,
76-
void** rasterData,
77-
int& topLeftCol, int& topLeftRow );
7839
/**Draws raster part
7940
@param topLeftCol Left position relative to left border of viewport
8041
@param topLeftRow Top position relative to top border of viewport*/
8142
void drawImage( QPainter* p, QgsRasterViewPort* viewPort, const QImage& img, int topLeftCol, int topLeftRow ) const;
82-
void stopRasterRead( int bandNumber );
83-
84-
QgsRasterInterface* mInput;
85-
QMap<int, RasterPartInfo> mRasterPartInfos;
8643

8744
private:
88-
/**Remove part into and release memory*/
89-
void removePartInfo( int bandNumer );
45+
QgsRasterIterator* mIterator;
9046
};
91-
/*
92-
inline double QgsRasterDrawer::readValue( void *data, QgsRasterInterface::DataType type, int index )
93-
{
94-
if ( !mProvider )
95-
{
96-
return 0;
97-
}
98-
99-
if ( !data )
100-
{
101-
return mProvider->noDataValue();
102-
}
103-
104-
switch ( type )
105-
{
106-
case QgsRasterInterface::Byte:
107-
return ( double )(( GByte * )data )[index];
108-
break;
109-
case QgsRasterInterface::UInt16:
110-
return ( double )(( GUInt16 * )data )[index];
111-
break;
112-
case QgsRasterInterface::Int16:
113-
return ( double )(( GInt16 * )data )[index];
114-
break;
115-
case QgsRasterInterface::UInt32:
116-
return ( double )(( GUInt32 * )data )[index];
117-
break;
118-
case QgsRasterInterface::Int32:
119-
return ( double )(( GInt32 * )data )[index];
120-
break;
121-
case QgsRasterInterface::Float32:
122-
return ( double )(( float * )data )[index];
123-
break;
124-
case QgsRasterInterface::Float64:
125-
return ( double )(( double * )data )[index];
126-
break;
127-
default:
128-
//QgsMessageLog::logMessage( tr( "GDAL data type %1 is not supported" ).arg( type ), tr( "Raster" ) );
129-
break;
130-
}
131-
132-
return mProvider->noDataValue();
133-
}
134-
*/
13547

13648
#endif // QGSRASTERDRAWER_H

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ email : tim at linfiniti.com
4848

4949
#include "qgsrasterprojector.h"
5050

51+
#include "qgsrasteriterator.h"
52+
#include "qgsrasterdrawer.h"
53+
5154
#include <cstdio>
5255
#include <cmath>
5356
#include <limits>
@@ -844,7 +847,8 @@ void QgsRasterLayer::draw( QPainter * theQPainter,
844847
#endif
845848

846849
// Drawer to pipe?
847-
QgsRasterDrawer drawer( mPipe.last() );
850+
QgsRasterIterator iterator( mPipe.last() );
851+
QgsRasterDrawer drawer( &iterator );
848852
drawer.draw( theQPainter, theRasterViewPort, theQgsMapToPixel );
849853

850854
#ifdef QGISDEBUG

‎src/core/raster/qgsrasterlayer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include "qgscolorrampshader.h"
4343
#include "qgsrastershaderfunction.h"
4444
#include "qgsrasterinterface.h"
45-
#include "qgsrasterdrawer.h"
4645
#include "qgsrasterresamplefilter.h"
4746
#include "qgsrasterdataprovider.h"
4847
#include "qgsrasterpipe.h"

0 commit comments

Comments
 (0)
Please sign in to comment.