22 , mTileOverlapPixels( tileOverlapPixels )
31 mMaximumTileWidth = rdp->
stepWidth() - 2 * mTileOverlapPixels;
32 mMaximumTileHeight = rdp->
stepHeight() - 2 * mTileOverlapPixels;
40 const double xRes = rasterExtent.
width() / rasterWidth;
41 const double yRes = rasterExtent.
height() / rasterHeight;
44 int bottom = rasterHeight - 1;
46 int right = rasterWidth - 1;
50 top =
static_cast< int >( std::floor( ( rasterExtent.
yMaximum() -
subRegion.yMaximum() ) / yRes ) );
54 bottom =
static_cast< int >( std::ceil( ( rasterExtent.
yMaximum() -
subRegion.yMinimum() ) / yRes ) - 1 );
59 left =
static_cast< int >( std::floor( (
subRegion.xMinimum() - rasterExtent.
xMinimum() ) / xRes ) );
63 right =
static_cast< int >( std::ceil( (
subRegion.xMaximum() - rasterExtent.
xMinimum() ) / xRes ) - 1 );
66 if ( resamplingFactor > 1 )
69 left = ( ( left + resamplingFactor - 1 ) / resamplingFactor ) * resamplingFactor;
70 top = ( ( top + resamplingFactor - 1 ) / resamplingFactor ) * resamplingFactor;
73 right = ( right / resamplingFactor ) * resamplingFactor - 1;
74 bottom = ( bottom / resamplingFactor ) * resamplingFactor - 1;
77 subRegionWidth = right - left + 1;
78 subRegionHeight = bottom - top + 1;
84 rasterExtent.
yMaximum() - ( ( top + subRegionHeight ) * yRes ),
85 rasterExtent.
xMinimum() + ( ( left + subRegionWidth ) * xRes ),
86 rasterExtent.
yMaximum() - ( top * yRes ) );
100 removePartInfo( bandNumber );
103 RasterPartInfo pInfo;
106 pInfo.currentCol = 0;
107 pInfo.currentRow = 0;
108 mRasterPartInfos.insert( bandNumber, pInfo );
110 mNumberBlocksWidth =
static_cast< int >( std::ceil(
static_cast< double >( nCols ) / mMaximumTileWidth ) );
111 mNumberBlocksHeight =
static_cast< int >( std::ceil(
static_cast< double >( nRows ) / mMaximumTileHeight ) );
116 int outTileColumns = 0;
118 int outTileTopLeftColumn = 0;
119 int outTileTopLeftRow = 0;
120 return readNextRasterPartInternal( bandNumber, columns, rows,
nullptr, topLeftColumn, topLeftRow, &blockExtent, outTileColumns, outTileRows, outTileTopLeftColumn, outTileTopLeftRow );
124 int &nCols,
int &nRows,
126 int &topLeftCol,
int &topLeftRow )
129 std::unique_ptr< QgsRasterBlock > nextBlock;
130 const bool result =
readNextRasterPart( bandNumber, nCols, nRows, nextBlock, topLeftCol, topLeftRow );
132 *block = nextBlock.release();
136bool QgsRasterIterator::readNextRasterPart(
int bandNumber,
int &nCols,
int &nRows, std::unique_ptr<QgsRasterBlock> &block,
int &topLeftCol,
int &topLeftRow,
QgsRectangle *blockExtent,
int *tileColumns,
int *tileRows,
int *tileTopLeftColumn,
int *tileTopLeftRow )
138 int outTileColumns = 0;
140 int outTileTopLeftColumn = 0;
141 int outTileTopLeftRow = 0;
142 const bool res = readNextRasterPartInternal( bandNumber, nCols, nRows, &block, topLeftCol, topLeftRow, blockExtent, outTileColumns, outTileRows, outTileTopLeftColumn, outTileTopLeftRow );
145 *tileColumns = outTileColumns;
147 *tileRows = outTileRows;
148 if ( tileTopLeftColumn )
149 *tileTopLeftColumn = outTileTopLeftColumn;
150 if ( tileTopLeftRow )
151 *tileTopLeftRow = outTileTopLeftRow;
156bool QgsRasterIterator::readNextRasterPartInternal(
int bandNumber,
int &nCols,
int &nRows, std::unique_ptr<QgsRasterBlock> *block,
int &topLeftCol,
int &topLeftRow,
QgsRectangle *blockExtent,
int &tileColumns,
int &tileRows,
int &tileTopLeftColumn,
int &tileTopLeftRow )
162 const QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
163 if ( partIt == mRasterPartInfos.end() )
168 RasterPartInfo &pInfo = partIt.value();
171 if ( 0 == pInfo.nCols || 0 == pInfo.nRows )
179 if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow == pInfo.nRows )
185 tileTopLeftColumn = pInfo.currentCol;
186 tileTopLeftRow = pInfo.currentRow;
188 tileColumns =
static_cast< int >( std::min(
static_cast< qgssize >( mMaximumTileWidth ), pInfo.nCols - tileTopLeftColumn ) );
189 tileRows =
static_cast< int >( std::min(
static_cast< qgssize >( mMaximumTileHeight ), pInfo.nRows - tileTopLeftRow ) );
191 if ( mSnapToPixelFactor > 1 )
194 tileColumns = ( tileColumns / mSnapToPixelFactor ) * mSnapToPixelFactor;
195 tileRows = ( tileRows / mSnapToPixelFactor ) * mSnapToPixelFactor;
198 const qgssize tileRight = tileTopLeftColumn + tileColumns;
199 const qgssize tileBottom = tileTopLeftRow + tileRows;
201 const qgssize blockLeft = tileTopLeftColumn >= mTileOverlapPixels ? ( tileTopLeftColumn - mTileOverlapPixels ) : 0;
202 const qgssize blockTop = tileTopLeftRow >= mTileOverlapPixels ? ( tileTopLeftRow - mTileOverlapPixels ) : 0;
203 const qgssize blockRight = std::min< qgssize >( tileRight + mTileOverlapPixels, pInfo.nCols );
204 const qgssize blockBottom = std::min< qgssize >( tileBottom + mTileOverlapPixels, pInfo.nRows );
206 nCols = blockRight - blockLeft;
207 nRows = blockBottom - blockTop;
209 if ( mSnapToPixelFactor > 1 )
212 nCols = ( nCols / mSnapToPixelFactor ) * mSnapToPixelFactor;
213 nRows = ( nRows / mSnapToPixelFactor ) * mSnapToPixelFactor;
214 if ( nCols == 0 || nRows == 0 )
218 QgsDebugMsgLevel( QStringLiteral(
"nCols = %1 nRows = %2" ).arg( nCols ).arg( nRows ), 4 );
221 const QgsRectangle viewPortExtent = mExtent;
222 const double xmin = viewPortExtent.
xMinimum() + blockLeft /
static_cast< double >( pInfo.nCols ) * viewPortExtent.
width();
223 const double xmax = blockLeft + nCols == pInfo.nCols ? viewPortExtent.
xMaximum() :
224 viewPortExtent.
xMinimum() + ( blockLeft + nCols ) /
static_cast< double >( pInfo.nCols ) * viewPortExtent.
width();
225 const double ymin = blockTop + nRows == pInfo.nRows ? viewPortExtent.
yMinimum() :
226 viewPortExtent.
yMaximum() - ( blockTop + nRows ) /
static_cast< double >( pInfo.nRows ) * viewPortExtent.
height();
227 const double ymax = viewPortExtent.
yMaximum() - blockTop /
static_cast< double >( pInfo.nRows ) * viewPortExtent.
height();
228 const QgsRectangle blockRect( xmin, ymin, xmax, ymax );
231 *blockExtent = blockRect;
234 block->reset( mInput->block( bandNumber, blockRect, nCols, nRows, mFeedback ) );
235 topLeftCol = blockLeft;
236 topLeftRow = blockTop;
238 pInfo.currentCol = tileRight;
239 if ( pInfo.currentCol == pInfo.nCols && tileBottom == pInfo.nRows )
241 pInfo.currentRow = pInfo.nRows;
243 else if ( pInfo.currentCol == pInfo.nCols )
245 pInfo.currentCol = 0;
246 pInfo.currentRow = tileBottom;
254 removePartInfo( bandNumber );
259 const auto partIt = mRasterPartInfos.find( bandNumber );
260 if ( partIt == mRasterPartInfos.constEnd() )
265 return ( (
static_cast< double >( partIt->currentRow ) /
static_cast< double >( mMaximumTileHeight ) ) * mNumberBlocksWidth +
static_cast< double >( partIt->currentCol ) /
static_cast< double >( mMaximumTileWidth ) ) / (
static_cast< double >( mNumberBlocksWidth ) *
static_cast< double >( mNumberBlocksHeight ) );
268void QgsRasterIterator::removePartInfo(
int bandNumber )
270 const auto partIt = mRasterPartInfos.constFind( bandNumber );
271 if ( partIt != mRasterPartInfos.constEnd() )
273 mRasterPartInfos.remove( bandNumber );
Feedback object tailored for raster block reading.
Base class for raster data providers.
virtual int stepHeight() const
Step height for raster iterations.
virtual int stepWidth() const
Step width for raster iterations.
Base class for processing filters like renderers, reprojector, resampler etc.
static QgsRectangle subRegion(const QgsRectangle &rasterExtent, int rasterWidth, int rasterHeight, const QgsRectangle &subRegion, int &subRegionWidth, int &subRegionHeight, int &subRegionLeft, int &subRegionTop, int resamplingFactor=1)
Given an overall raster extent and width and height in pixels, calculates the sub region of the raste...
bool next(int bandNumber, int &columns, int &rows, int &topLeftColumn, int &topLeftRow, QgsRectangle &blockExtent)
Fetches details of the next part of the raster data.
QgsRasterIterator(QgsRasterInterface *input, int tileOverlapPixels=0)
Constructor for QgsRasterIterator, iterating over the specified input raster source.
const QgsRasterInterface * input() const
Returns the input raster interface which is being iterated over.
void stopRasterRead(int bandNumber)
Cancels the raster iteration and resets the iterator.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
Default maximum tile width.
bool readNextRasterPart(int bandNumber, int &nCols, int &nRows, QgsRasterBlock **block, int &topLeftCol, int &topLeftRow)
Fetches next part of raster data, caller takes ownership of the block and caller should delete the bl...
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
Default maximum tile height.
void startRasterRead(int bandNumber, qgssize nCols, qgssize nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback=nullptr)
Start reading of raster band.
double progress(int bandNumber) const
Returns the raster iteration progress as a fraction from 0 to 1.0, for the specified bandNumber.
A rectangle specified with double values.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
#define QgsDebugMsgLevel(str, level)