|
QGIS API Documentation
master-3f58142
|
00001 /*************************************************************************** 00002 qgsrasteriterator.cpp 00003 --------------------- 00004 begin : July 2012 00005 copyright : (C) 2012 by Marco Hugentobler 00006 email : marco dot hugentobler at sourcepole dot ch 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 #include "qgsrasteriterator.h" 00016 #include "qgsrasterinterface.h" 00017 #include "qgsrasterprojector.h" 00018 #include "qgsrasterviewport.h" 00019 00020 QgsRasterIterator::QgsRasterIterator( QgsRasterInterface* input ): mInput( input ), 00021 mMaximumTileWidth( 2000 ), mMaximumTileHeight( 2000 ) 00022 { 00023 } 00024 00025 QgsRasterIterator::~QgsRasterIterator() 00026 { 00027 } 00028 00029 void QgsRasterIterator::startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle& extent ) 00030 { 00031 if ( !mInput ) 00032 { 00033 return; 00034 } 00035 00036 mExtent = extent; 00037 00038 //remove any previous part on that band 00039 removePartInfo( bandNumber ); 00040 00041 //split raster into small portions if necessary 00042 RasterPartInfo pInfo; 00043 pInfo.nCols = nCols; 00044 pInfo.nRows = nRows; 00045 pInfo.currentCol = 0; 00046 pInfo.currentRow = 0; 00047 pInfo.prj = 0; 00048 mRasterPartInfos.insert( bandNumber, pInfo ); 00049 } 00050 00051 bool QgsRasterIterator::readNextRasterPart( int bandNumber, 00052 int& nCols, int& nRows, 00053 QgsRasterBlock **block, 00054 int& topLeftCol, int& topLeftRow ) 00055 { 00056 QgsDebugMsg( "Entered" ); 00057 *block = 0; 00058 //get partinfo 00059 QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber ); 00060 if ( partIt == mRasterPartInfos.end() ) 00061 { 00062 return false; 00063 } 00064 00065 RasterPartInfo& pInfo = partIt.value(); 00066 00067 // If we started with zero cols or zero rows, just return (avoids divide by zero below) 00068 if ( 0 == pInfo.nCols || 0 == pInfo.nRows ) 00069 { 00070 return false; 00071 } 00072 00073 //remove last data block 00074 delete pInfo.prj; 00075 pInfo.prj = 0; 00076 00077 //already at end 00078 if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow == pInfo.nRows ) 00079 { 00080 return false; 00081 } 00082 00083 //read data block 00084 nCols = qMin( mMaximumTileWidth, pInfo.nCols - pInfo.currentCol ); 00085 nRows = qMin( mMaximumTileHeight, pInfo.nRows - pInfo.currentRow ); 00086 QgsDebugMsg( QString( "nCols = %1 nRows = %2" ).arg( nCols ).arg( nRows ) ); 00087 00088 //get subrectangle 00089 QgsRectangle viewPortExtent = mExtent; 00090 double xmin = viewPortExtent.xMinimum() + pInfo.currentCol / ( double )pInfo.nCols * viewPortExtent.width(); 00091 double xmax = viewPortExtent.xMinimum() + ( pInfo.currentCol + nCols ) / ( double )pInfo.nCols * viewPortExtent.width(); 00092 double ymin = viewPortExtent.yMaximum() - ( pInfo.currentRow + nRows ) / ( double )pInfo.nRows * viewPortExtent.height(); 00093 double ymax = viewPortExtent.yMaximum() - pInfo.currentRow / ( double )pInfo.nRows * viewPortExtent.height(); 00094 QgsRectangle blockRect( xmin, ymin, xmax, ymax ); 00095 00096 *block = mInput->block( bandNumber, blockRect, nCols, nRows ); 00097 topLeftCol = pInfo.currentCol; 00098 topLeftRow = pInfo.currentRow; 00099 00100 pInfo.currentCol += nCols; 00101 if ( pInfo.currentCol == pInfo.nCols && pInfo.currentRow + nRows == pInfo.nRows ) //end of raster 00102 { 00103 pInfo.currentRow = pInfo.nRows; 00104 } 00105 else if ( pInfo.currentCol == pInfo.nCols ) //start new row 00106 { 00107 pInfo.currentCol = 0; 00108 pInfo.currentRow += nRows; 00109 } 00110 00111 return true; 00112 } 00113 00114 void QgsRasterIterator::stopRasterRead( int bandNumber ) 00115 { 00116 removePartInfo( bandNumber ); 00117 } 00118 00119 void QgsRasterIterator::removePartInfo( int bandNumber ) 00120 { 00121 QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber ); 00122 if ( partIt != mRasterPartInfos.end() ) 00123 { 00124 RasterPartInfo& pInfo = partIt.value(); 00125 delete pInfo.prj; 00126 mRasterPartInfos.remove( bandNumber ); 00127 } 00128 }