QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
21  mMaximumTileWidth( 2000 ), mMaximumTileHeight( 2000 )
22 {
23 }
24 
26 {
27 }
28 
29 void QgsRasterIterator::startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle& extent )
30 {
31  if ( !mInput )
32  {
33  return;
34  }
35 
36  mExtent = extent;
37 
38  //remove any previous part on that band
39  removePartInfo( bandNumber );
40 
41  //split raster into small portions if necessary
42  RasterPartInfo pInfo;
43  pInfo.nCols = nCols;
44  pInfo.nRows = nRows;
45  pInfo.currentCol = 0;
46  pInfo.currentRow = 0;
47  pInfo.prj = 0;
48  mRasterPartInfos.insert( bandNumber, pInfo );
49 }
50 
52  int& nCols, int& nRows,
53  QgsRasterBlock **block,
54  int& topLeftCol, int& topLeftRow )
55 {
56  QgsDebugMsg( "Entered" );
57  *block = 0;
58  //get partinfo
59  QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
60  if ( partIt == mRasterPartInfos.end() )
61  {
62  return false;
63  }
64 
65  RasterPartInfo& pInfo = partIt.value();
66 
67  // If we started with zero cols or zero rows, just return (avoids divide by zero below)
68  if ( 0 == pInfo.nCols || 0 == pInfo.nRows )
69  {
70  return false;
71  }
72 
73  //remove last data block
74  delete pInfo.prj;
75  pInfo.prj = 0;
76 
77  //already at end
78  if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow == pInfo.nRows )
79  {
80  return false;
81  }
82 
83  //read data block
84  nCols = qMin( mMaximumTileWidth, pInfo.nCols - pInfo.currentCol );
85  nRows = qMin( mMaximumTileHeight, pInfo.nRows - pInfo.currentRow );
86  QgsDebugMsg( QString( "nCols = %1 nRows = %2" ).arg( nCols ).arg( nRows ) );
87 
88  //get subrectangle
89  QgsRectangle viewPortExtent = mExtent;
90  double xmin = viewPortExtent.xMinimum() + pInfo.currentCol / ( double )pInfo.nCols * viewPortExtent.width();
91  double xmax = viewPortExtent.xMinimum() + ( pInfo.currentCol + nCols ) / ( double )pInfo.nCols * viewPortExtent.width();
92  double ymin = viewPortExtent.yMaximum() - ( pInfo.currentRow + nRows ) / ( double )pInfo.nRows * viewPortExtent.height();
93  double ymax = viewPortExtent.yMaximum() - pInfo.currentRow / ( double )pInfo.nRows * viewPortExtent.height();
94  QgsRectangle blockRect( xmin, ymin, xmax, ymax );
95 
96  *block = mInput->block( bandNumber, blockRect, nCols, nRows );
97  topLeftCol = pInfo.currentCol;
98  topLeftRow = pInfo.currentRow;
99 
100  pInfo.currentCol += nCols;
101  if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow + nRows == pInfo.nRows ) //end of raster
102  {
103  pInfo.currentRow = pInfo.nRows;
104  }
105  else if ( pInfo.currentCol == pInfo.nCols ) //start new row
106  {
107  pInfo.currentCol = 0;
108  pInfo.currentRow += nRows;
109  }
110 
111  return true;
112 }
113 
114 void QgsRasterIterator::stopRasterRead( int bandNumber )
115 {
116  removePartInfo( bandNumber );
117 }
118 
119 void QgsRasterIterator::removePartInfo( int bandNumber )
120 {
121  QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
122  if ( partIt != mRasterPartInfos.end() )
123  {
124  RasterPartInfo& pInfo = partIt.value();
125  delete pInfo.prj;
126  mRasterPartInfos.remove( bandNumber );
127  }
128 }