QGIS API Documentation  2.99.0-Master (cd0ba91)
qgsrasteriterator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasteriterator.cpp
3  ---------------------
4  begin : July 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsrasteriterator.h"
16 #include "qgsrasterinterface.h"
17 #include "qgsrasterprojector.h"
18 #include "qgsrasterviewport.h"
19 #include "qgsrasterdataprovider.h"
20 
22  : mInput( input )
23  , mMaximumTileWidth( DEFAULT_MAXIMUM_TILE_WIDTH )
24  , mMaximumTileHeight( DEFAULT_MAXIMUM_TILE_HEIGHT )
25 {
26  for ( QgsRasterInterface *ri = input; ri; ri = ri->input() )
27  {
28  QgsRasterDataProvider *rdp = dynamic_cast<QgsRasterDataProvider *>( ri );
29  if ( rdp )
30  {
31  mMaximumTileWidth = rdp->stepWidth();
32  mMaximumTileHeight = rdp->stepHeight();
33  break;
34  }
35  }
36 }
37 
38 void QgsRasterIterator::startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback )
39 {
40  if ( !mInput )
41  {
42  return;
43  }
44 
45  mExtent = extent;
46  mFeedback = feedback;
47 
48  //remove any previous part on that band
49  removePartInfo( bandNumber );
50 
51  //split raster into small portions if necessary
52  RasterPartInfo pInfo;
53  pInfo.nCols = nCols;
54  pInfo.nRows = nRows;
55  pInfo.currentCol = 0;
56  pInfo.currentRow = 0;
57  pInfo.prj = nullptr;
58  mRasterPartInfos.insert( bandNumber, pInfo );
59 }
60 
62  int &nCols, int &nRows,
63  QgsRasterBlock **block,
64  int &topLeftCol, int &topLeftRow )
65 {
66  QgsDebugMsgLevel( "Entered", 4 );
67  *block = nullptr;
68  //get partinfo
69  QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
70  if ( partIt == mRasterPartInfos.end() )
71  {
72  return false;
73  }
74 
75  RasterPartInfo &pInfo = partIt.value();
76 
77  // If we started with zero cols or zero rows, just return (avoids divide by zero below)
78  if ( 0 == pInfo.nCols || 0 == pInfo.nRows )
79  {
80  return false;
81  }
82 
83  //remove last data block
84  delete pInfo.prj;
85  pInfo.prj = nullptr;
86 
87  //already at end
88  if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow == pInfo.nRows )
89  {
90  return false;
91  }
92 
93  //read data block
94  nCols = std::min( mMaximumTileWidth, pInfo.nCols - pInfo.currentCol );
95  nRows = std::min( mMaximumTileHeight, pInfo.nRows - pInfo.currentRow );
96  QgsDebugMsgLevel( QString( "nCols = %1 nRows = %2" ).arg( nCols ).arg( nRows ), 4 );
97 
98  //get subrectangle
99  QgsRectangle viewPortExtent = mExtent;
100  double xmin = viewPortExtent.xMinimum() + pInfo.currentCol / static_cast< double >( pInfo.nCols ) * viewPortExtent.width();
101  double xmax = pInfo.currentCol + nCols == pInfo.nCols ? viewPortExtent.xMaximum() : // avoid extra FP math if not necessary
102  viewPortExtent.xMinimum() + ( pInfo.currentCol + nCols ) / static_cast< double >( pInfo.nCols ) * viewPortExtent.width();
103  double ymin = pInfo.currentRow + nRows == pInfo.nRows ? viewPortExtent.yMinimum() : // avoid extra FP math if not necessary
104  viewPortExtent.yMaximum() - ( pInfo.currentRow + nRows ) / static_cast< double >( pInfo.nRows ) * viewPortExtent.height();
105  double ymax = viewPortExtent.yMaximum() - pInfo.currentRow / static_cast< double >( pInfo.nRows ) * viewPortExtent.height();
106  QgsRectangle blockRect( xmin, ymin, xmax, ymax );
107 
108  *block = mInput->block( bandNumber, blockRect, nCols, nRows, mFeedback );
109  topLeftCol = pInfo.currentCol;
110  topLeftRow = pInfo.currentRow;
111 
112  pInfo.currentCol += nCols;
113  if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow + nRows == pInfo.nRows ) //end of raster
114  {
115  pInfo.currentRow = pInfo.nRows;
116  }
117  else if ( pInfo.currentCol == pInfo.nCols ) //start new row
118  {
119  pInfo.currentCol = 0;
120  pInfo.currentRow += nRows;
121  }
122 
123  return true;
124 }
125 
126 void QgsRasterIterator::stopRasterRead( int bandNumber )
127 {
128  removePartInfo( bandNumber );
129 }
130 
131 void QgsRasterIterator::removePartInfo( int bandNumber )
132 {
133  QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
134  if ( partIt != mRasterPartInfos.end() )
135  {
136  RasterPartInfo &pInfo = partIt.value();
137  delete pInfo.prj;
138  mRasterPartInfos.remove( bandNumber );
139  }
140 }
A rectangle specified with double values.
Definition: qgsrectangle.h:39
QgsRasterIterator(QgsRasterInterface *input)
virtual QgsRasterInterface * input() const
Current input.
Raster data container.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:38
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:138
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...
Base class for processing filters like renderers, reprojector, resampler etc.
void stopRasterRead(int bandNumber)
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:126
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:111
virtual int stepHeight() const
Step height for raster iterations.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:116
virtual int stepWidth() const
Step width for raster iterations.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:121
Feedback object tailored for raster block reading.
void startRasterRead(int bandNumber, int nCols, int nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback=nullptr)
Start reading of raster band.
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:145
Base class for raster data providers.